home

Complete and enable messaging system

Author
Maarten Vangeneugden
Date
Dec. 10, 2020, 1:48 p.m.
Hash
9c61ac333b856fab5b2f66109da907b5e69fa3aa
Parent
b29aa4fcc9f454f461dfb7285e393a0408660578
Modified files
.gitignore
forms.py
templates/about/main_content.djhtml
views.py

.gitignore

1 addition and 0 deletions.

View changes Hide changes
1
1
*.mo
2
2
# Cache is useless to track
3
3
__pycache__/**
4
4
5
5
migrations/**
6
6
telegram_bot_token.txt
7
7
token.txt
+
8
token.txt

forms.py

6 additions and 0 deletions.

View changes Hide changes
+
1
from . import models
+
2
+
3
class MessageForm(forms.Form):
+
4
    name = forms.CharField(max_length=64)
+
5
    message = forms.CharField(min_length=1, max_length=2000)
+
6

templates/about/main_content.djhtml

7 additions and 6 deletions.

View changes Hide changes
1
1
{% load static %}
2
2
<section class="emphasis">
3
3
    <h1>{% trans "Welcome!" %}</h1>
4
4
    <p>
5
5
        {% blocktrans %}Hello there! I'm Maarten, a happy
6
6
        student from Belgium, and this is my amazing personal website.
7
7
        I like a lot of things, and I share some of those in here. Take
8
8
        a look around, read a bit, talk with me, and most importantly,
9
9
        enjoy your stay!{% endblocktrans %}
10
10
    </p>
11
11
</section>
12
12
<div class="cards">
13
13
    {# Blog card #}
14
14
    <div id="blog" class="card">
15
15
        <img src="/media/main/blog.jpg" />
16
16
        <h3>{% trans "Blog" %}</h3>
17
17
        <p>
18
18
            {% blocktrans %}My personal scribblepad, written from scratch,
19
19
            on which I ramble about everything I like. Highly recommended
20
20
            while eating breakfast.{% endblocktrans %}
21
21
        </p>
22
22
        <a class="btn text" href="{% url 'blog-index' %}">{% trans "Visit blog" %}</a>
23
23
    </div>
24
24
25
25
    {# Gitar card #}
26
26
    <div id="gitar" class="card" style="flex-grow: 2">
27
27
        <!-- HAHAHAA I CAN'T DESIGN LOGOS -->
28
28
        <img src="/media/main/gitar.png" />
29
29
        <h3>Gitar</h3>
30
30
        <p>
31
31
            {% blocktrans %}I actually don't use GitHub to host my code,
32
32
            contrary to a lot of other coders. Instead, I've been trying to
33
33
            create something that's just fit to my taste. I host a couple of
34
34
            archive repositories there, as well as dotfiles, and that jazz. It's
35
35
            a continuing work, so don't flip your desk if the layout is upside
36
36
            down tomorrow =3{% endblocktrans %}
37
37
        </p>
38
38
        <a class="btn text" href="{% url 'gitar-index' %}">
39
39
        {% trans "Check code" %}</a>
40
40
    </div>
41
41
42
42
    {# About myself card #}
43
43
    <div id="about" class="card">
44
44
        <img src="/media/about/images/parallax.png" />
45
45
        <!-- I still don't have a good picture or still life about
46
46
            myself, so yes, I'm copying my parallax for a while. Sorry! -->
47
47
        <h3>{% trans "About me" %}</h3>
48
48
        <p>
49
49
            {% blocktrans %}Well, if you wish to know more of me, I have a page
50
50
            where I describe myself in a couple more sentences. You know,
51
51
    because I can =)
52
52
            {% endblocktrans %}
53
53
        </p>
54
54
        <a class="btn text" href="{% url "main-myself" %}">{% trans "Read on" %}</a>
55
55
    </div>
56
56
57
57
    {# Publications card #}
58
58
    <div id="eldonoj" class="card">
59
59
        <!--<img src="/media/about/images/publications.png" />-->
60
60
        <h3>{% translate "Publications" %}</h3>
61
61
        <p>
62
62
            {% blocktranslate %}Throughout my academic career, I've written a lot,
63
63
            sometimes together with other people. To avoid that these papers get
64
64
            lost to the passing of time, I've decided to publish them on my website, should somebody
65
65
            want to read them.{% endblocktranslate %}
66
66
        </p>
67
67
        <a class="btn text" href="{% url "publications-index" %}">{% translate "Consult publications" %}</a>
68
68
    </div>
69
69
70
70
    {# Projects card #}
71
71
    <div id="project" class="card">
72
72
        <h3>{% trans "Other projects" %}</h3>
73
73
        <p>
74
74
            {% blocktrans %}Projects come and go, and with my website I can
75
75
            present them to you. This cards takes you to the
76
76
            archive of past projects.
77
77
            {% endblocktrans %}
78
78
        </p>
79
79
        <a class="btn text disabled" href="{% url "main-project-archive" %}">
80
80
            {% trans "View projects" %}</a>
81
81
    </div>
82
82
83
83
    {# Activism card #}
84
84
    <div id="aktivismo" class="card">
85
85
        <h3>{% trans "Activism" %}</h3>
86
86
        <p>
87
87
            {% blocktrans %}Trying to make this place better takes some time. So
88
88
            I made a page in the hopes that it can engage you to partake in that
89
89
            process. Because it's never too late to begin.
90
90
            {% endblocktrans %}
91
91
        </p>
92
92
        <a class="btn text" href="{% url "activism" %}">
93
93
            {% trans "List talking points" %}</a>
94
94
    </div>
95
95
96
96
</div>
97
97
{% comment %}
98
-
<section>
99
98
    <h2>{% translate "Contact me" %}</h2>
100
99
    <p>{% blocktranslate %}If you wish to contact me, use this convenient form,
101
100
        and leave some contact info of yourself so I can get back to you. Abuse
102
-
        will literally make your computer explode, so don't anger
103
-
        my server.{% endblocktranslate %}</p>
+
101
        something important, or correct a typo I made, it's all good, I love to
+
102
        hear from you!
+
103
        If necessary, put some contact info of yourself in your message so I can get
+
104
        back to you. Abuse will probably make your computer explode, so don't anger
+
105
        my server.{% endblocktranslate %}</p>
104
106
    <form action="" method="post">
105
107
        <input type="text" name="name" mozactionhint="send" enterkeyhint="send" maxlength="64" placeholder="{% translate "Your name" %}" required>
106
108
        <textarea name="" maxlength="2000" spellcheck="true" name="message" required
107
-
        <input type="submit" value="{% translate "Send" %}">
108
-
    </form>
+
109
        <input type="submit" value="✉️{% translate "Send" %}">
+
110
    </form>
109
111
</section>
110
112
{% endcomment %}
111
-

views.py

28 additions and 5 deletions.

View changes Hide changes
1
1
from datetime import date
+
2
from datetime import date
2
3
from django.utils import timezone
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.urls import reverse # Why?
6
7
from django.utils.translation import ugettext as _
7
8
from .models import *
8
9
+
10
+
11
9
12
def get_age():
10
13
    """Returns my current age."""
11
14
    today = date.today()
12
15
    birthday = date(1996, 8, 28)
13
16
    age = today - birthday
14
17
    years = str(int(age.days / 365))
15
18
    return years
16
19
17
20
def footer_description():
18
21
    return _("Main pages of Maarten's website, a %(years)s year old Belgian programmer. Also an undergraduate student of Informatics @ UHasselt, and graduate student of Engineering Informatics at Ghent University.") % {'years': get_age()}
19
22
20
23
def footer_links():
21
24
    footer_links = [
22
25
        [_("Contact me"), "mailto:maarten.vangeneugden@student.uhasselt.be"],
23
26
        [_("Hasselt University"), "https://www.uhasselt.be"],
24
27
        [_("Ghent University"), "https://www.ugent.be"],
25
28
            ]
26
29
    return footer_links
27
30
28
31
# TODO: Move this stuff to the template module. This is basically a description
29
32
# of HOW to display data, but the view module is only responsible for WHAT data
30
33
# to display.
31
34
def standard_context():
32
35
    context = {
33
36
            'navbar_backArrow': True,
34
37
            'footer_title': _("Home page"),
35
38
            'footer_description': footer_description(),
36
39
            'footer_links': footer_links(),
37
40
            'stylesheet_name': "home",
38
41
            }
39
42
    return context
40
43
41
44
def get_current_status(dt = None):
42
45
    """Returns a string specifying my current state (and sometimes location).
43
46
44
47
    This function is actually based on my weekly schedule. I'd normally hook it
45
48
    up to my iCal files, but that doesn't include things like sleeping. Not to
46
49
    mention my university has a hard time following standards like "Put the
47
50
    location in the location field, not in the title of the appointment". I
48
51
    figured a simple function would do the job just as well.
49
52
50
53
    Keyword arguments:
51
54
    dt -- The datetime object of the day to check (defaults to current local time)
52
55
    """
53
56
54
57
    MONDAY = 0
55
58
    TUESDAY = 1
56
59
    WEDNESDAY = 2
57
60
    THURSDAY = 3
58
61
    FRIDAY = 4
59
62
    SATURDAY = 5
60
63
    SUNDAY = 6
61
64
62
65
    if dt is None:
63
66
        timezone.activate("Europe/Brussels")
64
67
        dt = timezone.localtime(timezone.now())
65
68
66
69
    day = dt.weekday()
67
70
    hour = dt.time().hour
68
71
    minute = dt.time().minute
69
72
70
73
    """ Note on usage of the range() function:
71
74
    range(x, y) returns a list, beginning from x, but excluding y. So if a
72
75
    course runs from 13:00 to 15:00, then y should still be 15. Why? Because
73
76
    that makes it so that 14:59 is included, but 15:00 is not. if y would be 16,
74
77
    then 15:30 would also be included.
75
78
    """
76
79
77
80
    # If nothing's returned by now, return a general response
78
81
    return _("Probably chilling a bit. Feel free to talk! ❤")
79
82
80
83
# Views:
+
84
    """ Sends a message to me if the visitor submits one on my website. """
+
85
    # First task: Retrieve the token, which mustn't be in the tracker:
+
86
    with open("token.txt", 'r') as f:
+
87
        url = f.readline()
+
88
    url += name + " stuurt:\n" + message
+
89
    
+
90
    response = requests.get(url)
+
91
    return response.status_code == 200
+
92
+
93
# Views:
81
94
82
95
def index(request):
83
96
    timezone.activate("Europe/Brussels")
+
97
    # First, handle possible contact
+
98
    if request.method == "POST":  # Message received
+
99
        form = MessageForm(request.POST)
+
100
        if form.is_valid():
+
101
            clean_message = form.cleaned_data
+
102
            if send_message(clean_message.name, clean_message.message):
+
103
                context["contact_response"] = _("Message sent!")
+
104
            else:
+
105
                context["contact_response"] = _("An error occured while trying to send the message. Please try again later.")
+
106
        else:  # The submitted form data was invalid
+
107
            context["contact_response"] = _("The submitted form contained invalid data, and was discarded.")
+
108
        
+
109
+
110
    
+
111
    timezone.activate("Europe/Brussels")
84
112
    time_string = timezone.localtime(timezone.now()).strftime(" (%H:%M) ")
85
113
    status = _("Current status/location:") + time_string + get_current_status()
86
114
    template = "about/index.djhtml"
87
115
88
116
    # TODO: Move this stuff to the template module. This is basically a description
89
-
    # of HOW to display data, but the view module is only responsible for WHAT data
90
-
    # to display.
91
-
    context = standard_context()
92
-
93
-
    return render(request, template, context)
94
117
95
118
def myself(request):
96
119
    template = "about/about.djhtml"
97
120
98
121
    context = {
99
122
            'subject': _("Myself"),
100
123
            'navbar_title': _("Myself"),
101
124
            'age': get_age(),
102
125
            }
103
126
    context.update(standard_context())
104
127
    return render(request, template, context)
105
128
106
129
def project_archive(request):
107
130
    template = "about/project-archive.djhtml"
108
131
    context = standard_context()
109
132
    return render(request, template, context)
110
133
111
134
def activism(request):
112
135
    template = "about/activism.djhtml"
113
136
    context = standard_context()
114
137
    return render(request, template, context)
115
138
    
116
139