Skip to content

Commit

Permalink
Merge branch 'develop' into feature/recaptcha-verification
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j authored Mar 4, 2024
2 parents 37c7825 + 2c89770 commit 0c68ccd
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

env:
# Database for tests
DATABASE_URL: postgis://postgres:postgres@localhost/smbackend
DATABASE_URL: postgres://postgres:postgres@localhost/mobilityprofile
GOOGLE_RECAPTCHA_SECRET_KEY: testSecret
GOOGLE_RECAPTCHA_VERIFY_URL: https://www.google.com/recaptcha/api/siteverify

GOOGLE_RECAPTCHA_VERIFY_URL: https://www.google.com/recaptcha/api/siteverif
steps:
- uses: actions/checkout@v4
- name: Set up Python
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ Local setup:

```
sudo su postgres
psql template1 -c 'CREATE EXTENSION IF NOT EXISTS postgis;'
createuser -RSPd mobilityprofile
createdb -O mobilityprofile -T template1 -l fi_FI.UTF-8 -E utf8 mobilityprofile
createdb -O mobilityprofile -l fi_FI.UTF-8 -E utf8 mobilityprofile
```

5. Create database tables.
Expand Down
1 change: 0 additions & 1 deletion account/migrations/0011_profile_gender.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("account", "0010_rename_age_profile_year_of_birth"),
]
Expand Down
10 changes: 2 additions & 8 deletions config_dev.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ DEBUG=True

# Configures database for mpbackend using URL style. The format is
#
# postgis://USER:PASSWORD@HOST:PORT/NAME
# postgres://USER:PASSWORD@HOST:PORT/NAME
#
# Unused components may be left out, only Postgis is supported.
# The example below configures mpbackend to use local PostgreSQL database
# called "mpbackend", connecting same as username as Django is running as.
# Django setting: DATABASES (but not directly) https://docs.djangoproject.com/en/4.2/ref/settings/#databases
# When running with docker change 'localhost' host 'postgres'.
DATABASE_URL=postgis://mobilityprofile:mobilityprofile@localhost:5432/mobilityprofile
DATABASE_URL=postgres://mobilityprofile:mobilityprofile@localhost:5432/mobilityprofile

# List of Host-values, that mpbackend will accept in requests.
# This setting is a Django protection measure against HTTP Host-header attacks
Expand All @@ -27,12 +27,6 @@ CORS_ORIGIN_WHITELIST=http://localhost:8080
# https://docs.djangoproject.com/en/2.2/ref/settings/#languages
LANGUAGES=fi,sv,en

# Email settings
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=smtp.turku.fi
[email protected]
EMAIL_PORT=25
EMAIL_USE_TLS=True

# Media root is the place in file system where Django and, by extension
# smbackend stores "uploaded" files. This means any and all files
Expand Down
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ services:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data

memcached:
image: memcached:latest
command: ["-I", "4m"]
ports:
- "11211:11211"
restart: always

mpbackend:
image: mpbackend
Expand Down
3 changes: 0 additions & 3 deletions docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
FROM postgres:14

RUN apt-get update && apt-get install --no-install-recommends -y \
postgis postgresql-14-postgis-3 postgresql-14-postgis-3-scripts

RUN localedef -i fi_FI -c -f UTF-8 -A /usr/share/locale/locale.alias fi_FI.UTF-8

ENV LANG fi_FI.UTF-8
Expand Down
Binary file modified media/questions.xlsx
Binary file not shown.
15 changes: 1 addition & 14 deletions mpbackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
env = environ.Env(
DEBUG=(bool, False),
LANGUAGES=(list, ["fi", "sv", "en"]),
DATABASE_URL=(str, "postgis:///servicemap"),
DATABASE_URL=(str, "postgres://mobilityprofile:mobilityprofile"),
ALLOWED_HOSTS=(list, []),
EMAIL_BACKEND=(str, None),
EMAIL_HOST=(str, None),
EMAIL_HOST_USER=(str, None),
EMAIL_PORT=(int, None),
EMAIL_USE_TLS=(bool, None),
MEDIA_ROOT=(environ.Path(), root("media")),
STATIC_ROOT=(environ.Path(), root("static")),
MEDIA_URL=(str, "/media/"),
Expand All @@ -41,7 +36,6 @@
DEBUG = env("DEBUG")
ALLOWED_HOSTS = env("ALLOWED_HOSTS")


# Custom user model
AUTH_USER_MODEL = "account.User"

Expand Down Expand Up @@ -74,7 +68,6 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
# "mpbackend.middleware.TimingMiddleware",
]

ROOT_URLCONF = "mpbackend.urls"
Expand Down Expand Up @@ -241,12 +234,6 @@
},
}

EMAIL_BACKEND = env("EMAIL_BACKEND")
EMAIL_HOST = env("EMAIL_HOST")
EMAIL_HOST_USER = env("EMAIL_HOST_USER")
EMAIL_PORT = env("EMAIL_PORT")
EMAIL_USE_TLS = env("EMAIL_USE_TLS")

