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