views.py

1
import random
2
from datetime import date
3
from django.utils import timezone
4
from django.shortcuts import get_object_or_404, render # This allows to render the template with the view here. It's pretty cool and important.
5
from django.http import HttpResponseRedirect, HttpResponse # Why?
6
from django.core.urlresolvers import reverse # Why?
7
from django.utils.translation import ugettext as _
8
from .models import *
9
10
# First, I list some standard variables that are common for most of the sites of this app.
11
12
def get_age():
13
    """Returns my current age."""
14
    today = date.today()
15
    birthday = date(1996, 8, 28)
16
    age = today - birthday
17
    years = str(int(age.days / 365))
18
    return years
19
20
def footer_description():
21
    years = get_age()
22
23
    """
24
    Grandpa easter egg: The Dutch version of the most ingenius algorithm I ever wrote:
25
    vandaag = datum.vandaag()
26
    geboortedatum = datum(28-08-1996)
27
    leeftijd = vandaag - geboortedatum
28
    jaren = leeftijd.jaren()
29
    This will help me explain what programming has brought me. =3
30
    """
31
    return _("Main pages of Maarten's website, a %(years)s year old Belgian programmer. Also an undergraduate informatics student @ UHasselt.") % {'years': years}
32
33
def footer_links():
34
    footer_links = [
35
            ["Contact", "mailto:maarten.vangeneugden@student.uhasselt.be"],
36
            ["UHasselt", "https://www.uhasselt.be"],
37
            ]
38
    return footer_links
39
40
# TODO: Move this stuff to the template module. This is basically a description
41
# of HOW to display data, but the view module is only responsible for WHAT data
42
# to display.
43
def standard_context():
44
    context = {
45
            'materialDesign_color': "blue",
46
            'materialDesign_accentColor': "orange",
47
            'navbar_backArrow': True,
48
            'footer_title': _("Home page"),
49
            'footer_description': footer_description(),
50
            'footer_links': footer_links(),
51
            }
52
    return context
53
54
def get_current_status(dt = None):
55
    """Returns a string specifying my current state (and sometimes location).
56
57
    This function is actually based on my weekly schedule. I'd normally hook it
58
    up to my iCal files, but that doesn't include things like sleeping. Not to
59
    mention my university has a hard time following standards like "Put the
60
    location in the location field, not in the title of the appointment". I
61
    figured a simple function would do the job just as well.
62
    
63
    Keyword arguments:
64
    dt -- The datetime object of the day to check (defaults to current local time)
65
    """
66
67
    """ Readable standard schedule of my week (00:00 - 08:00 is always sleep):
68
    Monday:
69
        09:00 - 11:30: Mathematics @ C104
70
        13:00 - 15:00: Informatics Management @ A101
71
    Tuesday:
72
        10:00 - 11:30: Computer Graphics @ C105
73
        13:30 - 15:30: AI @ H1
74
    Wednesday: Free!
75
    Thursday:
76
        10:00 - 12:00: Software Engineering @ A7 (Temporary; prone to change!)
77
        13:00 - 15:00: Beleidsinformatica @ <variable>
78
    Friday - Sunday: Free!
79
    On free days, the planning is usually this:
80
    14:00 - 19:00: Studying / general work
81
    20:00 - 23:59: Leisure =D
82
83
    """
84
    
85
    # I fixed my server, so for now, it's this:
86
    
87
    if dt == None:
88
        timezone.activate("Europe/Brussels")
89
        dt = timezone.localtime(timezone.now())
90
91
    day = dt.weekday()
92
    hour = dt.time().hour
93
    #print(hour)
94
    minute = dt.time().minute
95
96
    """ Note on usage of the range() function:
97
    range(x, y) returns a list, beginning from x, but excluding y. So if a
98
    course runs from 13:00 to 15:00, then y should still be 15. Why? Because
99
    that makes it so that 14:59 is included, but 15:00 is not. if y would be 16,
100
    then 15:30 would also be included.
101
    """
102
103
    if day == 4:  # Friday... <3
104
        if hour >= 17:
105
            return _("❤ Spending time with someone very special... ❤")
106
    if day == 5:
