summaryrefslogtreecommitdiff
path: root/fossee_manim/models.py
blob: 21b2a3ce8919e540bc054830e2c635ab13fef0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import RegexValidator
from django.conf import settings
from django.utils import timezone
from django.core.files import File
from taggit.managers import TaggableManager
from simple_history.models import HistoricalRecords
from os import path, sep
import tempfile
import subprocess

position_choices = (
    ("contributor", "Contributor"),
    ("reviewer", "Reviewer")
)

department_choices = (
    ("computer", "Computers"),
    ("mathematics", "Mathematics"),
    ("physics", "Physics"),
    ("information technology", "Information Technology"),
    ("civil", "Civil"),
    ("electrical", "Electrical"),
    ("mechanical", "Mechanical"),
    ("chemical", "Chemical"),
    ("aerospace", "Aerospace"),
    ("biosciences and bioengineering", "Biosciences and  BioEngineering"),
    ("electronics", "Electronics"),
    ("energy science and engineering", "Energy Science and Engineering"),
    ("others", "Others")
)

title = (
    ("Professor", "Prof."),
    ("Doctor", "Dr."),
    ("Shriman", "Shri"),
    ("Shrimati", "Smt"),
    ("Kumari", "Ku"),
    ("Mr", "Mr."),
    ("Mrs", "Mrs."),
    ("Miss", "Ms."),
)

source = (
    ("FOSSEE website", "FOSSEE website"),
    ("Google", "Google"),
    ("Social Media", "Social Media"),
    ("From other College", "From other College"),
    ("others", "Others")
)

states = (
    ("IN-AP", "Andhra Pradesh"),
    ("IN-AR", "Arunachal Pradesh"),
    ("IN-AS", "Assam"),
    ("IN-BR", "Bihar"),
    ("IN-CT", "Chhattisgarh"),
    ("IN-GA", "Goa"),
    ("IN-GJ", "Gujarat"),
    ("IN-HR", "Haryana"),
    ("IN-HP", "Himachal Pradesh"),
    ("IN-JK", "Jammu and Kashmir"),
    ("IN-JH", "Jharkhand"),
    ("IN-KA", "Karnataka"),
    ("IN-KL", "Kerala"),
    ("IN-MP", "Madhya Pradesh"),
    ("IN-MH", "Maharashtra"),
    ("IN-MN", "Manipur"),
    ("IN-ML", "Meghalaya"),
    ("IN-MZ", "Mizoram"),
    ("IN-NL", "Nagaland"),
    ("IN-OR", "Odisha"),
    ("IN-PB", "Punjab"),
    ("IN-RJ", "Rajasthan"),
    ("IN-SK", "Sikkim"),
    ("IN-TN", "Tamil Nadu"),
    ("IN-TG", "Telangana"),
    ("IN-TR", "Tripura"),
    ("IN-UT", "Uttarakhand"),
    ("IN-UP", "Uttar Pradesh"),
    ("IN-WB", "West Bengal"),
    ("IN-AN", "Andaman and Nicobar Islands"),
    ("IN-CH", "Chandigarh"),
    ("IN-DN", "Dadra and Nagar Haveli"),
    ("IN-DD", "Daman and Diu"),
    ("IN-DL", "Delhi"),
    ("IN-LD", "Lakshadweep"),
    ("IN-PY", "Puducherry")
)

status = (
    ("pending", "Pending Acceptance"),
    ("rejected", "Rejected"),
    ("changes", "Changes Required"),
    ("released", "Released")
)


def has_profile(user):
    """ check if user has profile """
    return True if hasattr(user, 'profile') else False


def attachments(instance, filename):
    return path.join(instance.animation.category.name,
                     instance.animation.title,
                     str(instance.animation.id), filename)


def validate_file_extension(value):
    import os
    from django.core.exceptions import ValidationError
    ext = os.path.splitext(value.name)[1]  # [0] returns path+filename
    valid_extensions = ['.mp4']
    if not ext.lower() in valid_extensions:
        raise ValidationError(u'Unsupported file extension.')


