Revert "tralala"
This reverts commit fd47f25c36a67948a038fbd0121fa2e0e885d584.
This commit is contained in:
parent
fd47f25c36
commit
ea223812cc
@ -5,32 +5,101 @@ from django.contrib.auth.decorators import login_required
|
|||||||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||||
from django.contrib.auth import views as auth_views, login
|
from django.contrib.auth import views as auth_views, login
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.db import Q, IntegrityError
|
from django.db.models import Q
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from .forms import CustomUserCreationForm, CustomUserChangeForm, PlayerCreationForm, PlayerVerificationForm, ParentVerificationForm
|
from .forms import CustomUserCreationForm, CustomUserChangeForm, PlayerCreationForm, PlayerVerificationForm, ParentVerificationForm
|
||||||
from .models import CustomUser
|
from .models import CustomUser
|
||||||
from .utils import send_verification_email
|
from .utils import send_verification_email
|
||||||
import uuid
|
import uuid
|
||||||
...
|
|
||||||
elif new_email:
|
@login_required
|
||||||
|
def edit_profile(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = CustomUserChangeForm(request.POST, instance=request.user)
|
||||||
|
if form.is_valid():
|
||||||
|
form.save()
|
||||||
|
return redirect('edit_profile') # Or wherever you want to redirect
|
||||||
|
else:
|
||||||
|
form = CustomUserChangeForm(instance=request.user)
|
||||||
|
return render(request, 'accounts/edit_profile.html', {'form': form})
|
||||||
|
|
||||||
|
class HeadCoachCheckMixin(UserPassesTestMixin):
|
||||||
|
def test_func(self):
|
||||||
|
return self.request.user.is_superuser or self.request.user.coached_teams.exists()
|
||||||
|
|
||||||
|
class PlayerCreateView(LoginRequiredMixin, HeadCoachCheckMixin, CreateView):
|
||||||
|
model = CustomUser
|
||||||
|
form_class = PlayerCreationForm
|
||||||
|
template_name = 'accounts/player_form.html'
|
||||||
|
success_url = reverse_lazy('dashboard')
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
# Create player user
|
||||||
|
player = form.save(commit=False)
|
||||||
|
player.is_active = False # Player is inactive until verified
|
||||||
|
player.set_unusable_password() # Password must be set via verification
|
||||||
|
player.verification_code = uuid.uuid4()
|
||||||
|
player.save()
|
||||||
|
|
||||||
|
# Send verification email to player
|
||||||
|
send_verification_email(player, self.request, is_parent=False)
|
||||||
|
|
||||||
|
# Handle parents
|
||||||
|
for i in ['1', '2']:
|
||||||
|
search_identifier = form.cleaned_data.get(f'parent{i}_search')
|
||||||
|
new_email = form.cleaned_data.get(f'parent{i}_new')
|
||||||
|
|
||||||
|
if search_identifier:
|
||||||
|
import re
|
||||||
|
match = re.search(r'\((\w+)\)', search_identifier)
|
||||||
|
if match:
|
||||||
|
username = match.group(1)
|
||||||
try:
|
try:
|
||||||
parent_user = CustomUser.objects.create(
|
parent_user = CustomUser.objects.get(username=username)
|
||||||
|
player.parents.add(parent_user)
|
||||||
|
except CustomUser.DoesNotExist:
|
||||||
|
form.add_error(f'parent{i}_search', 'User not found.')
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
parent_user = CustomUser.objects.get(Q(username=search_identifier) | Q(email=search_identifier))
|
||||||
|
player.parents.add(parent_user)
|
||||||
|
except CustomUser.DoesNotExist:
|
||||||
|
form.add_error(f'parent{i}_search', 'User not found.')
|
||||||
|
except CustomUser.MultipleObjectsReturned:
|
||||||
|
form.add_error(f'parent{i}_search', 'Multiple users found. Please be more specific.')
|
||||||
|
|
||||||
|
elif new_email:
|
||||||
|
parent_user, created = CustomUser.objects.get_or_create(
|
||||||
email=new_email,
|
email=new_email,
|
||||||
username=new_email,
|
defaults={'username': new_email, 'is_active': False}
|
||||||
is_active=False
|
|
||||||
)
|
)
|
||||||
|
if created:
|
||||||
parent_user.set_unusable_password()
|
parent_user.set_unusable_password()
|
||||||
parent_user.verification_code = uuid.uuid4()
|
parent_user.verification_code = uuid.uuid4()
|
||||||
parent_user.save()
|
parent_user.save()
|
||||||
# Send verification email to new parent
|
# Send verification email to new parent
|
||||||
send_verification_email(parent_user, self.request, is_parent=True)
|
send_verification_email(parent_user, self.request, is_parent=True)
|
||||||
player.parents.add(parent_user)
|
player.parents.add(parent_user)
|
||||||
except IntegrityError:
|
|
||||||
form.add_error(f'parent{i}_new', f"A user with the email '{new_email}' already exists. Please use the search field to add them.")
|
|
||||||
|
|
||||||
if form.errors:
|
if form.errors:
|
||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
...
|
|
||||||
|
return redirect(self.success_url)
|
||||||
|
|
||||||
|
class MyLoginView(auth_views.LoginView):
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
if request.user.is_authenticated:
|
||||||
|
return redirect('dashboard')
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def user_search(request):
|
||||||
|
q = request.GET.get('q', '')
|
||||||
|
users = CustomUser.objects.filter(last_name__istartswith=q).values('username', 'first_name', 'last_name')
|
||||||
|
results = []
|
||||||
|
for user in users:
|
||||||
|
results.append(f"{user['last_name']}, {user['first_name']} ({user['username']})")
|
||||||
|
return JsonResponse(results, safe=False)
|
||||||
|
|
||||||
def verify_account(request, verification_code):
|
def verify_account(request, verification_code):
|
||||||
user = get_object_or_404(CustomUser, verification_code=verification_code, is_verified=False)
|
user = get_object_or_404(CustomUser, verification_code=verification_code, is_verified=False)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user