Validating Entered Data with custom constraints in Django Forms in __init__ - python

In my project, the user is entering data in a settings page of the application and it should update the database with the user's settings preference. I read this answer by Alasdair and how using the __init__() will allow to access the user's details. I was wondering if it's possible to return data from __init__() so I can validate the entered data before calling the save() function in the view? This is what I tried (and did not work). I am open to going about this in a better approach so I appreciate all your suggestions!
**EDIT: ** I am deciding on moving the validation of the data entered by the user in the forms file because when I wrote it in my views, I ended up getting 5 if statements (nested). IMO, and given the situation of this application, moving it to the forms seems to be a cleaner approach. I also considered using the clean_['field'] but I need the side variable from the __init__() function to do that and I am not sure how to extract that value.
Forms.py
class t_max_form(forms.ModelForm):
side = None
def __init__(self, *args, **kwargs):
side = kwargs.pop('side', None)
super(t_max_form, self).__init__(*args,**kwargs)
if side == "ms":
self.fields['hs_max_sessions'].widget.attrs.update({'class':'form-control', 'readonly':True})
self.fields['ms_max_sessions'].widget.attrs.update({'class':'form-control mb-3'})
elif side == "hs":
self.fields['hs_max_sessions'].widget.attrs.update({'class':'form-control'})
self.fields['ms_max_sessions'].widget.attrs.update({'class':'form-control', 'readonly':True})
else:
self.fields['hs_max_sessions'].widget.attrs.update({'class':'form-control'})
self.fields['ms_max_sessions'].widget.attrs.update({'class':'form-control'})
#Trying to validate the data here
valid_session_count = settings.objects.first()
if(side == "ms"):
input_sessions = self.fields['ms_max_sessions'].widget.attrs['readonly']
if(input_sessions > valid_session_count.max_sessions):
self.add_error("ms_max_sessions", "You have entered more than the limit set by the TC. Try again")
elif(side == "hs"):
input_sessions = self.cleaned_data['hs_max_sessions']
if(input_sessions > valid_session_count.max_sessions):
self.add_error("hs_max_sessions", "You have entered more than the limit set by the TC. Try again")
else:
input_sessions = self.cleaned_data['ms_max_sessions'] + self.cleaned_data['hs_max_sessions']
if(input_sessions > valid_session_count.max_sessions):
self.add_error("hs_max_sessions", "You have entered more than the limit set by the TC. Try again")
return input_sessions
And this is what I was trying in my views
views.py
def t_time_slots(request):
name = request.user.username
t = t_info.objects.get(student__user__username = name)
timeSlots = tutor_time_slot.objects.filter(tutor = tutor).order_by('time')
side = decide_side(request.user)
if request.method == 'POST':
form = t_time_slot_form(request.POST)
if form.is_valid():
if check_unique_time_slot(request.user, form.cleaned_data['day'], form.cleaned_data['time']):
timeSlots = form.save(commit=False)
timeSlots.t = t
timeSlots.save()
messages.success(request, "Added Successfully")
return redirect('TTimeSlot')
else:
print("Overlap")
messages.error(request, "The time slot overlaps with a Current one. Please Change the time and try again.")
return redirect('TTimeSlot')
else:
form = t_time_slot_form()
context = {'timeSlots': timeSlots, 'form': form, 'side':side}
return render(request, 't/t_time_slot.html', context)

Set self.side in the __init__ method, then you can access it in the clean() or clean_<field> methods.
I would avoid putting validation code in the __init__ method.
class MyForm(forms.Form):
my_field = forms.CharField()
def __init__(self, *args, **kwargs):
self.side = kwargs.pop('side', None)
super(t_max_form, self).__init__(*args,**kwargs)
...
def clean_my_field(self):
my_field = self.cleaned_data['my_field']
# Use self.side to validate data
if my_field = self.side:
raise forms.ValidationError("Invalid")
return my_field

Related

Django form field update

