Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ennead/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

from ennead.views.auth import register, register_page, login, login_page, logout
from ennead.views.tasks import index
from ennead.views.student_profile import create_update_student_profile, read_student_profile

from ennead.models.base import database
from ennead.models.user import User
from ennead.models.task import TaskSet, Task
from ennead.models.thread import Thread, Post
from ennead.models.student_profile import StudentProfile


def inject_user() -> None:
Expand All @@ -38,7 +40,7 @@ def create_app(config_path: Optional[str] = None) -> Flask:
app.config.from_object(config)

database.initialize(config.DB_CLASS(config.DB_NAME, **config.DB_PARAMS))
database.create_tables([User, Task, TaskSet, Thread, Post])
database.create_tables([User, Task, TaskSet, Thread, Post, StudentProfile])

app.before_request(inject_user)

Expand All @@ -50,8 +52,12 @@ def create_app(config_path: Optional[str] = None) -> Flask:
app.add_url_rule('/login', 'login', login, methods=['POST'])
app.add_url_rule('/logout', 'logout', logout)

app.add_url_rule('/student_profile', 'read_student_profile', read_student_profile)
app.add_url_rule('/student_profile', 'create_student_profile', create_update_student_profile, methods=['POST'])

return app


if __name__ == '__main__':
create_app('ennead.json').run()

38 changes: 38 additions & 0 deletions ennead/models/student_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Module for Ennead task model and corresponding helper classes"""

from typing import TYPE_CHECKING, List

from peewee import BooleanField, CharField, TextField, IntegerField, ForeignKeyField, DateField

from datetime import date
from ennead.models.user import User
from flask import Request

from ennead.models.base import BaseModel


class StudentProfile(BaseModel):
"""Student questionnaire

Attributes:
grade: year of education
city: city of living
birth_date: date of birth
sex: sex (True if male)
allergy: human-readable list of allergies
communication: preferred communication type (vk/telegram profile)
telephone: student's telephone number
parent_information: human-readable name and contacts of one
"""

grade: int = IntegerField()
city: str = CharField(32)
birth_date: date = DateField()
sex: bool = BooleanField()
allergy: str = TextField()
communication: str = TextField()
telephone: str = CharField(20)
parent_information: str = TextField()

user: User = ForeignKeyField(User, backref='student_profiles')

4 changes: 4 additions & 0 deletions ennead/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
position: relative;
top: 0.05em;
}

textarea.student-profile-area {
height: 6em;
}
48 changes: 48 additions & 0 deletions ennead/templates/student_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% extends 'base.html' %}

{% block title %}Ennead / Анкета ученика{% endblock %}

{% block body %}
<div class="row">
<div class="col-4 offset-4">
<form method="POST">
<div class="form-group row">
<label class="col-12" for="grade">Класс, который вы закончили:</label>
<input class="col-12 form-control" type="number" name="grade" id="grade" required value="{{ student_profile.grade }}">
</div>
<div class="form-group row">
<label class="col-12" for="city">Город, в котором вы живёте:</label>
<input class="col-12 form-control" name="city" id="city" required value="{{ student_profile.city }}">
</div>
<div class="form-group row">
<label class="col-12" for="birth_date">Дата вашего рождения:</label>
<input class="col-12 form-control" type="date" name="birth_date" id="birth_date" required value="{{ student_profile.birth_date }}">
</div>
<div class="form-group row">
<label class="col-12" for="sex">Ваш пол:</label>
<select class="col-12 form-control" name="sex" id="sex" required value="{{ student_profile.sex }}">
<option value="1">Мужской</option>
<option value="0">Женский</option>
</select>
</div>
<div class="form-group row">
<label class="col-12" for="allergy">Перечислите всё, на что у вас аллергия:</label>
<textarea class="col-12 form-control" type="textarea" name="allergy" id="allergy" required>{{ student_profile.allergy }}</textarea>
</div>
<div class="form-group row">
<label class="col-12" for="communication">Ссылка на вас в социальных сетях (vk, telegram):</label>
<input class="col-12 form-control" name="communication" id="communication" required value="{{ student_profile.communication }}">
</div>
<div class="form-group row">
<label class="col-12" for="telephone">Ваш мобильный телефон:</label>
<input class="col-12 form-control" name="telephone" id="telephone" required value="{{ student_profile.telephone }}">
</div>
<div class="form-group row">
<label class="col-12" for="parent_information">Контактный телефон и имя вашего родителя:</label>
<textarea class="col-12 student-profile-area form-control" type="textarea" name="parent_information" id="parent_information" required >{{ student_profile.parent_information }}</textarea>
</div>
<input type="submit" class="btn btn-primary" value="Сохранить">
</form>
</div>
</div>
{% endblock %}
46 changes: 46 additions & 0 deletions ennead/views/student_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Views, used for student profiles"""

from datetime import date
from ennead.models.user import User

from flask import session, current_app, render_template, request, redirect, url_for, g
from werkzeug.wrappers import Response

from ennead.utils import require_logged_in
from ennead.models.user import User, UserGroup
from ennead.models.student_profile import StudentProfile


@require_logged_in
def read_student_profile() -> Response:
"""GET /student_profile: read profile page"""

if g.user.student_profiles:
student_profile = g.user.student_profiles[0]
else:
student_profile = StudentProfile()

return render_template('student_profile.html', student_profile=student_profile)


@require_logged_in
def create_update_student_profile() -> Response:
"""POST /student_profile: update student profile"""

if g.user.student_profiles:
student_profile = g.user.student_profiles[0]
else:
student_profile = StudentProfile()

for field in ('grade', 'city', 'birth_date', 'allergy', 'sex',
'communication', 'parent_information'):
setattr(student_profile, field, request.form[field])

# TODO: add telephone validation
student_profile.telephone = request.form['telephone']

student_profile.user = g.user

student_profile.save()
return redirect(url_for('read_student_profile'))