Corp/backend/users/views.py
Игорь Надеин 6fb2cdcea0 feat: enhance Employee model and integrate Telegram/WhatsApp bot
- Add birth_date, created_at/updated_at to Employee; remove username field
- Introduce EmployeeAction for tracking HR events and is_actually_active/status properties
- Create BotSubscriber model (OneToOne relation) for TG/WA binding
- Implement Aiogram-based Telegram bot:
  • request contact to bind by phone or @username
  • department selection keyboard from DEPARTMENTS dict
  • async ORM via sync_to_async, custom filters for bound & active employees
- Update profile view & template:
  • show “Bind Telegram/WhatsApp” button only when not already bound
  • preview avatar on upload
- Remove registration tokens in favor of contact-based auth
- Clean up templates, forms and add avatar validation mixin
2025-07-30 08:15:53 +03:00

42 lines
1.2 KiB
Python

import os
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import ProfileUpdateForm
from bots.models import BotSubscriber
from dotenv import load_dotenv
load_dotenv()
USERNAME_TELEGRAM_BOT = os.getenv('USERNAME_TELEGRAM_BOT')
@login_required
def profile(request):
bot_sub, _ = BotSubscriber.objects.get_or_create(user=request.user)
if request.method == 'POST':
form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = ProfileUpdateForm(instance=request.user)
tg_link = f"https://t.me/{USERNAME_TELEGRAM_BOT}"
return render(request, 'profile.html', {
'form': form,
'tg_link': tg_link,
'bot_sub': bot_sub,
})
@login_required
def avatar_remove(request):
if request.method == 'POST':
user = request.user
if user.avatar:
user.avatar.delete(save=True)
messages.success(request, 'Аватар успешно удалён')
return redirect('profile')
return redirect('profile')