I have a Django form that lets users choose a tag from multiple tag options. The problem I am facing is that even when the tag list gets updated, the model form does not get the updated tag list from database. As a result, new tags do not appear in options.
Here is my code in forms.py:
class EnglishTagForm(forms.Form):
tag_choices = [(x.tagName, x.tagName.upper()) for x in ClassTag.objects.filter(
agentId=Agent.objects.get(name='English Chowdhury'))]
tag = forms.CharField(widget=forms.Select(choices=tag_choices,
attrs={'class':'form-control'}))
def __init__(self, *args, **kwargs):
super(EnglishTagForm, self).__init__(*args, **kwargs)
self.fields['tag'].choices = [(x.tagName,
x.tagName.upper()) for x in ClassTag.objects.filter(
agentId=Agent.objects.get(name='English Chowdhury'))]
This form is being instantiated in view. My question is what changes should I do so that tag_choices gets updated from database on every instantiation.
How the above form is used in views.py:
```
def complaintDetail(request, complaint_id):
complaint = Complaints.objects.filter(pk=complaint_id).first()
context = {}
if request.method == 'POST':
agent = Agent.objects.get(name="English Chowdhury")
if "SubmitTag" in request.POST:
englishForm = EnglishTagForm(request.POST)
if englishForm.is_valid:
// Complaint Delete Logic
return redirect('chatbot:modComplaints')
else:
englishForm = EnglishTagForm()
context['eForm'] = englishForm
elif "SubmitBundle" in request.POST:
newTagForm = NewTagForm(request.POST)
if newTagForm.is_valid():
// Complaint Delete Logic
complaint.delete()
return redirect('chatbot:modComplaints')
else:
newTagForm = NewTagForm()
context['newForm'] = newTagForm
else:
englishForm = EnglishTagForm()
context['eForm'] = englishForm
newTagForm = NewTagForm()
context['newForm'] = newTagForm
context['complaint'] = complaint
return render(request, 'chatbot/complaintDetail.html', context)
```
Edit: (For future reference)
I decided to modify the tag attribute and convert CharField to ModelChoiceField, which seems to fix the issue.
Updated Class:
class EnglishTagForm(forms.Form):
tag = forms.ModelChoiceField(queryset=ClassTag.objects.filter(
agentId=Agent.objects.get(name='English Chowdhury')),
empty_label=None, widget=forms.Select(
attrs={'class':'form-control'}))
Please remove the list comprehension from Line 2. So that,
tag_choices = [(x.tagName, x.tagName.upper()) for x in ClassTag.objects.filter(
agentId=Agent.objects.get(name='English Chowdhury'))]
becomes
tag_choices = []

Django multiple forms on one view

I have a Django template that has data from a few different model types combining to make it. A dashboard if you will. And each of those has an edit form.
Is it best to process all those forms in one view as they are posted back to the same place and differentiating between them by a unique field like below?
Or if having lots of different dedicated avenues is the way forward? Thanks for any guidance
class ProjectDetail(DetailView):
template_name = 'project/view.html'
def get_object(self):
try:
return Project.objects.filter(brief__slug=self.kwargs['slug']).filter(team=get_user_team(self.request)).first()
# add loop to allow multiple teams to work on the same brief (project)
except Exception as e:
project_error = '%s (%s)' % (e.message, type(e))
messages.error(self.request, 'OH NO! %s' % project_error)
return redirect('home')
def get_context_data(self, **kwargs):
project = self.get_object()
context = dict()
context['project'] = project
context['milestone_form'] = MilestoneForm(initial={'project': project})
context['view'] = self
return context
def post(self, request, *args, **kwargs):
# get the context for the page
context = self.get_context_data()
try:
# switch for each of the form types on the team profile page (shown if member)
if 'milestone_form_submit' in request.POST:
project=self.get_object()
# set date arbitrarily to half way to brief deadline
brief = Brief.objects.get(project=project)
last_milestone = self.milestones().last()
milestone_del_date = last_milestone.del_date + timedelta(days=7)
new_milestone = Milestone(
project=project,
title_text=request.POST.get('title_text'),
del_date=milestone_del_date,
)
try:
new_milestone.save()
messages.success(self.request, "Excellent! New delivery popped on the bottom of the list")
except Exception as e:
# pass the erroring form back in the context if not
form = MilestoneForm(request.POST)
context['milestone_form'] = form
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
elif 'milestone-edit-date-form-submit' in request.POST:
# get object from db
milestone = Milestone.objects.get(pk=request.POST['id'])
# update del_date field sent
milestone.del_date = request.POST['del_date']
# save back to db
milestone.save()
messages.success(self.request, "Updated that delivery right there!")
elif ...
except Exception as e:
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
return render(request, self.template_name, context)
You can use mixins in order to solve your problem.
Example from the gist https://gist.github.com/michelts/1029336
class MultipleFormsMixin(FormMixin):
"""
A mixin that provides a way to show and handle several forms in a
request.
"""
form_classes = {} # set the form classes as a mapping
def get_form_classes(self):
return self.form_classes
def get_forms(self, form_classes):
return dict([(key, klass(**self.get_form_kwargs())) \
for key, klass in form_classes.items()])
def forms_valid(self, forms):
return super(MultipleFormsMixin, self).form_valid(forms)
def forms_invalid(self, forms):
return self.render_to_response(self.get_context_data(forms=forms))
As you can see, when you inherit from this class, you can handle multiple forms simultaneously. Look at the gist's code and adapt it to your problem.
Look at this answer

How do I assign specific users to a user-uploaded file so they can modify it/delete it (Django + Apache)

