Fakultas Ilmu Komputer UI

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

3
from django.core.exceptions import ValidationError
4
from django.db import models
5
from django.utils import timezone
6

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

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

16

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


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

Saul Andre's avatar
Saul Andre committed
24
25
26

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

    def __str__(self):
        return self.name

34

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

67
68

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

    def __str__(self):
78
        return self.username
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112


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)


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")
    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)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131


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"]