Allow form submission only once a day django

2024/11/16 7:28:59

I want to allow users to submit a django form once, and only once everyday. After submitting the form, the form wouldn't even show (server-side checkings, I don't want to use JS or client side thing; easily 'tamper-able')

What is the best way to do so in Django?

What I have tried?

I have not tried any yet, however, I've considered this options.

  • Trigger a boolean field (where? on the form user submit or his/her account?) to change to True, then reset that field to False at midnight

With this approach, I wonder how I can reset the field to False at midnight too.

I asked the same question on Django Users, and a couple of suggestions have come in, so I'm looking into as well

Answer

Its been a while, since I asked this question, and I figured a way (bad way, maybe) to get it done. Here's how

#forms.py
class FormLastActiveForm(forms.ModelForm):class Meta:model = FormLastActivefields = '__all__'# Create a model form for MyModel model below here#models.py
class FormLastActive(models.Model):created_by = models.ForeignKey(User, blank=True, null=True)class MyModel(models.Model):name = models.CharField(max_length=300)remark = models.CharField(max_length=300)#views.py
schedule.every().day.at("00:00").do(reset_formlastactive) # will explain this in a bit too
def homepage(request):schedule.run_pending() # will explain this in a bit belowif request.method == 'POST':form1 = MyModelForm(request.POST, prefix='form1', user=request.user)form2 = FormLastActiveForm(request.POST, prefix='form2')if form1.is_valid() and form2.is_valid():form1.instance.created_by = request.userform1.save()form2.instance.created_by = request.userform2.save()return HttpResponseRedirect('/thanks/')else:form1 = GhanaECGForm(prefix='form1')form2 = FormLastActiveForm(prefix='form2')active = Noneif request.user.is_authenticated():form_is_active = FormLastActive.objects.filter(created_by=request.user).exists()if is_active:active = True #if there exist a record for user in contextelse:active = Falsereturn render(request, 'index.html', {'first_form': form1,'second_form': form2,'no_form': active, # this triggers display of entire form or not})# index.html
<form action="." method="POST">{% csrf_token %} {% form form=first_form %} {% endform %} # Using material package for form styling. Could use {{ first_form.as_p }} too for default styling.<!-- {% form form=second_form %} {% endform %} # This part is intentionally hidden to the user, and doesn't show up in the template --><input type="submit" href="#" value="Submit" />
</form>

Upon submitting, the hidden form and and the visible form all get submitted, and are processed in the views.py above.

Since users are allowed to submit one form per day, then it means there need to be a means to reset the FormLastActive model at midnight, to give every user a fresh start. This reset for all happens whenever the first person makes a request after 12 midnight every day.

Here's how that approach ties in:

# the function for resetting FormLastActive Model every midnight
def reset_formlastactive():'''Resets the database responsible forallowing form submissions'''x = FormLastActive.objects.all()x.delete()schedule.every().day.at("00:00").do(reset_formlastactive)
# if you noticed, this came just before the `homepage()` function begun in the views.py. This set the stage for resetting everyday, at 00:00 GMT.

To actually do the resetting, we need to call the above function, and we do so via:

schedule.run_pending()

as seen just below the homepage() function in the views.py

The run_pending() is used, so that even if the time to reset is passed, and the homepage() hasn't been run after 12 midnight, whenever that happens, the run_pending() will ensure every backlog resetting is done.

I'm currently using this approach at logecg.khophi.co

I hope there's a better, cleaner and reusable way of doing this, but in the mean time, this works!.

I hope to this question I asked doesn't deserve the downvotes after all. Wished django had an inbuilt feature of this for all types of forms.

https://en.xdnf.cn/q/120349.html

Related Q&A

Counting percentage of element occurence from an attribute in a class. Python

I have a class called transaction that have these attributes Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type, location])an example of the data set is as sucht…

AWS | Syntax error in module: invalid syntax

I have created python script which is uploaded as a zip file in AWS Lambda function with stompy libraries bundled in them.Logs for python 2.7:-Response: nullRequest ID: "c334839f-ee46-11e8-8970-61…

Algorithm for finding if an array is balanced [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Merging two dataframes in python pandas [duplicate]

This question already has answers here:Pandas Merging 101(8 answers)Closed 5 years ago.I have a dataframe A:a 1 a 2 b 1 b 2Another dataframe B:a 3 a 4 b 3I want my result dataframe to be like a 1 3 a …

Searching for the best fit price for multiple customers [duplicate]

This question already has an answer here:Comparing multiple price options for many customers algorithmically(1 answer)Closed 10 years ago.A restatement of Comparing multiple price options for many cust…

Can we chain the ternary operator in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

evaluate a python string expression using dictionary values

I am parsing a text file which contain python "string" inside it. For e.g.:my_home1 in houses.split(,) and 2018 in iphone.split(,) and 14 < maskfor the example above, I wrote a possible di…

How to simply get the master volume of Windows in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.Improve …

Python: Adding positive values in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python IM Program [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…