Add required 'os' import to models.py
- Author
- Maarten 'Vngngdn' Vangeneugden
- Date
- July 10, 2017, 7:11 p.m.
- Hash
- d428c4062de85680be9ba2e546802cee1c77f59b
- Parent
- adc32862daeb330844ec838a7b5477350a4848e2
- Modified file
- models.py
models.py ¶
1 addition and 0 deletions.
View changes Hide changes
1 |
1 |
from django.db import models |
2 |
2 |
import datetime |
3 |
3 |
|
+ |
4 |
|
4 |
5 |
def post_title_directory(instance, filename): |
5 |
6 |
""" Files will be uploaded to MEDIA_ROOT/blog/<year of publishing>/<blog |
6 |
7 |
title> |
7 |
8 |
The blog title is determined by the text before the first period (".") in |
8 |
9 |
the filename. So if the file has the name "Trains are bæ.en.md", the file |
9 |
10 |
will be stored in "blog/<this year>/Trains are bæ". Name your files |
10 |
11 |
properly! |
11 |
12 |
It should also be noted that all files are stored in the same folder if they |
12 |
13 |
belong to the same blogpost, regardless of language. The titles that are |
13 |
14 |
displayed to the user however, should be the titles of the files themselves, |
14 |
15 |
which should be in the native language. So if a blog post is titled |
15 |
16 |
"Universities of Belgium", its Dutch counterpart should be titled |
16 |
17 |
"Universiteiten van België", so the correct title can be derived from the |
17 |
18 |
filename. |
18 |
19 |
|
19 |
20 |
Recommended way to name the uploaded file: "<name of blog post in language |
20 |
21 |
it's written>.md". This removes the maximum amount of redundancy (e.g. the |
21 |
22 |
language of the file can be derived from the title, no ".fr.md" or something |
22 |
23 |
like that necessary), and can directly be used for the end user (the title |
23 |
24 |
is what should be displayed). |
24 |
25 |
""" |
25 |
26 |
english_file_name = os.path.basename(instance.file_en.name) # TODO: Test if this returns the file name! |
26 |
27 |
english_title = english_file_name.partition(".")[0] |
27 |
28 |
year = datetime.date.today().year |
28 |
29 |
|
29 |
30 |
return "blog/{0}/{1}".format(year, english_title) |
30 |
31 |
|
31 |
32 |
class Post(models.Model): |
32 |
33 |
""" Represents a blog post. The title of the blog post is determnined by the name |
33 |
34 |
of the files. |
34 |
35 |
A blog post can be in 5 different languages: German, Spanish, English, French, |
35 |
36 |
and Dutch. For all these languages, a seperate field exists. Thus, a |
36 |
37 |
translated blog post has a seperate file for each translation, and is |
37 |
38 |
seperated from Django's internationalization/localization system. |
38 |
39 |
Only the English field is mandatory. The others may contain a value if a |
39 |
40 |
translated version exists, which will be displayed accordingly. |
40 |
41 |
""" |
41 |
42 |
published = models.DateTimeField(auto_now_add=True) |
42 |
43 |
file_de = models.FileField(upload_to=post_title_directory, unique=True, blank=True) |
43 |
44 |
file_es = models.FileField(upload_to=post_title_directory, unique=True, blank=True) |
44 |
45 |
file_en = models.FileField(upload_to=post_title_directory, unique=True, blank=False) |
45 |
46 |
file_fr = models.FileField(upload_to=post_title_directory, unique=True, blank=True) |
46 |
47 |
file_nl = models.FileField(upload_to=post_title_directory, unique=True, blank=True) |
47 |
48 |
# Please note that there's no need to create any indexes, because unique |
48 |
49 |
# fields constitute an index already. |
49 |
50 |
|
50 |
51 |
def __str__(self): |
51 |
52 |
return os.path.basename(self.file_en.name).partition(".")[0] |
52 |
53 |