def form_valid(self, form): form.instance.user = self.request.user has_social = form.cleaned_data['has_social'] if has_social: form.instance.status = PROJECT_SOCIALFORM_REQUIRED self.object = form.save() bids = [ key.split('_')[1] for key in self.request.POST if key.startswith('item') ] bids.sort() for bid in bids: item = self.request.POST.get('item_' + bid, None) amount = self.request.POST.get('amount_' + bid, None) detail = self.request.POST.get('detail_' + bid, None) if not (item and amount): return HttpResponseForbidden() budget = Budget(item=item, amount=amount, detail=detail, project=self.object) budget.save() assign_perms(self.info_name, self.request.user, self.object, perms=['update', 'view']) assign_perms(self.info_name, self.object.workshop.group, self.object, perms=['update', 'view']) return super().form_valid(form)
def form_valid(self, form): site = form.cleaned_data['site'] activity_time_from = form.cleaned_data['activity_time_from'] activity_time_to = form.cleaned_data['activity_time_to'] conflict = is_conflict(activity_time_from, activity_time_to, site) if conflict: context = self.get_context_data() context['form'] = form html = render_to_string(self.template_name, request=self.request, context=context) return JsonResponse({ 'status': 2, 'reason': _('Conflict with existing ' 'reservation'), 'html': html }) form.instance.user = self.request.user self.object = form.save() assign_perms('reservation', self.request.user, self.object, perms=['update', 'view']) assign_perms('reservation', self.object.workshop.group, self.object, perms=['update', 'view']) return JsonResponse({'status': 0, 'redirect': self.success_url})
def create_teacher_info(num=10, prefix='teacher_'): teachers = [] for i in range(1, 1 + num): user = User.objects.create_user( username=prefix + str(i), password=str(i), first_name=str(i)) info = TeacherInfo( user=user, identity=USER_IDENTITY_TEACHER) info.save() assign_perms('teacherinfo', user, info) teachers.append(info) return teachers
def form_valid(self, form): username = form.cleaned_data['username'] first_name = form.cleaned_data['name'] password = form.cleaned_data['password'] email = form.cleaned_data['email'] user = User.objects.create_user(email=email, username=username, password=password, first_name=first_name) form.instance.user = user form.instance.identity = self.identity self.object = form.save() assign_perms(self.info_name, user, self.object, perms=['update', 'view']) return HttpResponseRedirect(self.get_success_url())
def post(self, *args, **kwargs): self.object = None formset = BillInfoMultiForm(self.request.POST) user = self.request.user if formset.is_valid(): bill_sheet = BillSheet.objects.create(user=user.user, count=0, amount=0, status=ESTATUS_SUBMIT) bill_infos = formset.save(commit=False) for bill_info in bill_infos: bill_info.sheet = bill_sheet bill_info.save() bill_sheet.save() assign_perms('billsheet', user, obj=bill_sheet) return redirect('bills_sheet_detail', permanent=True, uid=bill_sheet.uid) else: return self.form_invalid(formset)
def form_valid(self, form): username = form.cleaned_data['username'] first_name = form.cleaned_data['name'] password = form.cleaned_data['password'] email = form.cleaned_data['email'] user = User.objects.create_user(email=email, username=username, password=password, first_name=first_name) form.instance.user = user form.instance.identity = self.identity login(self.request, user, backend='django.contrib.auth.backends.ModelBackend') self.object = form.save() assign_perms(self.info_name, user, self.object, perms=['update', 'view']) # Assign perm to add Project and Reservation assign_perms('reservation', user, perms='add', app_name='SiteReservation') assign_perms('project', user, perms='add', app_name='ProjectApproval') return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): site = form.cleaned_data['site'] activity_time_from = form.cleaned_data['activity_time_from'] activity_time_to = form.cleaned_data['activity_time_to'] conflict = is_conflict(activity_time_from, activity_time_to, site) if conflict: context = self.get_context_data() context['form'] = form return HttpResponseForbidden() form.instance.user = self.request.user self.object = form.save() assign_perms('reservation', self.request.user, self.object, perms=['update', 'view']) assign_perms('reservation', self.object.workshop.group, self.object, perms=['update', 'view']) return HttpResponseRedirect(self.get_success_url())
def create_student_info(num=10, prefix='student_'): students = [] for i in range(1, 1 + num): user = User.objects.create_user( username=prefix + str(i), password=str(i), first_name=str(i)) info = StudentInfo( user=user, identity=USER_IDENTITY_STUDENT, phone=str(i), student_id=str(i)) info.save() assign_perms('studentinfo', user, info) assign_perms('reservation', user, perms='add', app_name='SiteReservation') assign_perms('project', user, perms='add', app_name='ProjectApproval') students.append(info) return students