blog

Various changes on the Post model in models.py

The function that determines where to store all files is now behaving correctly: It stores all files of one post in the same directory, retaining their own name in that directory.
In addition, only the English file field now has to be unique. I just found out that blank fields are also seen as a normal value, and thus two blank fields weren't allowed in the same column. This has to be changed back when this is changed in Django.
The names of the fields have been made more expressed, full names for the languages are now used instead of locale abbreviations. To top it off, the order has been changed as well, with English on top, and then alphabetically, which just so happens to be in the same order of likeliness that I'll add a translation in that language.

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 10, 2017, 5:34 p.m.
Hash
7c717d97cecae78d3ce135554725a3ab4522df6f
Parent
be65264432fc725ee6202ba2ada09b48660d9b1c
Modified file
models.py

models.py

9 additions and 9 deletions.

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