home

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 Product(models.Model):
57
    name = models.CharField(max_length=128)
58
    estimated_price = models.PositiveSmallIntegerField(null=False)
59
    picture = models.URLField(blank=True)
60
    url = models.URLField(blank=True)
61
    isbn = models.CharField(max_length=13, blank=True)
62
    jonathan = models.BooleanField()
63
    maarten = models.BooleanField()
64
    extra_info = models.TextField(blank=True)
65
66
    purchaser = models.GenericIPAddressField(blank=True,null=True)
67
    def __str__(self):
68
        return self.name
69
70
71
class Principle(Subject):
72
    pass
73
class AboutMe(Subject):
74
    pass
75
class Program(Subject):
76
    link = models.URLField()
77
class Stack(Subject):
78
    link = models.URLField()
79
class Hack(Subject):
80
    pass
81
class SecurityMeasure(Subject):
82
    pass
83
class Goal(Subject):
84
    pass
85
86
# A last pick on inheritance (I feel so obliged to defend my decision because I
87
# decided to use inheritance, apologies if you're sick of taking notes):
88
# Note that I could've easily solved this problem without inheritance. Some
89
# objects even lack any other variable or function of some sort.
90
# But also, note how easy it now becomes to edit all those objects at once: If I
91
# want to increase the length (And yes, that length should be the same, I like
92
# that), I can now do that all at once. THIS is when inheritance can be a good
93
# thing: To increase code maintainability for a lot of different objects.
94
95
class Quote(models.Model):
96
    text = models.TextField()
97
    author = models.CharField(max_length=256)
98
    link = models.URLField(blank=True)
99
100
# Really, these max_lengths... Sometimes I look at my code and just think what an arbitrary asshole I can be to my database.
101