-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
125 lines (99 loc) · 3.79 KB
/
models.py
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
import audio_metadata
from audio_metadata.utils import humanize_duration
from audio_validator.validator import AudioValidator
from constrainedfilefield.fields import ConstrainedFileField, ConstrainedImageField
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.db import models
from django.urls import reverse
from tbm_utils import humanize_filesize
from user.models import User
from utility.generator import hex_uuid, user_directory_path
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ManyToManyField("PostCategory", blank=True)
caption = models.CharField(max_length=2000)
slug = models.SlugField(default=hex_uuid, editable=False)
image = models.ForeignKey("Image", on_delete=models.CASCADE, null=True, blank=True)
audio = models.ForeignKey("Audio", on_delete=models.CASCADE, null=True, blank=True)
video = models.ForeignKey("Video", on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return str(self.caption[:9])
def get_absolute_url(self):
return reverse(
"show_post",
kwargs={"slug": self.slug},
)
class PostLike(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
post = models.ForeignKey("Post", on_delete=models.CASCADE)
def __str__(self):
return f"{self.user} liked Your Post"
class PostCategory(models.Model):
title = models.CharField(max_length=30)
url_title = models.SlugField(max_length=30)
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
def __str__(self):
return self.title
class Image(models.Model):
file = ConstrainedImageField(
upload_to=user_directory_path,
content_types=["image/png", "image/jpeg", "image/heic", "image/heif"],
blank=True,
)
class Video(models.Model):
file = ConstrainedFileField(
upload_to=user_directory_path, content_types=["video/mp4"], blank=True
)
class Audio(models.Model):
file = models.FileField(
upload_to=user_directory_path,
blank=True,
validators=[AudioValidator("mp3")],
)
name = models.CharField(max_length=100, null=True, blank=True)
artist = models.CharField(max_length=100, null=True, blank=True)
album = models.CharField(max_length=100, null=True, blank=True)
size = models.CharField(max_length=30, null=True, blank=True)
date = models.CharField(max_length=30, null=True, blank=True)
duration = models.CharField(max_length=100, null=True, blank=True)
def save(self, *args, **kwargs):
file = self.file.file
file_path = TemporaryUploadedFile.temporary_file_path(file)
metadata = audio_metadata.load(file_path)
try:
name = metadata.tags.title[0]
self.name = name
except AttributeError:
pass
try:
artist = metadata.tags.artist[0]
self.artist = artist
except AttributeError:
pass
try:
album = metadata.tags.album[0]
self.album = album
except AttributeError:
pass
try:
size = metadata.filesize
self.size = humanize_filesize(size)
except AttributeError:
pass
try:
date = metadata.tags.date[0]
self.date = date
except AttributeError:
pass
try:
duration = metadata.streaminfo["duration"]
self.duration = humanize_duration(duration)
except AttributeError:
pass
super(Audio, self).save(*args, **kwargs)
def __str__(self):
return str(self.name)