Last active
February 6, 2022 11:30
-
-
Save seanmavley/c9a4af36c2693d9b437a to your computer and use it in GitHub Desktop.
Allow user submit form once per day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The problem: | |
# A user has to be able to submit a form only once per day | |
# The solution - my approach | |
# A hidden form (connected to a kinda Control Model) is embedded in the normal form the user submits. | |
# Hidden form when submitted, triggers a save on a model ForeignKey'ed | |
# to the request.user | |
# Upon re-rendering form, Control Model is checked. If an object exist relating to user in context, | |
# no form shows in template | |
# forms.py | this checks if | |
# | user submitted form today | |
class FormLastActiveForm(forms.ModelForm): | |
class Meta: | |
model = FormLastActive | |
fields = '__all__' | |
# models.py | Showing only the Control Model | All Imports as usual | |
class FormLastActive(models.Model): | |
created_by = models.ForeignKey(User) | |
# views.py | All imports as usual | |
def homepage(request): | |
if request.method == 'POST': | |
# using form prefix: https://docs.djangoproject.com/en/1.8/ref/forms/api/#prefixes-for-forms | |
form1 = GhanaECGForm(request.POST, prefix='form1') | |
form2 = FormLastActiveForm(request.POST, prefix='form2') | |
if form1.is_valid() and form2.is_valid(): | |
form1.instance.created_by = request.user | |
form1.save() | |
# saves to control model | |
form2.instance.created_by = request.user | |
form2.save() | |
return HttpResponseRedirect('/ta/') | |
else: | |
form1 = GhanaECGForm(prefix='form1') | |
form2 = FormLastActiveForm(prefix='form2') | |
active = None | |
# SimpleLazy throws error when | |
# is_authenticated isn't checked first | |
if request.user.is_authenticated(): | |
is_active = FormLastActive.objects.filter(created_by=request.user).exists() | |
if is_active: | |
active = True | |
else: | |
active = False | |
return render(request, 'index.html', { | |
# 'all_hours': GhanaECG.get_total_hours(), | |
# 'today_hours': GhanaECG.get_today_hours(), | |
# 'past_week': GhanaECG.get_past_week_hours(), | |
# 'past_month': GhanaECG.get_past_month_hours(), | |
'ecg_form': form1, | |
'last_form': form2, | |
'no_form': active, | |
} | |
) | |
# index.html | Complex structure of html, but simplified here for brevity | |
{% if not no_form %} | |
{% if user.is_authenticated %} | |
<!-- Render Form --> | |
<form action="." method="POST" class="padding"> | |
{% csrf_token %} | |
{% form form=ecg_form %} {% endform %} | |
<!-- {% form form=last_form %} {% endform %} --> | |
<input type="submit" href="#" class="mdl-button mdl-button--colored mdl-button--raised" value="Submit" /> | |
</form> | |
{% else %} | |
Tell user to log in here | |
{% endif %} | |
{% else %} | |
Tell user (he/she)'s run out of form submission attempts | |
{% endif %} | |
# Other noticeable challenges with above approach: | |
# --> Allowing 2 form submissions per day will require some code tweaking (code not reusable easily) | |
# --> Database relating to Control Model will have to be wiped automatically, daily at midnight. | |
# --> Code is really smelling. Please help. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment