blog

Fix typo in models.py

Author
Maarten 'Vngngdn' Vangeneugden
Date
April 9, 2018, 5:21 p.m.
Hash
28db6bb12bb6f479fd13b18afec8dfd26dba051a
Parent
692768670901c84f12ede4c3ffc2cf188704630e
Modified file
models.py

models.py

1 addition and 1 deletion.

View changes Hide changes
1
1
from django.utils import translation
2
2
from django.template.defaultfilters import slugify
3
3
from django.db import models
4
4
import datetime
5
5
import os
6
6
7
7
def post_title_directory(instance, filename):
8
8
    """ Files will be uploaded to MEDIA_ROOT/blog/<year of publishing>/<blog
9
9
    title>
10
10
    The blog title is determined by the text before the first period (".") in
11
11
    the filename. So if the file has the name "Trains are bæ.en.md", the file
12
12
    will be stored in "blog/<this year>/Trains are bæ". Name your files
13
13
    properly!
14
14
    It should also be noted that all files are stored in the same folder if they
15
15
    belong to the same blogpost, regardless of language. The titles that are
16
16
    displayed to the user however, should be the titles of the files themselves,
17
17
    which should be in the native language. So if a blog post is titled
18
18
    "Universities of Belgium", its Dutch counterpart should be titled
19
19
    "Universiteiten van België", so the correct title can be derived from the
20
20
    filename.
21
21
22
22
    Recommended way to name the uploaded file: "<name of blog post in language
23
23
    it's written>.org". This removes the maximum amount of redundancy (e.g. the
24
24
    language of the file can be derived from the title, no ".fr.org" or something
25
25
    like that necessary), and can directly be used for the end user (the title
26
26
    is what should be displayed).
27
27
    """
28
28
    english_file_name = os.path.basename(instance.english_file.name) # TODO: Test if this returns the file name!
29
29
    english_title = english_file_name.rpartition(".")[0]
30
30
    year = datetime.date.today().year
31
31
32
32
    return "blog/{0}/{1}/{2}".format(year, english_title, filename)
33
33
34
34
class Post(models.Model):
35
35
    """ Represents a blog post. The title of the blog post is determnined by the name
36
36
    of the files.
37
37
    A blog post can be in 5 different languages: German, Spanish, English, French,
38
38
    and Dutch. For all these languages, a seperate field exists. Thus, a
39
39
    translated blog post has a seperate file for each translation, and is
40
40
    seperated from Django's internationalization/localization system.
41
41
    Only the English field is mandatory. The others may contain a value if a
42
42
    translated version exists, which will be displayed accordingly.
43
43
    """
44
44
    published = models.DateTimeField(auto_now_add=True)
45
45
    english_file = models.FileField(upload_to=post_title_directory, unique=True, blank=False)
46
46
    dutch_file = models.FileField(upload_to=post_title_directory, blank=True)
47
47
    french_file = models.FileField(upload_to=post_title_directory, blank=True)
48
48
    german_file = models.FileField(upload_to=post_title_directory, blank=True)
49
49
    spanish_file = models.FileField(upload_to=post_title_directory, blank=True)
50
50
    # Only the English file can be unique, because apparantly, there can't be
51
51
    # two blank fields in a unique column. Okay then.
52
52
53
53
    def __str__(self):
54
54
        return self.slug("en")
55
55
56
56
    def slug(self, language_code=translation.get_language()):
57
57
        """ Returns a slug of the requested language, or None if no version exists in that language. """
58
58
        possibilities = {
59
59
            "en" : self.english_file,
60
60
            "de" : self.german_file,
61
61
            "nl" : self.dutch_file,
62
62
            "fr" : self.french_file,
63
63
            "es" : self.spanish_file,
64
64
            }
65
65
        if possibilities[language_code]:
66
66
            return slugify(os.path.basename(possibilities[language_code].name).rpartition(".")[0])
67
67
        else:
68
68
            return None
69
69
70
70
class Comment(models.Model):
71
71
    """ Represents a comment on a blog post.
72
72
73
73
    Comments are not linked to an account or anything, I'm trusting the
74
74
    commenter that he is honest with his credentials. That being said:
75
75
    XXX: Remember to put up a notification that comments are not checked for
76
76
    identity, and, unless verified by a trustworthy source, cannot be seen as
77
77
    being an actual statement from the commenter.
78
78
    Comments are linked to a blogpost, and are not filtered by language. (So a
79
79
    comment made by someone reading the article in Dutch, that's written in
80
80
    Dutch, will show up (unedited) for somebody whom's reading the Spanish
81
81
    version.
82
82
    XXX: Remember to notify (tiny footnote or something) that comments showing
83
83
    up in a foreign language is by design, and not a bug.
84
84
    """
85
85
    date = models.DateTimeField(auto_now_add=True)
86
86
    name = models.CharField(max_length=64)
87
87
    text = models.TextField(max_length=1000)  # Should be more than enough.
88
88
    post = models.ForeignKey(
89
89
        Post,
90
90
        on_delete=Models.CASCADE,
91
-
        null=False,
+
91
        null=False,
92
92
        )
93
93
    class meta:
94
94
        ordering = ['date']  # When printed, prints the oldest comment first.
95
95