blog

Fix function that determines upload directory

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 10, 2017, 7:21 p.m.
Hash
473c1d85075160112d122648722cea697d4af7d4
Parent
d428c4062de85680be9ba2e546802cee1c77f59b
Modified file
models.py

models.py

1 addition and 1 deletion.

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
27
    english_title = english_file_name.partition(".")[0] 
28
28
    year = datetime.date.today().year
29
29
30
30
    return "blog/{0}/{1}".format(year, english_title)
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
44
    file_es = models.FileField(upload_to=post_title_directory, unique=True, blank=True)
45
45
    file_en = models.FileField(upload_to=post_title_directory, unique=True, blank=False)
46
46
    file_fr = models.FileField(upload_to=post_title_directory, unique=True, blank=True)
47
47
    file_nl = models.FileField(upload_to=post_title_directory, unique=True, blank=True)
48
48
    # Please note that there's no need to create any indexes, because unique
49
49
    # fields constitute an index already.
50
50
51
51
    def __str__(self):
52
52
        return os.path.basename(self.file_en.name).partition(".")[0]
53
53