home

Change the get_current_status function to be correct

Author
Vngngdn
Date
Feb. 18, 2017, 3:29 p.m.
Hash
60df9610f72a89fd8f3b40d31dc7901cf1e641c6
Parent
8e44d6914181b48a9b71829de274252c9849f1eb
Modified file
views.py

views.py

80 additions and 29 deletions.

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