Home > database >  how to access specific choice in django choice field for conditional statements
how to access specific choice in django choice field for conditional statements

Time:07-30

I have an Account model which extends django's standard User model:

class Account(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    joined_groups = models.ManyToManyField(Group, related_name='joined_group', blank=True)
    EMAIL_PREFERENCES = [
        ('user_emails', 'User Emails'),
        ('group_emails', 'Group Emails'),
        ('leader_emails', 'Leader Emails'),
    ]
    email_preferences = MultiSelectField(
        verbose_name = 'Email Preferences',
        choices=EMAIL_PREFERENCES,
        blank=True,
        max_choices=3,
        max_length=255,
        default=['user_emails', 'group_emails', 'leader_emails']
    )

I also have many celery tasks that send email notifications for when Users create, update, delete, join, or leave Groups. However, I want these emails to only be sent if the User wants them to.

When a new User registers, their email preferences default to accepting all emails, but they have the ability to change their preferences in this view:

class EmailPreferencesUpdate(UpdateView):
    model = Account
    form_class = EmailPreferencesUpdateForm
    template_name = 'accounts/update_email_preferences.html'

    def form_valid(self, form):
        instance = form.save()
        email = instance.user.email
        update_email_preferences_notification.delay(email)
        return HttpResponseRedirect(reverse('user_detail', args=[str(instance.pk)]))

My issue is I'm trying to have conditionals, before I run my celery tasks, to see if the User allows this type of email, but I can't figure out how to access the User's choices to see if that specific choice was selected.

For example, I have a UserUpdate view:

class UserUpdate(generic.UpdateView):
    model = User
    form_class = UserUpdateForm
    template_name = 'accounts/update_user.html'

    def get_object(self):
        return self.request.user

    def form_valid(self, form):
        instance = form.save()
        user = self.request.user
        form.update_user_notification()
        return HttpResponseRedirect(reverse('user_detail', args=[str(instance.pk)]))

and I want to add an if statement when the form is validated to check email preferences of said User. I'm assuming it's something like this:

    def form_valid(self, form):
        instance = form.save()
        user = self.request.user
        if user.account.email_preferences.includes('user_emails')
            form.update_user_notification()
        return HttpResponseRedirect(reverse('user_detail', args=[str(instance.pk)]))

But that didn't work. I'm just not sure how to access the choices field to see if a User has a specific choice selected.

CodePudding user response:

It actually wasn't so complicated. All I have to do is see if the type of email exists in the User's choices:

    def form_valid(self, form):
        instance = form.save()
        user = self.request.user
        if 'user_emails' in user.account.email_preferences:
            form.update_user_notification()
        return HttpResponseRedirect(reverse('user_detail', args=[str(instance.pk)]))

and this worked. We'll see if it works for actually updating the email notification settings of a User, because I'm not sure what runs first, the celery task or updating of email preferences.

  • Related