SPECTACULAR_SETTINGS = {
"TITLE": "Mobility Profile API",
"DESCRIPTION": "Your project description",
Expand Down
18 changes: 7 additions & 11 deletions profiles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@ def get_or_create_row(model, filter):


def sub_question_condition_met(sub_question_condition, user):
if (
Answer.objects.filter(user=user, option=sub_question_condition.option).count()
> 0
):
return True
return False
return Answer.objects.filter(
user=user, option=sub_question_condition.option
).exists()


def verify_recaptcha(token):
Expand Down Expand Up @@ -123,7 +120,6 @@ def question_condition_met(question_condition_qs, user):
def update_postal_code_result(user):
# Ensure that duplicate results are not saved, profiles filled for fun and profiles whos result
# can not be used are ignored.

if (
user.postal_code_result_saved
or user.profile.is_filled_for_fun
Expand Down Expand Up @@ -371,7 +367,7 @@ def check_if_question_condition_met(self, request):
)
# Retrive the conditions for the question, note can have multiple conditions
question_condition_qs = QuestionCondition.objects.filter(question=question)
if question_condition_qs.count() == 0:
if not question_condition_qs.exists():
return Response(
f"QuestionCondition not found for question number {question_id}",
status=status.HTTP_404_NOT_FOUND,
Expand Down Expand Up @@ -457,7 +453,7 @@ def in_condition(self, request, *args, **kwargs):
)

qs = QuestionCondition.objects.filter(question_condition=question)
if qs.count() > 0:
if qs.exists():
response_data["in_condition"] = True
serializer = InConditionResponseSerializer(data=response_data)
if serializer.is_valid():
Expand Down Expand Up @@ -611,7 +607,7 @@ def create(self, request, *args, **kwargs):
status=status.HTTP_404_NOT_FOUND,
)
question_condition_qs = QuestionCondition.objects.filter(question=question)
if question_condition_qs.count() > 0:
if question_condition_qs.exists():
if not question_condition_met(question_condition_qs, user):
return Response(
"Question condition not met, i.e. the user has answered so that this question cannot be answered",
Expand All @@ -638,7 +634,7 @@ def create(self, request, *args, **kwargs):
)
filter["other"] = other
queryset = Answer.objects.filter(**filter)
if queryset.count() == 0:
if not queryset.exists():
filter["option"] = option
Answer.objects.create(**filter)
else:
Expand Down
3 changes: 3 additions & 0 deletions profiles/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ def get_and_create_results(data: pd.DataFrame) -> list:
col_data = data[column]
topic = get_language_dict(data.columns[RESULT_COLUMNS[0] + i])
description = get_language_dict(col_data[0])
value = get_language_dict(col_data[1])
filter = {"topic": topic["fi"]}
update_filter = {}

for lang in LANGUAGES:
update_filter[f"topic_{lang}"] = topic[lang]
update_filter[f"description_{lang}"] = description[lang]
update_filter[f"value_{lang}"] = value[lang]

queryset = Result.objects.filter(**filter)
if queryset.count() == 0:
result = Result.objects.create(**filter)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.2 on 2024-02-27 09:34

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("profiles", "0015_alter_question_options"),
]

operations = [
migrations.AddField(
model_name="result",
name="value",
field=models.CharField(max_length=64, null=True),
),
migrations.AddField(
model_name="result",
name="value_en",
field=models.CharField(max_length=64, null=True),
),
migrations.AddField(
model_name="result",
name="value_fi",
field=models.CharField(max_length=64, null=True),
),
migrations.AddField(
model_name="result",
name="value_sv",
field=models.CharField(max_length=64, null=True),
),
]
19 changes: 19 additions & 0 deletions profiles/migrations/0017_answer_create_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2 on 2024-02-29 08:41

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("profiles", "0016_result_value_result_value_en_result_value_fi_and_more"),
]

operations = [
migrations.AddIndex(
model_name="answer",
index=models.Index(
fields=["user", "question", "sub_question"],
name="profiles_an_user_id_8141b8_idx",
),
),
]
16 changes: 16 additions & 0 deletions profiles/migrations/0018_postalcoderesult_add_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 4.2 on 2024-02-29 08:56

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("profiles", "0017_answer_create_indexes"),
]

operations = [
migrations.AlterModelOptions(
name="postalcoderesult",
options={"ordering": ["postal_code", "postal_code_type"]},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2 on 2024-02-29 10:06

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("profiles", "0018_postalcoderesult_add_ordering"),
]

operations = [
migrations.AlterModelOptions(
name="questioncondition",
options={"ordering": ["id"]},
),
migrations.AlterModelOptions(
name="subquestioncondition",
options={"ordering": ["id"]},
),
]
20 changes: 20 additions & 0 deletions profiles/migrations/0020_postalcode_postalcodetype_add_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2 on 2024-02-29 10:25

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("profiles", "0019_questioncondition_subquestion_condition_add_ordering"),
]

operations = [
migrations.AlterModelOptions(
name="postalcode",
options={"ordering": ["id"]},
),
migrations.AlterModelOptions(
name="postalcodetype",
options={"ordering": ["id"]},
),
]
Loading

0 comments on commit 0c68ccd

Please sign in to comment.