Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Api users creation #274

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compose/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,16 @@ services:
- ../envs/.env.db
depends_on:
- db
web_dashboard:
build: ../images/web_dashboard
volumes:
- ../data/web_bashboard-data:/var/lib/postgresql/data
# - ../images/web_dashboard/app/:/app
ports:
- 8000:8000
- 5433:5432
env_file:
- ../envs/.env.web_dashboard
depends_on:
- db
working_dir: /app
18 changes: 18 additions & 0 deletions envs/.env.web_dashboard.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Local DB setup
SECRET_KEY=foo
DJANGO_ALLOWED_HOSTS="localhost 127.0.0.1 [::1]"
DJANGO_SUPERUSER_USERNAME=admin
DJANGO_SUPERUSER_PASSWORD=1234
DJANGO_SUPERUSER_EMAIL="[email protected]"
## local Database
POSTGRES_HOST=localhost
POSTGRES_DB=dashboard
POSTGRES_USER=postgres
POSTGRES_PORT=5432
POSTGRES_PASSWORD=1234
## osm-api database
API_DB_POSTGRES_HOST=host.docker.internal
API_DB_POSTGRES_DB=openstreetmap
API_DB_POSTGRES_USER=postgres
API_DB_POSTGRES_PASSWORD=1234
API_DB_POSTGRES_PORT=5432
16 changes: 16 additions & 0 deletions images/web_dashboard/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM postgres:12
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN apt-get update -qq
RUN apt-get -y install \
build-essential libpq-dev \
python3-dev python3-pip curl sudo

WORKDIR /app
COPY app/requirements.txt /app/
RUN pip install -r requirements.txt
COPY app/ /app/
EXPOSE 5433
EXPOSE 8000
ENTRYPOINT ["sh","/app/start.sh"]
10 changes: 10 additions & 0 deletions images/web_dashboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Web dashbord
This container is for user management in the Web APi database.

- Development mode

```
docker-compose -f compose/web.yml up db
docker-compose -f compose/web.yml up web
docker-compose -f compose/web.yml run --service-ports web_dashboard bash
``
Empty file.
16 changes: 16 additions & 0 deletions images/web_dashboard/app/dashboard/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for dashboard project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dashboard.settings")

application = get_asgi_application()
148 changes: 148 additions & 0 deletions images/web_dashboard/app/dashboard/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"""
Django settings for dashboard project.

Generated by 'django-admin startproject' using Django 3.2.16.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY", "foo")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"user",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "dashboard.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "dashboard.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("POSTGRES_DB"),
"USER": os.environ.get("POSTGRES_USER"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
"HOST": os.environ.get("POSTGRES_HOST"),
"PORT": os.environ.get("POSTGRES_PORT"),
},
"osm_api": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("API_DB_POSTGRES_DB"),
"USER": os.environ.get("API_DB_POSTGRES_USER"),
"PASSWORD": os.environ.get("API_DB_POSTGRES_PASSWORD"),
"HOST": os.environ.get("API_DB_POSTGRES_HOST"),
"PORT": os.environ.get("API_DB_POSTGRES_PORT"),
},
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


DATABASE_ROUTERS = ("user.dbrouters.AccountsDBRouter",)

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = "/static/"

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

PASSWORD_HASHERS = [
"django.contrib.auth.hashers.Argon2PasswordHasher",
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
# 'django.contrib.auth.hashers.ScryptPasswordHasher',
]
23 changes: 23 additions & 0 deletions images/web_dashboard/app/dashboard/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""dashboard URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

admin.site.site_header = "OSM-Seed Administration"
urlpatterns = [
path("admin/", admin.site.urls),
path("", admin.site.login),
]
16 changes: 16 additions & 0 deletions images/web_dashboard/app/dashboard/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for dashboard project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dashboard.settings")

application = get_wsgi_application()
22 changes: 22 additions & 0 deletions images/web_dashboard/app/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dashboard.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions images/web_dashboard/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Django>=3.0,<4.0
psycopg2>=2.8
argon2-cffi==21.3.0
django-scrypt==0.2.3
django[argon2]
13 changes: 13 additions & 0 deletions images/web_dashboard/app/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -e
flag=true
/usr/local/bin/docker-entrypoint.sh "postgres" &
while "$flag" = true; do
pg_isready -h $POSTGRES_HOST -p $POSTGRES_PORT >/dev/null 2>&2 || continue
flag=false
echo "===================Start app======================="
python3 manage.py migrate
python3 manage.py createsuperuser --no-input
# python3 manage.py migrate --database=osm_api
python3 manage.py runserver 0.0.0.0:8000
done
Empty file.
11 changes: 11 additions & 0 deletions images/web_dashboard/app/user/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from .models import Users
from .forms import UsersForm

# Register your models here.
class UserAdmin(admin.ModelAdmin):
list_display = ("email", "id", "display_name", "status", "changesets_count")
form = UsersForm


admin.site.register(Users, UserAdmin)
6 changes: 6 additions & 0 deletions images/web_dashboard/app/user/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UserConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "user"
13 changes: 13 additions & 0 deletions images/web_dashboard/app/user/dbrouters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .models import Users


class AccountsDBRouter:
def db_for_read(self, model, **hints):
if model == Users:
return "osm_api"
return None

def db_for_write(self, model, **hints):
if model == Users:
return "osm_api"
return None
25 changes: 25 additions & 0 deletions images/web_dashboard/app/user/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .models import Users
from django import forms

USER_STATUS = [
("active", "Active"),
("pending", "Pendig"),
("confirmed", "Confirmed"),
("suspended", "Suspended"),
("deleted", "Deleted"),
]


class UsersForm(forms.ModelForm):
email = forms.EmailField(help_text="Enter a valid email address.")
pass_crypt = forms.CharField(
widget=forms.PasswordInput(
attrs={"class": "form-control", "placeholder": "please enter password"}
)
)
display_name = forms.CharField(label="User name", required=True)
status = forms.CharField(label="Status", widget=forms.Select(choices=USER_STATUS))

class Meta:
model = Users
fields = ["email", "display_name", "pass_crypt", "status"]
Loading