Fakultas Ilmu Komputer UI

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

from django.db import models
4
from django.utils import timezone
5

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

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

# Create your models here.
16
17


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

Saul Andre's avatar
Saul Andre committed
22
23
24

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

    def __str__(self):
        return self.name

32

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

65
66

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

    def __str__(self):
76
        return self.username
77
78
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


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)