Skip to content

Commit

Permalink
Use a select field for universities
Browse files Browse the repository at this point in the history
  • Loading branch information
rth committed May 7, 2021
1 parent eb37d41 commit 63494b3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
7 changes: 3 additions & 4 deletions ramp-database/ramp_database/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self, name, hashed_password, lastname, firstname, email,
access_level='user', hidden_notes='', linkedin_url='',
twitter_url='', facebook_url='', google_url='', github_url='',
website_url='', bio='', is_want_news=True,
universities_id=None):
universities_id=None, graduation_year=None):
self.name = name
self.hashed_password = hashed_password
self.lastname = lastname
Expand All @@ -164,9 +164,8 @@ def __init__(self, name, hashed_password, lastname, firstname, email,
self.website_url = website_url
self.bio = bio
self.is_want_news = is_want_news
if universities_id is not None:
self.university = University.query.filter_by(
id=universities_id).one_or_none()
self.universities_id = universities_id
self.graduation_year = graduation_year

@property
def is_active(self):
Expand Down
12 changes: 10 additions & 2 deletions ramp-database/ramp_database/tools/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
def add_user(session, name, password, lastname, firstname, email,
access_level='user', hidden_notes='', linkedin_url='',
twitter_url='', facebook_url='', google_url='', github_url='',
website_url='', bio='', is_want_news=True):
website_url='', bio='', is_want_news=True,
universities_id=None, university=None, graduation_year=None):
"""Add a new user in the database.
Parameters
Expand Down Expand Up @@ -58,6 +59,12 @@ def add_user(session, name, password, lastname, firstname, email,
User biography.
is_want_news : bool, default is True
User wish to receive some news.
universities_id: int, default None
The id of the university
university: University
The university object
graduation_year: int, default=None
Graduation year for students
Returns
-------
Expand All @@ -74,7 +81,8 @@ def add_user(session, name, password, lastname, firstname, email,
linkedin_url=linkedin_url, twitter_url=twitter_url,
facebook_url=facebook_url, google_url=google_url,
github_url=github_url, website_url=website_url, bio=bio,
is_want_news=is_want_news)
is_want_news=is_want_news, universities_id=universities_id,
graduation_year=graduation_year)

# Creating default team with the same name as the user
# user is admin of his/her own team
Expand Down
8 changes: 7 additions & 1 deletion ramp-frontend/ramp_frontend/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from wtforms import ValidationError
from wtforms.widgets import CheckboxInput
from wtforms.widgets import ListWidget
from wtforms.ext.sqlalchemy.fields import QuerySelectField

from ramp_database.model import University


def _space_check(form, field):
Expand Down Expand Up @@ -92,7 +95,10 @@ class UserUpdateProfileForm(FlaskForm):
validators.DataRequired(),
validators.NumberRange(min=2010, max=2040)
])
university = StringField('university', [validators.DataRequired()])
university = QuerySelectField(
query_factory=lambda: University.query.all(),
get_label=lambda x: f"{x.name} ({x.country})",
)
is_want_news = BooleanField()


Expand Down
7 changes: 1 addition & 6 deletions ramp-frontend/ramp_frontend/templates/sign_up.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ <h4 class="modal-title" id="myModalLabel">{{ category }}</h4>
</div>
<div class="form-group">
<label>University</label> <br />
{{ form.university(placeholder="university", list="university-list") }}
<datalist id="university-list">
{% for el in university_names %}
<option value="{{ el[0] }}">
{% endfor %}
</datalist>
{{ form.university(placeholder="university", style="width: 500px;") }}
{% for error in form.website_url.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
Expand Down
3 changes: 2 additions & 1 deletion ramp-frontend/ramp_frontend/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def sign_up():
bio=form.bio.data,
is_want_news=form.is_want_news.data,
access_level='not_confirmed',
graduation_year=form.graduation_year,
universities_id=form.university.data.id,
graduation_year=form.graduation_year.data,
)
except NameClashError as e:
flash(str(e))
Expand Down

0 comments on commit 63494b3

Please sign in to comment.