107
        return _("I'm in love. Best thing that ever happened to me... ❤")
108
109
110
    if day == 0:  # Monday
111
        if hour in [9, 10] or (hour == 11 and minute <= 30):
112
            return _("Studying Mathematics @ C104. See you around noon!")
113
        if hour in range(13, 15):
114
            return _("Following college: Informatics Management @ A101.")
115
    if day == 1:  # Tuesday
116
        if hour == 10 or (hour == 11 and minute <= 30):
117
            return _("Studying Computer Graphics @ C105. Back in C24 at noon!")
118
        if (hour == 13 and minute >= 30) or \
119
           (hour == 14) or \
120
           (hour == 15 and minute < 30):
121
            return _("Trying to understand Artificial Intelligence @ H1...")
122
    if day == 3:  # Thursday
123
        if hour in range(10, 12):
124
            return _("Engineering the Software @ A7. Tough nut to crack...")
125
        if hour in range(13, 15):
126
            return _("Fighting boredom of Informatics Management. Looking \
127
            forward to the weekend!")
128
129
    # General answers that span over multiple days
130
    # They may overlap with previous tests, but that means they fill in gaps
131
    # like study time
132
    if hour <= 8:
133
        return _("I'm asleep. See you tomorrow! =D")
134
    if day in [0, 1, 3]:
135
        if hour == 12:
136
            return _("Having lunch in C24. I'm available if you need me!")
137
        elif hour in range(9, 18):
138
            return _("At UHasselt! Studying in C24. Come by for a chat, or email me!")
139
    if day in [2, 4, 5, 6]:
140
        if hour in range(13, 19):
141
            return _("Studying from home, or other work. Perhaps I'll see you in the evening?")
142
        elif hour in range(19, 24):
143
            #return _("Relaxing, and enjoying my free time. Feel free to talk! 😃")
144
            return _("Relaxing, and enjoying my free time. Feel free to talk! 😃")
145
146
    # If nothing's returned by now, return a general response
147
    return _("Probably chilling a bit. Feel free to talk! ❤")
148
149
# Views:
150
151
def index(request):
152
    timezone.activate("Europe/Brussels")
153
    time_string = timezone.localtime(timezone.now()).strftime(" (%H:%M) ")
154
    status = _("Current status/location:") + time_string + get_current_status()
155
    template = "about/index.html"
156
157
    cards = Card.objects.order_by('?') # The '?' indicates the objects must be ordered randomly.
158
    quotes = Quote.objects.all()
159
    randomQuote = random.choice(quotes)
160
161
    largeSizes = []
162
    mediumSizes = []
163
    for i in range(0, len(cards)):
164
        newLargeSizes = [6, 4, 2]
165
        newMediumSizes = [8, 4]
166
        random.shuffle(newLargeSizes)
167
        random.shuffle(newMediumSizes)
168
        largeSizes.extend(newLargeSizes)
169
        mediumSizes.extend(newMediumSizes)
170
171
    contextList = []
172
    i = 0
173
    for card in cards:
174
        contextList.append([card, mediumSizes[i], largeSizes[i]])
175
        i += 1
176
177
    # TODO: Move this stuff to the template module. This is basically a description
178
    # of HOW to display data, but the view module is only responsible for WHAT data
179
    # to display.
180
    context = {
181
            'status': status,
182
            'materialDesign_color': "blue",
183
            'materialDesign_accentColor': "orange",
184
            'navbar_title': _("Maarten's website"),
185
            'navbar_fixed': False,
186
            'parallax_src': "<img style=\"wdith:100vw;\" src=\"/media/about/images/parallax.png\">",
187
            'footer_title': _("Home page"),
188
            'footer_description': footer_description,
189
            'footer_links': footer_links,
190
            'cards': contextList,
191
            'quote': randomQuote,
192
            }
193
194
    return render(request, template, context)
195
196
def myself(request):
197
    template = "about/about.html"
198
199
    context = {
200
            'subject': _("Myself"),
201
            'navbar_title': _("Myself"),
202
            'age': get_age(),
203
            }
204
    context.update(standard_context())
205
    return render(request, template, context)
206