home

Small bug fix in views

Author
Maarten Vangeneugden
Date
Oct. 19, 2017, 7:53 p.m.
Hash
dc201272450aa39f658b78f1ef65f2460587f007
Parent
775e4ebb847a5778add9af9b06bf7453b4837953
Modified file
views.py

views.py

1 addition and 1 deletion.

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