diff --git a/registrasion/models.py b/registrasion/models.py index 1a8df463..2cb9ad63 100644 --- a/registrasion/models.py +++ b/registrasion/models.py @@ -21,6 +21,18 @@ class Attendee(models.Model): def __str__(self): return "%s" % self.user + @staticmethod + def get_instance(user): + ''' Returns the instance of attendee for the given user, or creates + a new one. ''' + attendees = Attendee.objects.filter(user=user) + if len(attendees) > 0: + return attendees[0] + else: + attendee = Attendee(user=user) + attendee.save() + return attendee + user = models.OneToOneField(User, on_delete=models.CASCADE) # Badge/profile is linked completed_registration = models.BooleanField(default=False) diff --git a/registrasion/urls.py b/registrasion/urls.py index ec0229e1..01946839 100644 --- a/registrasion/urls.py +++ b/registrasion/urls.py @@ -6,7 +6,7 @@ urlpatterns = patterns( url(r"^checkout$", "checkout", name="checkout"), url(r"^invoice/([0-9]+)$", "invoice", name="invoice"), url(r"^invoice/([0-9]+)/pay$", "pay_invoice", name="pay_invoice"), - url(r"^profile$", "profile", name="profile"), + url(r"^profile$", "edit_profile", name="profile"), url(r"^register$", "guided_registration", name="guided_registration"), url(r"^register/([0-9]+)$", "guided_registration", name="guided_registration"), ) diff --git a/registrasion/views.py b/registrasion/views.py index 976a3e59..5099e701 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -39,9 +39,20 @@ def guided_registration(request, page_id=0): return redirect("dashboard") @login_required -def profile(request): +def edit_profile(request): + attendee = rego.Attendee.get_instance(request.user) + + try: + profile = rego.BadgeAndProfile.objects.get(attendee=attendee) + except ObjectDoesNotExist: + profile = None + + form = forms.ProfileForm(request.POST or None, instance=profile) + + if request.POST and form.is_valid(): + form.instance.attendee = attendee + form.save() - form = forms.ProfileForm() data = { "form": form, }