From 981e793e3e856eacdb4b0418c6fa42100319aa59 Mon Sep 17 00:00:00 2001 From: Sokurov Idar Date: Sat, 6 Feb 2021 14:33:55 +0300 Subject: [PATCH] Add profile page and temporary base.html --- access_controller/settings.py | 7 +---- access_controller/urls.py | 6 ++++ main/extra_func.py | 46 +++++++++++++++++++++++++++++++ main/templates/base/base.html | 35 +++++++++++++++++++++++ main/templates/pages/profile.html | 35 +++++++++++++++++++++++ main/views.py | 27 +++++++++++++++++- 6 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 main/extra_func.py create mode 100644 main/templates/base/base.html create mode 100644 main/templates/pages/profile.html diff --git a/access_controller/settings.py b/access_controller/settings.py index dc44189..91aed60 100644 --- a/access_controller/settings.py +++ b/access_controller/settings.py @@ -15,7 +15,6 @@ from pathlib import Path # 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.1/howto/deployment/checklist/ @@ -27,7 +26,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -72,7 +70,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'access_controller.wsgi.application' - # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases @@ -83,7 +80,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators @@ -102,7 +98,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - # Internationalization # https://docs.djangoproject.com/en/3.1/topics/i18n/ @@ -116,7 +111,6 @@ USE_L10N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ @@ -126,3 +120,4 @@ STATICFILES_DIRS = [ ] MEDIA_ROOT = BASE_DIR / 'media' +MEDIA_URL = '/media/' diff --git a/access_controller/urls.py b/access_controller/urls.py index e0ecf63..12b2b8c 100644 --- a/access_controller/urls.py +++ b/access_controller/urls.py @@ -13,9 +13,15 @@ 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.conf.urls.static import static from django.contrib import admin from django.urls import path +from access_controller import settings +from main.views import * + urlpatterns = [ path('admin/', admin.site.urls), + path('profile/', profile_page, name="Profile"), ] +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/main/extra_func.py b/main/extra_func.py new file mode 100644 index 0000000..a5ca8d2 --- /dev/null +++ b/main/extra_func.py @@ -0,0 +1,46 @@ +from main.models import UserProfile + + +# Дополнительные функции +def set_and_get_username(UP: UserProfile): # TODO: Переделать с получением данных через API + """ + Функция устанавливает поле :class:`user.username` текущим именем в Zendesk + + :param UP: Объект профиля пользователя + :type UP: :class:`UserProfile` + :return: Имя пользователя + """ + return UP.user.username + + +def set_and_get_email(UP: UserProfile): # TODO: Переделать с получением данных через API + """ + Функция устанавливает поле :class:`user.email` текущей почтой в Zendesk + + :param UP: Объект профиля пользователя + :type UP: :class:`UserProfile` + :return: Почта пользователя + """ + return UP.user.email + + +def set_and_get_role(UP: UserProfile): # TODO: Переделать с получением данных через API + """ + Функция устанавливает поле :class:`role` текущей ролью в Zendesk + + :param UP: Объект профиля пользователя + :type UP: :class:`UserProfile` + :return: Роль пользователя + """ + return UP.role + + +def load_and_get_image(UP: UserProfile): # TODO: Переделать с получением изображения через API + """ + Функция загружает и устанавливает изображение в поле :class:`image` + + :param UP: Объект профиля пользователя + :type UP: :class:`UserProfile` + :return: Название изображения + """ + return UP.image.name diff --git a/main/templates/base/base.html b/main/templates/base/base.html new file mode 100644 index 0000000..b53080c --- /dev/null +++ b/main/templates/base/base.html @@ -0,0 +1,35 @@ + + + + + {% block title %}{% endblock %} + + + + + +
+
+

+ {% block heading %}{% endblock %} +

+
+ +
+ {% block content %}{% endblock %} +
+
+ + + diff --git a/main/templates/pages/profile.html b/main/templates/pages/profile.html new file mode 100644 index 0000000..c6c1780 --- /dev/null +++ b/main/templates/pages/profile.html @@ -0,0 +1,35 @@ +{% extends 'base/base.html' %} +{% load static %} + +{% block title %} + {{ pagename }} +{% endblock %} + +{% block heading %} + Профиль +{% endblock %} + +{% block extra_css%} + .img{ + width:auto; + height:auto; + max-width:400px!important; + max-height:500px!important; + } +{% endblock %} +{% block content %} +
+
+
+
+ Нет изображения
+
+
+
+

Имя пользователя {{name}}

+

Электронная почта {{email}}

+

Текущая роль {{role}}

+
+
+
+{% endblock %} diff --git a/main/views.py b/main/views.py index 91ea44a..19fe76c 100644 --- a/main/views.py +++ b/main/views.py @@ -1,3 +1,28 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect + +from main.extra_func import set_and_get_username, set_and_get_email, load_and_get_image, set_and_get_role +from main.models import UserProfile + # Create your views here. + +def profile_page(request): + """ + Отображение страницы профиля + + :param request: объект с деталями запроса + :type request: :class:`django.http.HttpResponse + :return: объект ответа сервера с HTML-кодом внутри + """ + if request.user.is_authenticated: + UP = UserProfile.objects.get(user=request.user) + else: # TODO: Убрать после появления регистрации и авторизации, добавить login_required() + UP = UserProfile.objects.get(user=1) + context = { + 'name': set_and_get_username(UP), + 'email': set_and_get_email(UP), + 'role': set_and_get_role(UP), + 'image_name': load_and_get_image(UP), + 'pagename': 'Страница профиля' + } + return render(request, 'pages/profile.html', context)