home

Fix bugs regarding age calculation

Author
Vngngdn
Date
Feb. 18, 2017, 7:14 p.m.
Hash
054293e0d00490d57e3ed565e92ff27a5e86258d
Parent
dceb96c710ddb17d7e80537c2950454940928dfd
Modified file
views.py

views.py

1 addition and 0 deletions.

View changes Hide changes
1
1
from django.utils import timezone
+
2
from django.utils import timezone
2
3
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.
3
4
from django.http import HttpResponseRedirect, HttpResponse # Why?
4
5
from django.core.urlresolvers import reverse # Why?
5
6
from django.utils.translation import ugettext as _
6
7
from .models import *
7
8
8
9
# First, I list some standard variables that are common for most of the sites of this app.
9
10
10
11
def get_age():
11
12
    """Returns my current age."""
12
13
    today = date.today()
13
14
    birthday = date(1996, 8, 28)
14
15
    age = today - birthday
15
16
    years = str(int(age.days / 365))
16
17
    return years
17
18
18
19
def footer_description():
19
20
    years = get_age()
20
21
21
22
    """
22
23
    Grandpa easter egg: The Dutch version of the most ingenius algorithm I ever wrote:
23
24
    vandaag = datum.vandaag()
24
25
    geboortedatum = datum(28-08-1996)
25
26
    leeftijd = vandaag - geboortedatum
26
27
    jaren = leeftijd.jaren()
27
28
    This will help me explain what programming has brought me. =3
28
29
    """
29
30
30
31
    return "Main pages of Maarten's website, a " + years + " year old Belgian programmer. Also an undergraduate informatics student @ UHasselt."
31
32
32
33
def footer_links():
33
34
    footer_links = [
34
35
            ["Contact", "mailto:maarten.vangeneugden@student.uhasselt.be"],
35
36
            ["UHasselt", "https://www.uhasselt.be"],
36
37
            ]
37
38
    return footer_links
38
39
39
40
# TODO: Move this stuff to the template module. This is basically a description
40
41
# of HOW to display data, but the view module is only responsible for WHAT data
41
42
# to display.
42
43
def standard_context():
43
44
    context = {
44
45
            'materialDesign_color': "blue",
45
46
            'materialDesign_accentColor': "orange",
46
47
            'navbar_backArrow': True,
47
48
            'footer_title': "Home pages",
48
49
            'footer_description': footer_description(),
49
50
            'footer_links': footer_links(),
50
51
            }
51
52
    return context
52
53
53
54
def get_current_status(dt = None):
54
55
    """Returns a string specifying my current state (and sometimes location).
55
56
56
57
    This function is actually based on my weekly schedule. I'd normally hook it
57
58
    up to my iCal files, but that doesn't include things like sleeping. Not to
58
59
    mention my university has a hard time following standards like "Put the
59
60
    location in the location field, not in the title of the appointment". I
60
61
    figured a simple function would do the job just as well.
61
62
    
62
63
    Keyword arguments:
63
64
    dt -- The datetime object of the day to check (defaults to current local time)
64
65
    """
65
66
66
67
    """ Readable standard schedule of my week (00:00 - 08:00 is always sleep):
67
68
    Monday:
68
69
        09:00 - 11:30: Mathematics @ C104
69
70
        13:00 - 15:00: Informatics Management @ A101
70
71
    Tuesday:
71
72
        10:00 - 11:30: Computer Graphics @ C105
72
73
        13:30 - 15:30: AI @ H1
73
74
    Wednesday: Free!
74
75
    Thursday:
75
76
        10:00 - 12:00: Software Engineering @ A7 (Temporary; prone to change!)
76
77
        13:00 - 15:00: Beleidsinformatica @ <variable>
77
78
    Friday - Sunday: Free!
78
79
    On free days, the planning is usually this:
79
80
    14:00 - 19:00: Studying / general work
80
81
    20:00 - 23:59: Leisure =D
81
82
    """
82
83
    
83
84
    if dt == None:
