Fakultas Ilmu Komputer UI

models.py 5.66 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"),
15
    ("BLOCKED", "Diblokir"),
igor lestin sianipar's avatar
igor lestin sianipar committed
16
]
Samuel Dimas's avatar
Samuel Dimas committed
17

18

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


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

Saul Andre's avatar
Saul Andre committed
26
27
28

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

    def __str__(self):
        return self.name

36

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

69
70
71
72
    @property
    def like_count(self):
        count = Like.objects.filter(materi=self).count()
        return count
73
74

class Comment(models.Model):
75
76
    username = models.CharField(max_length=100)
    profile = models.CharField(max_length=100, default=getRandomColor)
77
    comment = models.CharField(max_length=240, default="comments")
78
    materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
79
80
    user = models.ForeignKey(
        User, on_delete=models.SET_NULL, blank=True, null=True)
81
    timestamp = models.DateTimeField(default=timezone.now)
82
83

    def __str__(self):
84
        return self.username
85
86
87
88
89
90
91


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)

92
93
94
95
class ReqMaterial(models.Model):
    title = models.CharField(max_length=100)
    timestamp = models.DateTimeField(default=timezone.now)

96
97
98
99
100
101
102
103
104
105

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")
106
107
    downloader = models.ForeignKey(
        User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    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)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143


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"]
144
145
146
147
148
149
150
151
152
153
154


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:
155
156
157
158
159
160
161
162
            raise ValidationError("Rating score must be integer between 1-5")

class LaporanMateri(models.Model):
    materi = models.ForeignKey(Materi, on_delete=models.CASCADE, max_length=120)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    laporan = models.TextField(validators=[MinValueValidator(30), MaxValueValidator(120)], default="")
    timestamp = models.DateTimeField(default=timezone.now)
    is_rejected = models.BooleanField(default=False)