Fakultas Ilmu Komputer UI

models.py 5.13 KB
Newer Older
Saul Andre's avatar
Saul Andre committed
1
import random
2

3
from django.core.exceptions import ValidationError
4
from django.core.validators import MinValueValidator, MaxValueValidator
5
from django.db import models
6
from django.utils import timezone
7

igor lestin sianipar's avatar
igor lestin sianipar committed
8
9
10
from authentication.models import User

VERIFICATION_STATUS = [
11
12
    ("PENDING", "Diproses"),
    ("APPROVE", "Diterima"),
13
    ("DISAPPROVE", "Ditolak"),
14
    ("REVISION", "Perbaikan"),
igor lestin sianipar's avatar
igor lestin sianipar committed
15
]
Samuel Dimas's avatar
Samuel Dimas committed
16

17

Samuel Dimas's avatar
Samuel Dimas committed
18
# Create your models here.
19
20


Saul Andre's avatar
Saul Andre committed
21
22
def getRandomColor():
    color = "%06x" % random.randint(0, 0xFFFFFF)
23
24
    return color

Saul Andre's avatar
Saul Andre committed
25
26
27

class Category(models.Model):
    name = models.CharField(max_length=20)
28
29
    description = models.TextField(blank=False, default="")
    archived = models.BooleanField(default=False, blank=False)
30
    archived_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
Saul Andre's avatar
Saul Andre committed
31
32
33
34

    def __str__(self):
        return self.name

35

36
class Materi(models.Model):
igor lestin sianipar's avatar
igor lestin sianipar committed
37
38
    cover = models.ImageField()
    content = models.FileField()
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
39
40
    title = models.CharField(max_length=50, default='Judul')
    author = models.CharField(max_length=30, default='Penyusun')
igor lestin sianipar's avatar
igor lestin sianipar committed
41
    uploader = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
42
    publisher = models.CharField(max_length=30, default="Penerbit")
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
43
    pages = models.IntegerField(default=0)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
44
    descriptions = models.TextField(default="Deskripsi")
45
46
    status = models.CharField(
        max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
Saul Andre's avatar
Saul Andre committed
47
    categories = models.ManyToManyField(Category)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    date_created = models.DateTimeField(default=timezone.now)
    date_modified = models.DateTimeField(auto_now=True)

    @property
    def is_published(self):
        published = False
        if self.verificationreport_set.exists():
            report = self.verificationreport_set.latest('timestamp')
            published = True if report.status == 'Diterima' else False
        return published

    @property
    def published_date(self):
        published_date = None
        if self.verificationreport_set.exists():
            report = self.verificationreport_set.latest('timestamp')
            if report.status == 'Diterima':
                published_date = report.timestamp
        return published_date
67

68
69

class Comment(models.Model):
70
71
    username = models.CharField(max_length=100)
    profile = models.CharField(max_length=100, default=getRandomColor)
72
    comment = models.CharField(max_length=240, default="comments")
73
    materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
74
75
    user = models.ForeignKey(
        User, on_delete=models.SET_NULL, blank=True, null=True)
76
    timestamp = models.DateTimeField(default=timezone.now)
77
78

    def __str__(self):
79
        return self.username
80
81
82
83
84
85
86


class Like(models.Model):
    materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
    timestamp = models.DateTimeField(default=timezone.now)
    session_id = models.CharField(max_length=32, blank=False)

87
88
89
90
class ReqMaterial(models.Model):
    title = models.CharField(max_length=100)
    timestamp = models.DateTimeField(default=timezone.now)

91
92
93
94
95
96
97
98
99
100

class ViewStatistics(models.Model):
    materi = models.ForeignKey(
        Materi, models.SET_NULL, null=True, related_name="baca")
    timestamp = models.DateTimeField(default=timezone.now)


class DownloadStatistics(models.Model):
    materi = models.ForeignKey(
        Materi, models.SET_NULL, null=True, related_name="unduh")
101
102
    downloader = models.ForeignKey(
        User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    timestamp = models.DateTimeField(default=timezone.now)


class DummyLike(models.Model):
    item = models.ForeignKey(Like, on_delete=models.CASCADE)


class DummyViewStatistics(models.Model):
    item = models.ForeignKey(ViewStatistics, on_delete=models.CASCADE)


class DummyDownloadStatistics(models.Model):
    item = models.ForeignKey(DownloadStatistics, on_delete=models.CASCADE)


class DummyComment(models.Model):
    item = models.ForeignKey(Comment, on_delete=models.CASCADE)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138


class Rating(models.Model):
    materi = models.ForeignKey(Materi, models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
    timestamp = models.DateTimeField(default=timezone.now)
    score = models.IntegerField()

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if 1 <= self.score <= 5:
            super().save(force_insert, force_update, using, update_fields)
        else:
            raise ValidationError("Rating score must be integer between 1-5")

    def __str__(self):
        return "Material:{} | User:{} | Rating:{}".format(self.materi.title, self.user.name, self.score)

    class Meta:
        unique_together = ["materi", "user"]
139
140
141
142
143
144
145
146
147
148
149
150


class RatingContributor(models.Model):
    timestamp = models.DateTimeField(auto_now=True)
    score = models.PositiveIntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if 1 <= self.score <= 5:
            super().save(force_insert, force_update, using, update_fields)
        else:
            raise ValidationError("Rating score must be integer between 1-5")