Fakultas Ilmu Komputer UI

Commit 2503156d authored by Yumna Pratista Tastaftian's avatar Yumna Pratista Tastaftian
Browse files

Merge branch 'staging' into 'master'

Integrate SonarQube

See merge request ppl-fasilkom-ui/2020/ppl-c/ppti-mobile-apps-monitoring-wabah-tuberkolosis/neza-backend!2
parents 48645cf7 406a7334
# Created by https://www.gitignore.io/api/django,virtualenv
# Edit at https://www.gitignore.io/?templates=django,virtualenv
### Django ###
*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
db.sqlite3-journal
media
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly.
# <django-project-name>/staticfiles/
### Django.Python Stack ###
# Byte-compiled / optimized / DLL files
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# Mr Developer
.mr.developer.cfg
.project
.pydevproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
### VirtualEnv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
pyvenv.cfg
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pip-selfcheck.json
### VSCode ###
.vscode
env
\ No newline at end of file
### SonarQube ###
.sonar/
.scannerwork/
# End of https://www.gitignore.io/api/django,virtualenv
\ No newline at end of file
stages:
- test
- sonar-scanner
- deploy
test:
image: python:3.6.5
stage: test
before_script:
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- pip install -r requirements.txt
- >
if [[ "${CI_COMMIT_REF_NAME}" == "staging" ]];
then
DATABASE_URL=$DATABASE_URL_STAGING
export DATABASE_NAME=d2puge2oh9kcgg
export DATABASE_USER=wvkketkmnoishk
export DATABASE_PASSWORD=3a76f9e64591d23ed092961d90beec80889e1788b668fdfc6e897065d20e6b0a
export DATABASE_HOST=ec2-18-213-176-229.compute-1.amazonaws.com
export DATABASE_PORT=5432
elif [[ "${CI_COMMIT_REF_NAME}" == "master" ]];
then
DATABASE_URL=$DATABASE_URL_PRODUCTION
export DATABASE_NAME=d16n2kmrcvrd3g
export DATABASE_USER=owrnaqzrbylqzh
export DATABASE_PASSWORD=eee44bfc9b5177f575bb40b363b7539c27625c82e58135f00a5d7df9586bd49a
export DATABASE_HOST=ec2-18-213-176-229.compute-1.amazonaws.com
export DATABASE_PORT=5432
fi
- python manage.py makemigrations
- python manage.py migrate
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -y xvfb
- python manage.py collectstatic --no-input
- python manage.py runserver 8000 &
when: on_success
script:
- echo "Starting tests"
- coverage run --include="./app/*" manage.py test
- coverage erase
- coverage run --include="./*/*" --omit="./env/*","./project/*","./manage.py" manage.py test apps
- coverage xml -i
- coverage report -m
artifacts:
paths:
- coverage.xml
SonarScanner:
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
stage: sonar-scanner
script:
- sonar-scanner
-Dsonar.host.url=$SONARQUBE_URL
-Dsonar.login=$SONARQUBE_TOKEN
-Dsonar.branch.name=$CI_COMMIT_REF_NAME
-Dsonar.projectKey=$SONARQUBE_PROJECT_KEY
deploy:
image: ruby:2.4
......@@ -47,44 +44,33 @@ deploy:
- >
if [[ "${CI_COMMIT_REF_NAME}" == "staging" ]];
then
HEROKU_APP=$HEROKU_STAGING_APP
export HEROKU_APP_NAME=$HEROKU_STAGING_APP
HEROKU_APP_HOST=http://tuberculosis-ppti-staging.herokuapp.com/
ENVIRONMENT_NAME=staging
DATABASE_URL=$DATABASE_URL_STAGING
export DATABASE_NAME=$DATABASE_NAME_STAGING
export DATABASE_USER=$DATABASE_USER_STAGING
export DATABASE_PASSWORD=$DATABASE_PASSWORD_STAGING
export DATABASE_HOST=$DATABASE_HOST_STAGING
elif [[ "${CI_COMMIT_REF_NAME}" == "master" ]];
then
HEROKU_APP=$HEROKU_PRODUCTION_APP
export HEROKU_APP_NAME=$HEROKU_PRODUCTION_APP
HEROKU_APP_HOST=http://tuberculosis-ppti.herokuapp.com/
ENVIRONMENT_NAME=production
fi
- >
if [[ "${CI_COMMIT_REF_NAME}" == "staging" ]];
then
DATABASE_URL=$DATABASE_URL_STAGING
export DATABASE_NAME=d2puge2oh9kcgg
export DATABASE_USER=wvkketkmnoishk
export DATABASE_PASSWORD=3a76f9e64591d23ed092961d90beec80889e1788b668fdfc6e897065d20e6b0a
export DATABASE_HOST=ec2-18-213-176-229.compute-1.amazonaws.com
export DATABASE_PORT=5432
export HEROKU_APP_NAME=tuberculosis-ppti-staging
elif [[ "${CI_COMMIT_REF_NAME}" == "master" ]];
then
DATABASE_URL=$DATABASE_URL_PRODUCTION
export DATABASE_NAME=d16n2kmrcvrd3g
export DATABASE_USER=owrnaqzrbylqzh
export DATABASE_PASSWORD=eee44bfc9b5177f575bb40b363b7539c27625c82e58135f00a5d7df9586bd49a
export DATABASE_HOST=ec2-18-213-176-229.compute-1.amazonaws.com
export DATABASE_PORT=5432
export HEROKU_APP_NAME=tuberculosis-ppti
export DATABASE_NAME=$DATABASE_NAME_PROD
export DATABASE_USER=$DATABASE_USER_PROD
export DATABASE_PASSWORD=$DATABASE_PASSWORD_PROD
export DATABASE_HOST=$DATABASE_HOST_PROD
fi
- export HEROKU_API_KEY=0ee33a63-1aa9-4e5a-880a-ca123fdbbf16
- echo $HEROKU_APP_NAME
- echo $HEROKU_APP_HOST
- echo $HEROKU_API_KEY
- heroku run --app $HEROKU_APP_NAME makemigrations
- export DATABASE_PORT=5432
- export DATABASE_IS_PSQL=true
script:
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY
- export HEROKU_API_KEY=$HEROKU_API_KEY
- heroku run --app $HEROKU_APP_NAME migrate
only:
- staging
- master
environment:
name: $ENVIRONMENT_NAME
url: $HEROKU_APP_HOST
FROM python:3.7-alpine
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
LABEL maintainer="nezappl@gmail.com"
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
COPY ./requirements.txt /requirements.txt
RUN pip install -r requirements.txt
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D user
USER user
CMD python manage.py runserver 0.0.0.0:$PORT
\ No newline at end of file
web: gunicorn app.wsgi
release: python manage.py migrate
web: gunicorn project.wsgi
......@@ -4,6 +4,53 @@
This repository contains codebase for TBCare backend system
## How to Run
1. Run an instance of PostgreSQL database. Then set the credentials on a `.env` file at the root directory. For example you can spin a Docker container to run a Postgres DB instance:
```
docker run -p 5432:5432 -d postgres
```
```
# .env
# You can adjust the values with your Postgres instance credentials.
DATABASE_NAME=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=
```
2. Prepare and activate a Python virtual environment
```
virtualenv env
source env/bin/activate
```
3. Install required packages
```
pip install -r requirements.txt
```
4. Do database migrations
```
python manage.py migrate
```
4. Run
```
python manage.py runserver
```
## Tech Stack
- Django Rest Framework
......
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = "accounts"
# Generated by Django 3.0.1 on 2020-03-03 15:02
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="Account",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("name", models.CharField(max_length=128)),
("email", models.EmailField(max_length=128)),
("phone_number", models.CharField(max_length=64)),
("area", models.CharField(max_length=128)),
("is_admin", models.BooleanField(default=False)),
("is_verified", models.BooleanField(default=False)),
("is_active", models.BooleanField(default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={"verbose_name_plural": "accounts", "db_table": "account",},
),
]
import uuid
from django.contrib.auth.models import User
from django.db import models
class Account(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=128)
email = models.EmailField(max_length=128)
phone_number = models.CharField(max_length=64)
area = models.CharField(max_length=128)
is_admin = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
class Meta:
db_table = "account"
verbose_name_plural = "accounts"
from rest_framework import serializers
from apps.accounts.models import Account
class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source="user.username", required=False)
class Meta:
model = Account
fields = [
"id",
"username",
"name",
"email",
"phone_number",
"area",
"is_admin",
"is_verified",
"is_active",
]
read_only_fields = [
"id",
"username",
]
class AccountRegisterSerializer(serializers.ModelSerializer):
username = serializers.CharField(max_length=128)
password = serializers.CharField(max_length=128)
class Meta:
model = Account
fields = [
"id",
"username",
"password",
"name",
"email",
"phone_number",
"area",
"is_admin",
"is_verified",
"is_active",
]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment