python - Allow form submission only once a day django -
i want allow users submit django form once, , once everyday. after submitting form, form wouldn't show (server-side checkings, don't want use js or client side thing; 'tamper-able')
what best way in django?
what have tried?
i have not tried yet, however, i've considered options.
- trigger boolean field (where? on form user submit or his/her account?) change true, reset field false @ midnight
with approach, wonder how can reset field false @ midnight too.
i asked same question on django users, , couple of suggestions have come in, i'm looking well
its been while, since asked question, , figured way (bad way, maybe) done. here's how
#forms.py class formlastactiveform(forms.modelform): class meta: model = formlastactive fields = '__all__' # create model form 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) # explain in bit def homepage(request): schedule.run_pending() # explain in bit below if request.method == 'post': form1 = mymodelform(request.post, prefix='form1', user=request.user) form2 = formlastactiveform(request.post, prefix='form2') if form1.is_valid() , form2.is_valid(): form1.instance.created_by = request.user form1.save() form2.instance.created_by = request.user form2.save() return httpresponseredirect('/thanks/') else: form1 = ghanaecgform(prefix='form1') form2 = formlastactiveform(prefix='form2') active = none if request.user.is_authenticated(): form_is_active = formlastactive.objects.filter(created_by=request.user).exists() if is_active: active = true #if there exist record user in context else: active = false return render(request, 'index.html', { 'first_form': form1, 'second_form': form2, 'no_form': active, # triggers display of entire form or not } ) # index.html <form action="." method="post"> {% csrf_token %} {% form form=first_form %} {% endform %} # using material package form styling. use {{ first_form.as_p }} default styling. <!-- {% form form=second_form %} {% endform %} # part intentionally hidden user, , doesn't show in template --> <input type="submit" href="#" value="submit" /> </form>
upon submitting, hidden form , and visible form submitted, , processed in views.py above.
since users allowed submit 1 form per day, means there need means reset formlastactive model @ midnight, give every user fresh start. reset happens whenever first person makes request after 12 midnight every day.
here's how approach ties in:
# function resetting formlastactive model every midnight def reset_formlastactive(): '''resets database responsible allowing form submissions''' x = formlastactive.objects.all() x.delete() schedule.every().day.at("00:00").do(reset_formlastactive) # if noticed, came before `homepage()` function begun in views.py. set stage resetting everyday, @ 00:00 gmt.
to resetting, need call above function, , via:
schedule.run_pending()
as seen below homepage()
function in views.py
the run_pending()
used, if time reset passed, , homepage()
hasn't been run after 12 midnight, whenever happens, run_pending()
ensure every backlog resetting done.
i'm using approach @ logecg.khophi.co
i hope there's better, cleaner , reusable way of doing this, in mean time, works!.
i hope question asked doesn't deserve downvotes after all. wished django had inbuilt feature of types of forms.
Comments
Post a Comment