Skip to content

Commit

Permalink
Merge pull request #235 from Studio-Yandex-Practicum/feature/create_m…
Browse files Browse the repository at this point in the history
…ain_coordinator

Добавил отметку главного координатора
  • Loading branch information
Zh-Andrew authored Aug 29, 2023
2 parents 1aacdf6 + 8af870f commit f468ff0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/bot/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CoordinatorAdmin(admin.ModelAdmin):
list_display = (
"first_name",
"last_name",
"is_chief",
"region",
"email_address",
"phone_number",
Expand Down
8 changes: 7 additions & 1 deletion src/bot/handlers/show_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
EDIT_MESSAGE_PARSE_MODE = "HTML"
FUND_PROGRAM_DOES_NOT_EXIST_ERROR_MESSAGE = "Program does not exists!"
QUESTION_DOES_NOT_EXIST_ERROR_MESSAGE = "Question does not exists!"
REGION_C = "Региональный координатор:"
CHIEF_C = "Главный координатор:"


@debug_logger(state=SHOW_CONTACT, run_functions_debug_loger="show_contact")
Expand All @@ -28,9 +30,13 @@ async def show_contact(
coordinator = await Coordinator.objects.filter(
region__region_key=context.user_data[States.REGION]
).afirst()
chief = await Coordinator.objects.filter(is_chief=True).afirst()
await query.answer()
await query.edit_message_text(
text=f"{coordinator!r}",
text=f"{CHIEF_C if chief and chief != coordinator else ''}\n"
f"{chief.__repr__() if chief and chief != coordinator else ''}\n"
f"\n{REGION_C if chief != coordinator else CHIEF_C}\n"
f"{coordinator!r}",
reply_markup=contact_show_keyboard_markup,
)
return States.SHOW_CONTACT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.4 on 2023-08-29 18:57

from django.db import migrations, models

import bot.validators


class Migration(migrations.Migration):
"""Migrations for bot."""

dependencies = [
("bot", "0003_alter_coordinator_first_name_and_more"),
]

operations = [
migrations.AlterModelOptions(
name="coordinator",
options={
"ordering": ("-is_chief", "last_name"),
"verbose_name": "Координатор",
"verbose_name_plural": "Координаторы",
},
),
migrations.AddField(
model_name="coordinator",
name="is_chief",
field=models.BooleanField(
default=False,
validators=[bot.validators.validate_is_chief],
verbose_name="Главный",
),
),
]
12 changes: 11 additions & 1 deletion src/bot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
format_telegram_link,
phone_regex,
telegram_regex,
validate_is_chief,
)
from core.models import BaseModel, Region

Expand Down Expand Up @@ -50,6 +51,9 @@ class Coordinator(BaseModel):
verbose_name="Telegram",
help_text="Введите телеграмм-аккаунт регионального координатора",
)
is_chief = models.BooleanField(
default=False, validators=[validate_is_chief], verbose_name="Главный"
)

def save(self, *args, **kwargs):
"""Check and save telegram_account and phone_number."""
Expand All @@ -71,10 +75,16 @@ def __repr__(self):
representation += f"Telegram: {self.telegram_account}"
return representation

def __str__(self):
return f"{self.first_name} {self.last_name}"

class Meta: # noqa
verbose_name = "Координатор"
verbose_name_plural = "Координаторы"
ordering = ("last_name",)
ordering = (
"-is_chief",
"last_name",
)


class HelpTypes(models.TextChoices):
Expand Down
8 changes: 8 additions & 0 deletions src/bot/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re

from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator

import bot.models as models
from bot.constants.validation import (
PHONE,
PHONE_PATTERN,
Expand Down Expand Up @@ -34,3 +36,9 @@ def format_phone_number(phone):
def format_telegram_link(telegram):
"""Format the telegram link."""
return "https://t.me/" + telegram


def validate_is_chief(value):
"""Validate that the Chief coordinator may be only one."""
if value and models.Coordinator.objects.filter(is_chief=True).exists():
raise ValidationError("Может быть только один Главный координатор!")

0 comments on commit f468ff0

Please sign in to comment.