class Profile(models.Model):
    """Profile for users(instructors and coordinators)"""

    user = models.OneToOneField(User)
    title = models.CharField(max_length=32, blank=True, choices=title)
    institute = models.CharField(max_length=150, blank=True)
    department = models.CharField(max_length=150, choices=department_choices)
    phone_number = models.CharField(
        max_length=10,
        validators=[RegexValidator(
                    regex=r'^.{10}$', message=(
                        "Phone number must be entered \
                                in the format: '9999999999'.\
                                Up to 10 digits allowed.")
                    )], null=False)
    position = models.CharField(max_length=32, choices=position_choices,
                                default='contributor')
    how_did_you_hear_about_us = models.CharField(max_length=255, blank=True,
                                                 choices=source)
    location = models.CharField(max_length=255, blank=True,
                                help_text="Place/City")
    state = models.CharField(max_length=255, choices=states, default="IN-MH")
    pincode = models.CharField(max_length=6, blank=True,
                               validators=[RegexValidator(
                                   regex=r'^.{6}$', message=(
                                       "Please enter valid PINCODE"
                                   )
                               )])
    is_email_verified = models.BooleanField(default=False)
    activation_key = models.CharField(max_length=255, blank=True, null=True)
    key_expiry_time = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return u"id: {0}| {1} {2} | {3} ".format(
            self.user.id,
            self.user.first_name,
            self.user.last_name,
            self.user.email
        )


class Category(models.Model):
    name = models.CharField(max_length=255, unique=True)
    created = models.DateTimeField(default=timezone.now)
    description = models.TextField()

    def __str__(self):
        return u"{0}".format(self.name)


# test
class Example(models.Model):
    name = models.CharField(max_length=20)
    tags = TaggableManager()


class Animation(models.Model):
    title = models.CharField(max_length=255)
    contributor = models.ForeignKey(User, on_delete=models.CASCADE)
    reviewer = models.ForeignKey(
        User,
        null=True,
        on_delete=models.CASCADE,
        related_name="%(app_label)s_%(class)s_related")
    outline = models.TextField()
    status = models.CharField(max_length=255, choices=status)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    subcategory = models.CharField(max_length=255, blank=True)
    created = models.DateTimeField(default=timezone.now)
    tags = TaggableManager()
    history = HistoricalRecords()

    def __str__(self):
        return u"{0} | {1}".format(self.title, self.status)


class Comment(models.Model):
    comment = models.TextField()
    commentor = models.ForeignKey(User, on_delete=models.CASCADE)
    animation = models.ForeignKey(Animation, on_delete=models.CASCADE)
    created_date = models.DateTimeField(default=timezone.now)
    animation_status = models.CharField(max_length=255)

    def __str__(self):
        return u"{1} | {0}".format(
            self.created_date,
            self.commentor
        )


class AnimationStats(models.Model):
    animation = models.ForeignKey(Animation, on_delete=models.CASCADE)
    views = models.PositiveIntegerField(default=0)
    like = models.PositiveIntegerField(default=0)
    dislike = models.PositiveIntegerField(default=0)
    thumbnail = models.ImageField(null=True, blank=True, upload_to=attachments)
    video_path = models.FileField(
        null=True,
        blank=True,
        upload_to=attachments,
        validators=[validate_file_extension])

    def _create_thumbnail(self):
        video_path = self.video_path.path
        img_output = path.join(
            tempfile.mkdtemp(), "{0}.jpg".format(self.animation.title)
        )
        file_name = "{0}.jpg".format(self.animation.title)
        subprocess.call(['ffmpeg', '-i', video_path, '-ss', '00:00:02.000',
                         '-vframes', '1', img_output])
        if path.exists(img_output):
            que_file = open(img_output, 'rb')
            # Converting to Python file object with
            # some Django-specific additions
            django_file = File(que_file)
            self.thumbnail.save(file_name, django_file, save=True)

    def _create_ogv(self):
        video_input = self.video_path.path
        vid_output = path.join(
            tempfile.mkdtemp(), "{0}.ogv".format(self.animation.title)
        )
        file_name = "{0}.ogv".format(self.animation.title)
        subprocess.call(['ffmpeg', '-i', video_input, '-r', '24', vid_output])
        if path.exists(vid_output):
            que_file = open(vid_output, 'rb')
            # Converting to Python file object with
            # some Django-specific additions
            django_file = File(que_file)
            self.video_path.save(file_name, django_file, save=True)