blog

Try to apply fix to naming in models.py

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 10, 2017, 7:08 p.m.
Hash
adc32862daeb330844ec838a7b5477350a4848e2
Parent
dcd54c2df39a90f9ec5c861b4e2bc865e7dd44b6
Modified file
models.py

models.py

5 additions and 3 deletions.

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