Skip to content

Commit

Permalink
Merge pull request #2 from City-of-Turku/feature/initial-project
Browse files Browse the repository at this point in the history
Feature/initial project
  • Loading branch information
juuso-j authored Feb 1, 2024
2 parents 8fc3f03 + a77bc87 commit ce16831
Show file tree
Hide file tree
Showing 88 changed files with 5,264 additions and 16 deletions.
28 changes: 28 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
/postgres-data/
8 changes: 4 additions & 4 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -53,7 +53,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -67,4 +67,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
17 changes: 7 additions & 10 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ jobs:


steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.10.4
- name: Install required Ubuntu packages
run: |
sudo apt-get install gdal-bin
# - name: Create needed postgis extensions
# run: |
# psql -h localhost -U postgres template1 -c 'create extension postgis;'
sudo apt-get update && sudo apt-get install gdal-bin
- name: Install PyPI dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Python side code neatness tests
run: |
flake8
black --check .
# black --check .
isort . -c
- name: Run pytest code functionality tests
run: |
Expand All @@ -45,7 +42,7 @@ jobs:
pip install coverage
coverage report -m
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
# Majority of the tests require database
services:
# Label used to access the service container
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ dmypy.json

# Pyre type checker
.pyre/

# static files
static/

# Django project
.django_secret
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# mpbackend
Mobility Profile Backend
# Mobility Profile Backend
This is the backend for the Mobility Profile

## Installation with Docker Compose
First configure development environment settings as stated in `config_dev.env.example`.

### Running the application
Run application with `docker-compose up`
This will startup and bind local postgres and mobilityprofile backend.

### Runnig the application in production
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

### Importing questions
To import questions run: `docker-compose run mpbackend import_questions`


## Installation without Docker
1.
First, install the necessary Debian packages.
TODO, add packages.

2. Clone the repository.
```
git clone https://github.com/City-of-Turku/mpbackend.git
```
3. Install python 3.10 and pip requiremends
Be sure to load the **environment** before installing the requirements.
```
pip install pip-tools
pip install -r requirements.txt
```
4. Setup the PostGIS database.

Please note, we recommend PostgreSQL version 13 or higher.
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
```

5. Create database tables.
```
./manage.py migrate
```

6. Import questions
```
./manage.py import_questions
```

Empty file added account/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions account/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django import forms
from django.contrib import admin

from .models import MailingList, Profile, User

admin.site.register(User)


class ProfileAdmin(admin.ModelAdmin):
list_display = ("user", "result")

def result(self, obj):
if obj.user.result:
return obj.user.result.value
else:
return None


class MailingListAdminForm(forms.ModelForm):
csv_emails = forms.CharField(widget=forms.Textarea(attrs={"rows": 20, "cols": 120}))

class Meta:
model = MailingList
fields = ["result"]


class MailingListAdmin(admin.ModelAdmin):
list_display = ("result", "csv_emails")

readonly_fields = ("result",)
form = MailingListAdminForm

def csv_emails(self, obj):
return obj.csv_emails

def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
form.base_fields["csv_emails"].initial = self.csv_emails(obj)
return form

def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
extra_context["show_save_and_add_another"] = False
extra_context["show_save_and_continue"] = False
return super().change_view(
request, object_id, form_url, extra_context=extra_context
)

def has_delete_permission(self, request, obj=None):
return False


admin.site.register(Profile, ProfileAdmin)
admin.site.register(MailingList, MailingListAdmin)
Empty file added account/api/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions account/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers

from account.models import MailingListEmail, Profile


class SubscribeSerializer(serializers.Serializer):
email = serializers.CharField()
result = serializers.IntegerField()


class UnSubscribeSerializer(serializers.Serializer):
email = serializers.CharField()


class PublicUserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ["id"]


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = [
"id",
"username",
"email",
"first_name",
"last_name",
"last_login",
"date_joined",
"is_staff",
"is_active",
"email_verified",
]


class ProfileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Profile
fields = [
"id",
"year_of_birth",
"postal_code",
"optional_postal_code",
"is_filled_for_fun",
"result_can_be_used",
]


class MailingListEmailSerializer(serializers.ModelSerializer):
class Meta:
model = MailingListEmail
fields = "__all__"
13 changes: 13 additions & 0 deletions account/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import include, path
from rest_framework import routers

from . import views

app_name = "account"

# Create a router and register our viewsets with it.
router = routers.DefaultRouter()
router.register("profile", views.ProfileViewSet, "profiles")

# The API URLs are now determined automatically by the router.
urlpatterns = [path("", include(router.urls), name="account")]
Loading

0 comments on commit ce16831

Please sign in to comment.