diff --git a/.gitignore b/.gitignore index b5ce1bd..efa2a10 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -/frontend/node_modules \ No newline at end of file +/frontend/node_modules +/backend/webapp/db.sqlite3 +/backend/webapp/myvenv +/backend/webapp/media +*__pycache__ diff --git a/backend/webapp/README.md b/backend/webapp/README.md new file mode 100644 index 0000000..00636ec --- /dev/null +++ b/backend/webapp/README.md @@ -0,0 +1,41 @@ +## Analiza dyskusji na forum (Development) + +### Wymagania: + +- Python +- Django >= 3.0.5 + +### Środowisko wirtualne +Zaczynamy od stworzenia i uruchomienia środowiska wirtualnego, przy pomocy poleceń: +```bash +$ python3 -m venv myvenv +$ source myvenv/bin/activate +``` +Powinniśmy ujrzeć (myvenv) poprzedzające nasza nazwę użytkownika w wierszu poleceń. + +### Instalacja Django +Pierwszym krokiem będzie zaktualizowanie menedżera pakietów języka Python: +```bash +$ python3 -m pip install --upgrade pip +``` +Instalujemy pakietu Django przy pomocy listy wymagań: +```bash +pip install -r requirements.txt +``` + +### Uruchomienie + +Aby uruchomić wersję prototyp aplikacji, w pierwszej kolejności należy zainicjalizować bazę danych: +```bash +$ python manage.py migrate --run-syncdb +``` +Następnie uruchamiamy serwer WWW: +```bash +$ python manage.py runserver +``` +Aplikacja dostępna jest pod adresem `127.0.0.1:8000` +Aby zakończyć działanie serwera, korzystamy w kombinacji klawiszy CTRL + C w terminalu, w którym uruchomiliśmy usługę. + +Aby opuścić środowisko wirtualne, korzytamy z polecenia `deactivate`. + + diff --git a/backend/webapp/manage.py b/backend/webapp/manage.py new file mode 100755 index 0000000..c5a5e1e --- /dev/null +++ b/backend/webapp/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prototype.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() diff --git a/backend/webapp/prototype/__init__.py b/backend/webapp/prototype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/webapp/prototype/asgi.py b/backend/webapp/prototype/asgi.py new file mode 100644 index 0000000..8cd9c9b --- /dev/null +++ b/backend/webapp/prototype/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for prototype 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.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prototype.settings') + +application = get_asgi_application() diff --git a/backend/webapp/prototype/filehandler/__init__.py b/backend/webapp/prototype/filehandler/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/webapp/prototype/filehandler/apps.py b/backend/webapp/prototype/filehandler/apps.py new file mode 100644 index 0000000..33efe16 --- /dev/null +++ b/backend/webapp/prototype/filehandler/apps.py @@ -0,0 +1,6 @@ +from __future__ import unicode_literals + +from django.apps import AppConfig + +class CoreConfig(AppConfig): + name = 'filehandler' diff --git a/backend/webapp/prototype/filehandler/forms.py b/backend/webapp/prototype/filehandler/forms.py new file mode 100644 index 0000000..13a4c94 --- /dev/null +++ b/backend/webapp/prototype/filehandler/forms.py @@ -0,0 +1,7 @@ +from django import forms +from prototype.filehandler.models import Document + +class DocumentForm(forms.ModelForm): + class Meta: + model = Document + fields = ('description', 'document', ) diff --git a/backend/webapp/prototype/filehandler/models.py b/backend/webapp/prototype/filehandler/models.py new file mode 100644 index 0000000..928e2d0 --- /dev/null +++ b/backend/webapp/prototype/filehandler/models.py @@ -0,0 +1,7 @@ +from __future__ import unicode_literals +from django.db import models + +class Document(models.Model): + description = models.CharField(max_length=255, blank=True) + document = models.FileField(upload_to='documents/') + uploaded_at = models.DateTimeField(auto_now_add=True) diff --git a/backend/webapp/prototype/filehandler/templates/core/home.html b/backend/webapp/prototype/filehandler/templates/core/home.html new file mode 100644 index 0000000..9d273a9 --- /dev/null +++ b/backend/webapp/prototype/filehandler/templates/core/home.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} + +{% block content %} + + +

Wysłane pliki:

+ +{% endblock %} diff --git a/backend/webapp/prototype/filehandler/templates/core/model_form_upload.html b/backend/webapp/prototype/filehandler/templates/core/model_form_upload.html new file mode 100644 index 0000000..461745a --- /dev/null +++ b/backend/webapp/prototype/filehandler/templates/core/model_form_upload.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + +{% block content %} +
+ {% csrf_token %} + {{ form.as_p }} + +
+ +

Powrót do strony głównej

+{% endblock %} diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py new file mode 100644 index 0000000..050b3f2 --- /dev/null +++ b/backend/webapp/prototype/filehandler/views.py @@ -0,0 +1,22 @@ +from django.shortcuts import render, redirect +from django.conf import settings +from django.core.files.storage import FileSystemStorage + +from prototype.filehandler.models import Document +from prototype.filehandler.forms import DocumentForm + +def home(request): + documents = Document.objects.all() + return render(request, 'core/home.html', { 'documents': documents}) + +def model_form_upload(request): + if request.method == 'POST': + form = DocumentForm(request.POST, request.FILES) + if form.is_valid(): + form.save() + return redirect('home') + else: + form = DocumentForm() + return render(request, 'core/model_form_upload.html', { + 'form' : form + }) diff --git a/backend/webapp/prototype/settings.py b/backend/webapp/prototype/settings.py new file mode 100644 index 0000000..14c13d7 --- /dev/null +++ b/backend/webapp/prototype/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for prototype project. + +Generated by 'django-admin startproject' using Django 3.0.5. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'l@x1!0zws!($=!tz4n5f^p4z8o1j!r0wn8nz(jzg-k3i%b6(37' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['127.0.0.1'] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'prototype.filehandler', +] + +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 = 'prototype.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, 'prototype/templates')], + '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', + 'django.template.context_processors.media', + ], + }, + }, +] + +WSGI_APPLICATION = 'prototype.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/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', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'pl-pl' + +TIME_ZONE = 'Europe/Warsaw' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') + +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') diff --git a/backend/webapp/prototype/templates/base.html b/backend/webapp/prototype/templates/base.html new file mode 100644 index 0000000..06eb031 --- /dev/null +++ b/backend/webapp/prototype/templates/base.html @@ -0,0 +1,12 @@ + + + + + Prosty moduł przesyłania plików + + + + {% block content %} + {% endblock %} + + diff --git a/backend/webapp/prototype/urls.py b/backend/webapp/prototype/urls.py new file mode 100644 index 0000000..e1c7532 --- /dev/null +++ b/backend/webapp/prototype/urls.py @@ -0,0 +1,30 @@ +"""prototype URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/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 +from django.conf import settings +from django.conf.urls.static import static + +from prototype.filehandler import views + +urlpatterns = [ + path('', views.home, name='home'), + path('prototype/form/', views.model_form_upload, name='model_form_upload'), + path('admin/', admin.site.urls), +] + +if settings.DEBUG: + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/backend/webapp/prototype/wsgi.py b/backend/webapp/prototype/wsgi.py new file mode 100644 index 0000000..b8012a7 --- /dev/null +++ b/backend/webapp/prototype/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for prototype 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.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prototype.settings') + +application = get_wsgi_application() diff --git a/backend/webapp/requirements.txt b/backend/webapp/requirements.txt new file mode 100644 index 0000000..30a93cd --- /dev/null +++ b/backend/webapp/requirements.txt @@ -0,0 +1 @@ +Django~=3.0.5