84
85
        timezone.activate("Europe/Brussels")
85
86
        dt = timezone.localtime(timezone.now())
86
87
87
88
    day = dt.weekday()
88
89
    hour = dt.time().hour
89
90
    print(hour)
90
91
    minute = dt.time().minute
91
92
92
93
    """ Note on usage of the range() function:
93
94
    range(x, y) returns a list, beginning from x, but excluding y. So if a
94
95
    course runs from 13:00 to 15:00, then y should still be 15. Why? Because
95
96
    that makes it so that 14:59 is included, but 15:00 is not. if y would be 16,
96
97
    then 15:30 would also be included.
97
98
    """
98
99
99
100
    if day == 0:  # Monday
100
101
        if hour in [9, 10] or (hour == 11 and minute <= 30):
101
102
            return _("Studying Mathematics @ C104. See you around noon!")
102
103
        if hour in range(13, 15):
103
104
            return _("Following college: Informatics Management @ A101.")
104
105
    if day == 1:  # Tuesday
105
106
        if hour == 10 or (hour == 11 and minute <= 30):
106
107
            return _("Studying Computer Graphics @ C105. Back in C24 at noon!")
107
108
        if (hour == 13 and minute >= 30) or \
108
109
           (hour == 14) or \
109
110
           (hour == 15 and minute < 30):
110
111
            return _("Trying to understand Artificial Intelligence @ H1...")
111
112
    if day == 3:  # Thursday
112
113
        if hour in range(10, 12):
113
114
            return _("Engineering the Software @ A7. Tough nut to crack...")
114
115
        if hour in range(13, 15):
