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

Создание простой формы регистрации #40

Merged
merged 10 commits into from
May 16, 2024
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ migrate:

# Создание новых миграций на основе сформированных моделей.
makemigrations:
cd $(PROJECT_DIR) && $(DJANGO_RUN) makemigrations --no-input
cd $(PROJECT_DIR) && $(DJANGO_RUN) makemigrations


# Создание супер-юзера.
Expand Down
34 changes: 24 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ poetry-plugin-export = "^1.7.1"
django-environ= "^0.11.2"
django-asgi-lifespan = "^0.3.1"
django-phonenumber-field = {extras = ["phonenumbers"], version = "^7.3.0"}
django-bootstrap5 = "^24.2"


[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/admin_user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.4 on 2024-05-11 21:07
# Generated by Django 5.0.6 on 2024-05-13 09:47

import django.utils.timezone
import phonenumber_field.modelfields
Expand Down
Empty file removed src/bot/migrations/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions src/core/config/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_bootstrap5',
]

LOCAL_APPS = [
Expand All @@ -53,6 +54,7 @@

ROOT_URLCONF = 'core.urls'


TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand Down
7 changes: 6 additions & 1 deletion src/core/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from django.contrib import admin
from django.urls import path
from django.urls import path, include


from core.views import send_greeting_email

urlpatterns = [
path('admin/', admin.site.urls),
path('registration/',
include('potential_user.urls'),
name='registration',
),
# TODO: убрать после реализации функционала
path('mail/', send_greeting_email, name='send-email'),
]
13 changes: 13 additions & 0 deletions src/potential_user/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django import forms

from potential_user.models import ApplicationForm


class RegistrationForm(forms.ModelForm):
"""Form to register a new user."""

class Meta:
"""Form meta class."""

model = ApplicationForm
fields = {'name', 'surname', 'phone_number', 'city', }
14 changes: 7 additions & 7 deletions src/potential_user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.4 on 2024-05-10 18:48
# Generated by Django 5.0.6 on 2024-05-16 08:59

import phonenumber_field.modelfields
from django.db import migrations, models
Expand All @@ -16,12 +16,12 @@ class Migration(migrations.Migration):
name='ApplicationForm',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('telegram_id', models.IntegerField(unique=True, verbose_name='Telegram ID')),
('role', models.CharField(choices=[('teacher', 'Преподаватель'), ('student', 'Учащийся')], max_length=20)),
('name', models.CharField(max_length=20)),
('surname', models.CharField(max_length=20)),
('city', models.CharField(max_length=20)),
('phone_number', phonenumber_field.modelfields.PhoneNumberField(max_length=128, region=None)),
('telegram_id', models.PositiveBigIntegerField(help_text='Введите свой ID', unique=True, verbose_name='Telegram ID')),
('role', models.CharField(choices=[('teacher', 'Преподаватель'), ('student', 'Учащийся')], max_length=20, verbose_name='Роль')),
('name', models.CharField(help_text='Обязательное поле', max_length=20, verbose_name='Имя')),
('surname', models.CharField(help_text='Обязательное поле', max_length=20, verbose_name='Фамилия')),
('city', models.CharField(max_length=20, verbose_name='Город')),
('phone_number', phonenumber_field.modelfields.PhoneNumberField(help_text='Формат +7XXXXXXXXXX', max_length=128, region=None, verbose_name='Номер телефона')),
('approved', models.BooleanField(default=False)),
],
options={
Expand Down
35 changes: 28 additions & 7 deletions src/potential_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,34 @@ class ApplicationForm(models.Model):
('teacher', 'Преподаватель'),
('student', 'Учащийся'),
)

telegram_id = models.IntegerField('Telegram ID', unique=True)
role = models.CharField(choices=ROLE_CHOICES, max_length=20)
name = models.CharField(max_length=20)
surname = models.CharField(max_length=20)
city = models.CharField(max_length=20)
phone_number = PhoneNumberField()
telegram_id = models.PositiveBigIntegerField(
'Telegram ID',
unique=True,
help_text='Введите свой ID',
)
role = models.CharField(
'Роль',
choices=ROLE_CHOICES,
max_length=20,
)
name = models.CharField(
'Имя',
max_length=20,
help_text='Обязательное поле',
)
surname = models.CharField(
'Фамилия',
max_length=20,
help_text='Обязательное поле',
)
city = models.CharField(
'Город',
max_length=20,
)
phone_number = PhoneNumberField(
'Номер телефона',
help_text='Формат +7XXXXXXXXXX',
)
approved = models.BooleanField(default=False)

class Meta:
Expand Down
11 changes: 11 additions & 0 deletions src/potential_user/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path

from potential_user.views import RegistrationCreateView

app_name = 'registration'

urlpatterns = [
path('',
RegistrationCreateView.as_view(),
name='registration'),
]
20 changes: 20 additions & 0 deletions src/potential_user/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random
import sys

from potential_user.models import ApplicationForm

#TODO Убрать после реализации хендлеров
def get_telegram_id() -> int:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Напиши еще #TODO Убрать после реализации хендлеров

"""
Получает телеграм id от бота.

Сейчас исполняет роль заглушки.
"""
while True:
telegram_id = random.randint(0, sys.maxsize)
tg_id_in_db = ApplicationForm.objects.filter(
telegram_id=telegram_id,
).exists()
if tg_id_in_db:
continue
return telegram_id
24 changes: 24 additions & 0 deletions src/potential_user/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.urls import reverse_lazy
from django.views.generic import CreateView

from potential_user.forms import RegistrationForm
from potential_user.models import ApplicationForm
from potential_user.utils import get_telegram_id


class RegistrationCreateView(CreateView):
"""Создает форму регистрации нового пользователя."""

model = ApplicationForm
form_class = RegistrationForm
template_name = 'registration/registration_form.html'

# TODO убрать после реализации получения telegram_id
def form_valid(self, form):
"""Присваивает telegram_id."""
form.instance.telegram_id = get_telegram_id()
return super().form_valid(form)

def get_success_url(self):
"""Переадресовывет на главную страницу."""
return reverse_lazy('registration:registration')
Empty file.
15 changes: 15 additions & 0 deletions src/templates/registration/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Телеграм-бот для преподавателей и учеников</title>
{% load django_bootstrap5 %}
{% bootstrap_css %}
</head>
<body>
{% include "registration/header.html" %}
<div class="container pt-5" align="center">
{% block content %}{% endblock %}
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions src/templates/registration/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load static %}
<footer class="footer">

</footer>
4 changes: 4 additions & 0 deletions src/templates/registration/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load static %}
<header class="header-login">
<h1 align="center">Телеграм-бот <br/> для преподавателей и учеников</h1>
</header>
16 changes: 16 additions & 0 deletions src/templates/registration/registration_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "registration/base.html" %}
{% load django_bootstrap5 %}
{% block content %}
<div class="card col-4">
<div class="card-header">
Регистрация пользователя
</div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_button button_type="submit" content="Регистрация" %}
</form>
</div>
</div>
{% endblock %}
Loading