Skip to content

Commit

Permalink
login_from_uuid + pulldata + fixed test (#92)
Browse files Browse the repository at this point in the history
* login_from_uuid & pull data functions

functional, no unit tests

* updated gitignore for pycharm

* removing datapull from unhandled function tests

* sorting imports for style compliance

* running ruff --fix

no idea why this is supposed to be better

* removing .coverage files

* implementing sangyoon's suggestions

* requested updates implemented
  • Loading branch information
sylvaingchassang authored Aug 12, 2024
1 parent a3e53f6 commit 12d8953
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# coverage
.coverage

# PyCharm
.idea

# Flask
instance/
.flask_sessions/
Expand Down
22 changes: 14 additions & 8 deletions safely_report/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ def index():
return render_template("auth/index.html")


@auth_blueprint.route("/login/respondent/<uuid>", methods=["GET"])
def login_respondent_with_uuid(uuid):
user = User.query.filter_by(uuid=uuid).first()
if user is not None and user.role == Role.Respondent:
login_user(user)
current_app.logger.info(f"Login - user {user.id}")
return redirect(url_for("survey.index"))
current_app.logger.warning("Failed respondent login")
flash("Respondent not found", "error")
return redirect(url_for("auth.login_respondent"))


@auth_blueprint.route("/login/respondent", methods=["GET", "POST"])
def login_respondent():
form = make_auth_form("Please enter your UUID:")
form.meta.update_values({"back_url": url_for("auth.index")})

if form.validate_on_submit():
uuid = form.field.data
user = User.query.filter_by(uuid=uuid).first()
if user is not None and user.role == Role.Respondent:
login_user(user)
current_app.logger.info(f"Login - user {user.id}")
return redirect(url_for("survey.index"))
current_app.logger.warning("Failed respondent login")
flash("Respondent not found", "error")
return redirect(url_for("auth.login_respondent"))
return redirect(
url_for("auth.login_respondent_with_uuid", uuid=uuid))

return render_template("auth/submit.html", form=form)

Expand Down
37 changes: 37 additions & 0 deletions safely_report/survey/xlsform_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from typing import Any

from flask_login import current_user

from safely_report.models import Respondent, Role


class XLSFormFunctions:
"""
Expand Down Expand Up @@ -29,3 +33,36 @@ def _selected_at(cls, choice_array: list[str], index: int) -> str:
return choice_array[index]
except IndexError:
return ""

@classmethod
def _str2int(cls, value: str) -> int:
return int(value)

@classmethod
def _pulldata(cls, column: str, default: str) -> str:
"""
Pull data from 'column' in the respondent roster table.
Parameters
----------
column: str
Name of the field you want to retrieve
default: str
Default value in case the field is not available,
or the user is an Enumerator
Returns
-------
str
Field value if available, default otherwise
"""
if current_user.role != Role.Respondent:
return default

respondent_uuid = current_user.uuid
respondent = Respondent.query.filter_by(uuid=respondent_uuid).first()

if hasattr(respondent, column):
return getattr(respondent, column)
else:
return default
Binary file modified tests/data/test_cases.xlsx
Binary file not shown.
3 changes: 2 additions & 1 deletion tests/integration/xlsform_reader/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"case_name, error_message",
[
("unsupported_question", "Unsupported question type: geopoint"),
("unsupported_function", "Unsupported XLSForm function: pulldata"),
("infinite_repeat", "Infinite repeat not allowed: holiday_loop"),
("nested_repeat", "Nested repeat not allowed: person_loop"),
("unsupported_function", "Unsupported XLSForm function: "
"notyetimplemented"),
],
)
def test_xlsform_validation(
Expand Down

0 comments on commit 12d8953

Please sign in to comment.