models.py

1
from django.db import models
2
3
# Cards: Describes a card that will be loaded in the main page, providing links to useful information and other apps.
4
class Card(models.Model):
5
    title = models.CharField(max_length=20) # 20 should sustain well.
6
    description = models.CharField(max_length=256) # 256 as well.
7
    link1 = models.CharField('First link', blank=True, max_length=50) # I could make this a URLField, but that doesn't accept cases like "/ITdays".
8
    linkName1 = models.CharField('Name of first link', blank=True, max_length=20)
9
    link2 = models.CharField('Second link', blank=True, max_length=50) # This may be empty.
10
    linkName2 = models.CharField('Name of second link', blank=True, max_length=20) # This too.
11
    image = models.ImageField(upload_to='about/images/cards', blank=True) # Sometimes the cards should get an image.
12
    
13
    def __str__(self):
14
        return self.title
15
16
# What you're about to see, is a very rare case of where multi-level inheritance
17
# is actually done correctly. Notes should be taken. (Also worth mentioning
18
# Django is so well built it actually makes this possible.)
19
20
# First, it should be said all objects have an extremely similar structure. That
21
# doesn't qualify them yet for multilevel inheritance, but it's a good sign.
22
23
# Also, note how Card, even though it has both title and description, is NOT
24
# inherited from this base class. This is deliberate; Card is not an object with
25
# a comparable behavior, and thus, differs too much as an object to derive from
26
# this one.
27
class Subject(models.Model):
28
    """ This class should not be instantiated. Look at it as being an abstract
29
    class. """
30
31
    title = models.CharField(max_length=50)
32
    description = models.TextField()
33
34
    def __str__(self):
35
        return self.title
36
37
    """ This Meta is important, because it tells Django that Subject is
38
    'abstract', in that it just has some fields that should be passed to its
39
    children. """
40
    class Meta:
41
        abstract = True
42
# Now you may say: Well, some subjects have links, why not make another object,
43
# and use it for just that? Well, because that would imply multiple inheritance,
44
# which is not necessary here*, and (if that second object would inherit Model
45
# again) cause a diamond inheritance.
46
# *Most of the time people use inheritance because it's convenient. While that
47
# may be a reason, it shouldn't be the only reason for it. Inheritance is not
48
# made to make programming convenient, that's what programming languages are
49
# for. Inheritance is to inherit behavior from another object. And since that's
50
# my goal here, inheritance is a passable programming decision.
51
# Also, I'm referring here to OOP inheritance, which means "inheriting state AND
52
# behavior". Languages that implement other paradigms can also have inheritance.
53
# Clojure, for example, is not OOP, but fully functional, and its inheritance
54
# capabilities are WAY better.
55
56
class Principle(Subject):
57
    pass
58
class AboutMe(Subject):
59
    pass
60
class Program(Subject):
61
    link = models.URLField()
62
class Stack(Subject):
63
    link = models.URLField()
64
class Hack(Subject):
65
    pass
66
class SecurityMeasure(Subject):
67
    pass
68
class Goal(Subject):
69
    pass
70
71
# A last pick on inheritance (I feel so obliged to defend my decision because I
72
# decided to use inheritance, apologies if you're sick of taking notes):
73
# Note that I could've easily solved this problem without inheritance. Some
74
# objects even lack any other variable or function of some sort.
75
# But also, note how easy it now becomes to edit all those objects at once: If I
76
# want to increase the length (And yes, that length should be the same, I like
77
# that), I can now do that all at once. THIS is when inheritance can be a good
78
# thing: To increase code maintainability for a lot of different objects.
79
80
class Quote(models.Model):
81
    text = models.TextField()
82
    author = models.CharField(max_length=256)
83
    link = models.URLField(blank=True)
84
85
# Really, these max_lengths... Sometimes I look at my code and just think what an arbitrary asshole I can be to my database.
86