bornhack-website/src/profiles/views.py

37 lines
1.3 KiB
Python
Raw Normal View History

2016-02-20 04:18:12 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView, UpdateView
2018-04-03 16:44:10 +00:00
from django.urls import reverse_lazy
2016-02-20 04:18:12 +00:00
from django.contrib import messages
2015-10-03 01:07:05 +00:00
2017-04-13 15:44:46 +00:00
from . import models
2016-02-20 04:18:12 +00:00
class ProfileDetail(LoginRequiredMixin, DetailView):
model = models.Profile
2019-06-16 12:32:24 +00:00
template_name = "profile_detail.html"
active_menu = "profile"
def get_object(self, queryset=None):
2016-02-20 04:18:12 +00:00
return models.Profile.objects.get(user=self.request.user)
class ProfileUpdate(LoginRequiredMixin, UpdateView):
model = models.Profile
2019-06-16 12:32:24 +00:00
fields = ["name", "description", "public_credit_name", "nickserv_username"]
success_url = reverse_lazy("profiles:detail")
template_name = "profile_form.html"
2016-02-20 04:18:12 +00:00
def get_object(self, queryset=None):
return models.Profile.objects.get(user=self.request.user)
def form_valid(self, form, **kwargs):
2019-06-16 12:32:24 +00:00
if (
"public_credit_name" in form.changed_data
and form.cleaned_data["public_credit_name"]
):
2017-07-11 20:50:31 +00:00
# user changed the name (to something non blank)
form.instance.public_credit_name_approved = False
form.instance.save()
2019-06-16 12:32:24 +00:00
messages.success(self.request, "Your profile has been updated.")
2017-07-11 20:50:31 +00:00
return super().form_valid(form, **kwargs)