Fakultas Ilmu Komputer UI

models.py 3.45 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
25
26
27
28
29

class Category(models.Model):
    name = models.CharField(max_length=20)
    description = models.CharField(max_length=20)

    def __str__(self):
        return self.name

30

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

64
65

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

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


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)