Im using django 1.10 + Apache in Linux.
I've created a small webapp to upload documents (with dropzone.js) and want to implement the ability for a user to specify who can view/modify/delete a specific file but i can't figure out a way how. I attempted using a ManyToManyField but maybe im not understading the Field itself correctly.
The "Document" model is this:
Model
class Document(models.Model):
file = models.FileField(upload_to = 'files/')
#validators=[validate_file_type])
uploaded_at = models.DateTimeField(auto_now_add = True)
extension = models.CharField(max_length = 30, blank = True)
thumbnail = models.ImageField(blank = True, null = True)
is_public = models.BooleanField(default = False)
accesible_by = models.ManyToManyField(User) #This is my attempt at doing this task.
def clean(self):
self.extension = self.file.name.split('/')[-1].split('.')[-1]
if self.extension == 'xlsx' or self.extension == 'xls':
self.thumbnail = 'xlsx.png'
elif self.extension == 'pptx' or self.extension == 'ppt':
self.thumbnail = 'pptx.png'
elif self.extension == 'docx' or self.extension == 'doc':
self.thumbnail = 'docx.png'
def delete(self, *args, **kwargs):
#delete file from /media/files
self.file.delete(save = False)
#call parent delete method.
super().delete(*args, **kwargs)
#Redirect to file list page.
def get_absolute_url(self):
return reverse('dashby-files:files')
def __str__(self):
return self.file.name.split('/')[-1]
class Meta():
ordering = ['-uploaded_at']
My View to handle the creation of documents:
View
class DocumentCreate(CreateView):
model = Document
fields = ['file', 'is_public']
def form_valid(self, form):
self.object = form.save(commit = False)
## I guess here i would Add the (self.request.user) to the accesible_by Field.
self.object.save()
data = {'status': 'success'}
response = JSONResponse(data, mimetype =
response_mimetype(self.request))
return response
Thanks in advance to anyone for any ideas or suggestions...
You have a model and a view that hopefully works for adding new documents, you still have a number of steps to go.
You'll need a place to assign users that can view/modify/delete your files. If you need to store access levels (view/delete...), your accessible_by will not suffice and you'll do well with a through table to add more information like access level.
You need to write views for various actions like view, delete... that users will request and here you ensure users have the right privileges. An implementation would be to get the request.user and the document id, look up if the user has the permission for what she's doing, return an http unauthorized exception or allow the action to proceed.
Edit: My question is about how can I assign user-permissions to each
individual file
If we're keeping this to access control from the django level, using the document model you already have, and you've taken some steps and for every document, you can assign users (accessible_by). Something like this can get you started:
from django.core.exceptions import PermissionDenied
def view_document(request, doc_pk):
doc = get_object_or_404(Document, pk=doc_pk)
if not doc.accessible_by.filter(username=request.user.username):
raise PermissionDenied
#perform rest of action
Or do you mean to use the permissions framework itself?

Partial Pipeline to ask user's phone number

I am creating a partial pipeline to get user's phone number on signup and skip the step on subsequent logins. My partial pipeline looks like this:
#partial
def other_info(strategy, details, user=None, is_new=False, *args, **kwargs):
if is_new or not details.get('email'):
request = kwargs['request']
return redirect('require_phone')
else:
return
On the page-welcome.html there is a form. Corresponding view looks like this:
def require_phone(request):
if request.method == 'POST':
phone = request.POST.get('phone',None)
user = User.objects.get(username = request.user.username)
if phone is not None:
up = UserProfile.objects.get_or_create(user=request.user,
phone = phone)
up.save()
backend = request.session['partial_pipeline']['backend']
return redirect('social:complete', backend=backend)
else:
return render(request,'app/page-welcome.html')
The problem is, the request object is not passed correctly to the view and hence the user shows anonymous. I am not able to access user object and hence cannot save phone no.
You might be able to get the user with
user = User.objects.get(id=request.session['partial_pipeline']['kwargs']['user'])
and then update this object, instead of trying to get it from the request (which will not work during the pipeline).

Error on django - .save() function

class Member(models.Model):# member db table
userID = models.CharField(max_length=80,primary_key=True) #user id
password = models.CharField(max_length=32)# password
nickname = models.CharField(max_length=100)# user nickname
penalty = models.PositiveSmallIntegerField(max_length=10,default=0,null=True)
participation=models.ForeignKey('Room',default=None,blank=True,null=True)
def __unicode__(self):
return self.userID
def doJoin(request):
if request.is_ajax() and request.method == 'POST':
# check validation
userID = request.POST['userID']
userNickname = request.POST['nickname']
if (checkID(userID) == False) and (checkNickname(userNickname) == False) :
#save to the database
newUser = Member()
newUser.userID = userID
newUser.nickname = userNickname
newUser.password = request.POST['password']
print newUser.userID , newUser.nickname , newUser.password , newUser.penalty , newUser.participation
newUser.save() #<------------error line!!!!
return HttpResponse('true')
else:
return HttpResponse('false')
else:
HttpResponse('false')
line about 8
In function doJoin:
newUser.save() # <--- error... so sad...
What should I do? Help me please.
What's wrong in this source?
Do you have debugging turned off? If you're getting a 500 and you have debugging turned on, you'll get a stack trace with the exception.
What are checkID() and checkNickname() doing? If those are performing some sort of validation, you really should be doing that in a form class instead of in the view. I also wouldn't be pulling values directly out of the request.POST to populate your model. I would highly recommend retrieving those values from a form's cleaned_data dictionary.

Categories

Resources