115
116
            return _("Fighting boredom of Informatics Management. Looking \
116
117
            forward to the weekend!")
117
118
118
119
    # General answers that span over multiple days
119
120
    # They may overlap with previous tests, but that means they fill in gaps
120
121
    # like study time
121
122
    if hour <= 8:
122
123
        return _("I'm asleep. See you tomorrow! =D")
123
124
    if day in [0, 1, 3]:
124
125
        if hour == 12:
125
126
            return _("Having lunch in C24. I'm available if you need me!")
126
127
        elif hour in range(9, 18):
127
128
            return _("At UHasselt! Studying in C24. Come by for a chat, or email me!")
128
129
    if day in [2, 4, 5, 6]:
129
130
        if hour in range(13, 19):
130
131
            return _("Studying from home, or other work. Perhaps I'll see you in the evening?")
131
132
        elif hour in range(19, 24):
132
133
            return _("Relaxing, and enjoying my free time. Feel free to talk! 😃")
133
134
134
135
    # If nothing's returned by now, return a general response
135
136
    return _("Probably chilling a bit. Feel free to talk! ❤")
136
137
137
138
# Views:
138
139
139
140
def index(request):
140
141
    timezone.activate("Europe/Brussels")
141
142
    time_string = timezone.localtime(timezone.now()).strftime(" (%H:%M) ")
142
143
    status = _("Current status/location:") + time_string + get_current_status()
143
144
    template = "about/index.html"
144
145
145
146
    cards = Card.objects.order_by('?') # The '?' indicates the objects must be ordered randomly.
146
147
    quotes = Quote.objects.all()
147
148
    randomQuote = random.choice(quotes)
148
149
149
150
    largeSizes = []
150
151
    mediumSizes = []
151
152
    for i in range(0, len(cards)):
152
153
        newLargeSizes = [6, 4, 2]
153
154
        newMediumSizes = [8, 4]
154
155
        random.shuffle(newLargeSizes)
155
156
        random.shuffle(newMediumSizes)
156
157
        largeSizes.extend(newLargeSizes)
157
158
        mediumSizes.extend(newMediumSizes)
158
159
159
160
    contextList = []
160
161
    i = 0
161
162
    for card in cards:
162
163
        contextList.append([card, mediumSizes[i], largeSizes[i]])
163
164
        i += 1
164
165
165
166
    # TODO: Move this stuff to the template module. This is basically a description
166
167
    # of HOW to display data, but the view module is only responsible for WHAT data
167
168
    # to display.
168
169
    context = {
169
170
            'status': status,
170
171
            'materialDesign_color': "blue",
171
172
            'materialDesign_accentColor': "orange",
172
173
            'navbar_title': "Maarten's website",
173
174
            'navbar_fixed': False,
174
175
            'parallax_src': "/media/about/images/parallax.png",
175
176
            'footer_title': "Home pages",
176
177
            'footer_description': footer_description,
177
178
            'footer_links': footer_links,
178
179
            'cards': contextList,
179
180
            'quote': randomQuote,
180
181
            }
181
182
182
183
    return render(request, template, context)
183
184
184
185
def myself(request):
185
186
    template = "about/about.html"
186
187
 
187
188
    context = {
188
189
            'subject': "Myself",
189
190
            'navbar_title': "Myself",
190
191
            'age': get_age(),
191
192
            }
192
193
    context.update(standard_context())
193
194
    return render(request, template, context)
194
195
195
196
def principles(request):
196
197
    template = "about/principles.html"
197
198
 
198
199
    data = Principle.objects.order_by('title')
199
200
200
201
    context = {
201
202
            'subject': "principles",
202
203
            'navbar_title': "Principles",
203
204
            'data': data,
204
205
            }
205
206
    context.update(standard_context())
206
207
    return render(request, template, context)
207
208
    
208
209
def hacking(request):
209
210
    template = "about/hacking.html"
210
211
 
211
212
    data = Hack.objects.order_by('title')
212
213
213
214
    context = {
214
215
            'subject': "hacking",
215
216
            'navbar_title': "Hacking",
216
217
            'data': data,
217
218
            }
218
219
    context.update(standard_context())
219
220
    return render(request, template, context)
220
221
    
221
222
def me(request):
222
223
    template = "about/me.html"
223
224
224
225
    data = AboutMe.objects.order_by('title')
225
226
226
227
    context = {
227
228
            'subject': "me",
228
229
            'navbar_title': "Me",
229
230
            'data': data,
230
231
            }
231
232
    context.update(standard_context())
232
233
    return render(request, template, context)
233
234
234
235
def webstack(request):
235
236
    template = "about/webstack.html"
236
237
 
237
238
    data = Stack.objects.order_by('title')
238
239
239
240
    context = {
240
241
            'subject': "web stack",
241
242
            'navbar_title': "Web stack",
242
243
            'data': data,
243
244
            }
244
245
    context.update(standard_context())
245
246
    return render(request, template, context)
246
247
    
247
248
def software(request):
248
249
    template = "about/software.html"
249
250
 
250
251
    data = Program.objects.order_by('title')
251
252
252
253
    context = {
253
254
            'subject': "software",
254
255
            'navbar_title': "Software",
255
256
            'data': data,
256
257
            }
257
258
    context.update(standard_context())
258
259
    return render(request, template, context)
259
260
    # WARNING:
260
261
    # Because I haven't been able to figure out yet how to add colors to hyperlinks, I put them hardcoded in the database. IF YOU FIND A WAY TO DO IT RIGHT, DO SO ASAP!
261
262
262
263
def security(request):
263
264
    template = "about/security.html"
264
265
 
265
266
    data = SecurityMeasure.objects.order_by('title')
266
267
267
268
    context = {
268
269
            'subject': "security",
269
270
            'navbar_title': "Security",
270
271
            'data': data,
271
272
            }
272
273
    context.update(standard_context())
273
274
    return render(request, template, context)
274
275
 
275
276
def roadmap(request):
276
277
    template = "about/roadmap.html"
277
278
 
278
279
    data = Goal.objects.order_by('title')
279
280
280
281
    context = {
281
282
            'subject': "roadmap",
282
283
            'navbar_title': "Roadmap",
283
284
            'data': data,
284
285
            }
285
286
    context.update(standard_context())
286
287
    return render(request, template, context)
287
288
 
288
289