def clean(self): super(BaseLeaveFormSetOffline, self).clean() curr_year = timezone.now().year data = self.cleaned_data leave_counts = LeavesCount.objects.filter( user=User.objects.get(username=data['leave_user_select']), year=curr_year) mapping = dict() for form in self.forms: try: data = form.cleaned_data leave_type = LeaveType.objects.get(id=data.get('leave_type')) #if leave_type.is_station: # continue count = get_leave_days(data.get('start_date'), data.get('end_date'), leave_type, data.get('start_half'), data.get('end_half')) if leave_type in mapping.keys(): mapping[leave_type] += count else: mapping[leave_type] = count except: raise VE('Some error occured, please contact admin.') for key, value in mapping.items(): tp = leave_counts.get(leave_type=key) if tp.remaining_leaves < value: raise VE( f'There are only {tp.remaining_leaves} {tp.leave_type.name} ' f'Leaves remaining and you have filled {value}.')
def clean(self): cleaned_data = super(ChartForm, self).clean() layer = cleaned_data.get('layer') category = cleaned_data.get('category') quantity = cleaned_data.get('quantity') aggr_type = cleaned_data.get('aggr_type') categories, quantities = get_fields(layer) if category not in categories: lyr = Layer.objects.get(pk=layer) raise VE( _('%(lyr_title)s does not contain the field %(category)s'), code='non_extant_category_field', params={ 'lyr_title': lyr.title, 'category': category }) if quantity not in quantities and aggr_type != 2: lyr = Layer.objects.get(pk=layer) raise VE( _('%(lyr_title)s does not contain the field %(quantity)s'), code='non_extant_quantity_field', params={ 'lyr_title': lyr.title, 'quantity': quantity })
def clean(self): super(EmployeeCommonFormOffline, self).clean() data = self.cleaned_data if not data.get('purpose'): raise VE({'purpose': ['Please provide purpose of the leave.']}) elif not data.get('application_date'): raise VE({ 'application_date': ['Please provide the application date of the leave.'] }) return self.cleaned_data
def clean(self): super(AcademicReplacementFormOffline, self).clean() data = self.cleaned_data errors = dict() start_date, end_date = data['acad_start_date'], data['acad_end_date'] if start_date > end_date: errors['acad_start_date'] = [ 'Academic Start Date must not be more than End Date' ] """now = timezone.now().date() if data['acad_start_date'] < now: error = 'You have inserted past date.' if 'acad_start_date' in errors: errors['acad_start_date'].append(error) else: errors['acad_start_date'] = error if data['acad_end_date'] < now: error = 'You have inserted past date.' if 'acad_end_date' in errors: errors['acad_end_date'].append(error) else: errors['acad_end_date'] = error""" if errors.keys(): raise VE(errors) return self.cleaned_data
def clean(self, *args, **kwargs): super(StudentApplicationForm, self).clean(*args, **kwargs) data = self.cleaned_data errors = dict() today = timezone.now().date() """if data.get('start_date') < today: errors['start_date'] = ['Past Dates are not allowed'] if data.get('end_date') < today: errors['end_date'] = ['Past Dates are not allowed'] """ lt = LeaveType.objects.filter(name=data.get('leave_type')).first() if lt.requires_proof and not data.get('document'): errors['document'] = [f'{lt.name} Leave requires document proof'] if data.get('start_date') > data.get('end_date'): if 'start_date' in errors: errors['start_date'].append( 'Start Date must be less than End Date') else: errors['start_date'] = [ 'Start Date must be less than End Date' ] leave_type = LeaveType.objects.get(name=data.get('leave_type')) count = get_leave_days(data.get('start_date'), data.get('end_date'), leave_type, False, False) remaining_leaves = LeavesCount.objects.get(user=self.user, leave_type=leave_type) \ .remaining_leaves if remaining_leaves < count: errors['leave_type'] = f'You have only {remaining_leaves} {leave_type.name} leaves' \ ' remaining.' raise VE(errors)
def clean(self): super(BaseLeaveFormSet, self).clean() curr_year = timezone.now().year leave_counts = LeavesCount.objects.filter(user=self.user, year=curr_year) mapping = dict() for form in self.forms: # if form.is_valid(): try: data = form.cleaned_data leave_type = data.get('leave_type') count = (data.get('end_date') - data.get('start_date')).days + 1 if data.get('start_half'): count -= 0.5 if data.get('end_half'): count -= 0.5 if leave_type in mapping.keys(): mapping[leave_type] += count else: mapping[leave_type] = count except TypeError: pass for key, value in mapping.items(): tp = leave_counts.get(leave_type__id=key) if tp.remaining_leaves < value: raise VE( f'There are only {tp.remaining_leaves} {tp.leave_type.name} ' f'Leaves remaining and you have filled {value}.')
def clean(self): super(EmployeeCommonForm, self).clean() data = self.cleaned_data #if data.get('is_station') and not data.get('leave_info'): #if not data.get('leave_info'): # raise VE({'leave_info': ['If there is a station leave, provide details about it.']}) if not data.get('purpose'): raise VE({'purpose': ['Please provide purpose of the leave.']}) return self.cleaned_data
def clean(self): data = self.cleaned_data start_date, end_date = data['admin_start_date'], data['admin_end_date'] if start_date > end_date: raise VE({ 'admin_start_date': ['Start Date must not be more than End Date'] }) rep_user = User.objects.get(username=data['admin_rep']) if LeaveSegment.objects.filter( Q(leave__applicant=rep_user), ~Q(leave__status='rejected'), Q(start_date__range=[start_date, end_date]) | Q(end_date__range=[start_date, end_date])).exists(): raise VE({'admin_rep': ['User may be on leave in this period.']}) return self.cleaned_data
def clean(self): super(AdminReplacementForm, self).clean() data = self.cleaned_data start_date, end_date = data['admin_start_date'], data['admin_end_date'] errors = dict() if start_date > end_date: errors['admin_start_date'] = [ 'Start Date must not be more than End Date' ] now = timezone.localtime(timezone.now()).date() if data['admin_start_date'] < now: error = 'You have inserted past date.' if 'admin_start_date' in errors: errors['admin_start_date'].append(error) else: errors['admin_start_date'] = error if data['admin_end_date'] < now: error = 'You have inserted past date.' if 'admin_end_date' in errors: errors['admin_end_date'].append(error) else: errors['admin_end_date'] = error rep_user = User.objects.get(username=data['admin_rep']) if LeaveSegment.objects.filter( Q(leave__applicant=rep_user), ~Q(leave__status='rejected'), Q(start_date__range=[start_date, end_date]) | Q(end_date__range=[start_date, end_date])).exists(): errors['admin_rep'] = [ f'{rep_user.get_full_name()} may be on leave in this period.' ] if errors.keys(): raise VE(errors) return self.cleaned_data
def clean(self, *args, **kwargs): super(LeaveSegmentForm, self).clean(*args, **kwargs) data = self.cleaned_data errors = dict() if data['start_date'] < data['end_date']: pass elif data['start_date'] == data['end_date']: if data['start_half'] and data['end_half']: errors['start_half'] = ['Invalid Input'] errors['end_half'] = ['Invalid Input'] else: errors['start_date'] = [ 'Start date must not be more than End date.' ] now = timezone.localtime(timezone.now()).date() if data['start_date'] < now: error = 'You have inserted past date in Start Date Field' if 'start_date' in errors: errors['start_date'].append(error) else: errors['start_date'] = error if data['end_date'] < now: error = 'You have inserted past date in End Date Field' if 'end_date' in errors: errors['end_date'].append(error) else: errors['end_date'] = error leave_type = LeaveType.objects.filter(id=data['leave_type']).first() if leave_type and leave_type.requires_proof and not data.get( 'document'): errors['document'] = [ f'{leave_type.name} requires a document for proof.' ] if errors.keys(): raise VE(errors) return self.cleaned_data
def clean(self, *args, **kwargs): super(LeaveSegmentFormOffline, self).clean(*args, **kwargs) data = self.cleaned_data errors = dict() def check_special_leave_overlap(start_date, end_date, leave_type_id): leave_type = LeaveType.objects.get(id=leave_type_id) if leave_type.name.lower() in ['restricted']: count = get_special_leave_count(start_date, end_date, leave_type.name.lower()) if count < 0: return 'The period for this leave doesn\'t match with Restricted holiday calendar' \ '. Check Academic Calendar.' elif leave_type.name.lower() in ['vacation']: count = get_vacation_leave_count(start_date, end_date, leave_type.name.lower()) if count < 0: return 'The period for this leave doesn\'t match with Vacation holidays' \ '. Check Academic Calendar.' return '' if data['start_date'] < data['end_date']: error = check_special_leave_overlap(data.get('start_date'), data.get('end_date'), data.get('leave_type')) if error: if 'leave_type' in errors: errors['leave_type'].append(error) else: errors['leave_type'] = [ error, ] elif data['start_date'] == data['end_date']: if data['start_half'] and data['end_half']: errors['start_half'] = ['Invalid Input'] errors['end_half'] = ['Invalid Input'] else: error = check_special_leave_overlap(data.get('start_date'), data.get('end_date'), data.get('leave_type')) if error: if 'leave_type' in errors: errors['leave_type'].append(error) else: errors['leave_type'] = [ error, ] else: errors['start_date'] = [ 'Start date must not be more than End date.' ] leave_type = LeaveType.objects.filter(id=data['leave_type']).first() if leave_type and leave_type.requires_proof and not data.get( 'document'): errors['document'] = [ f'{leave_type.name} requires a document for proof.' ] leave_type = LeaveType.objects.filter(id=data['leave_type']).first() if leave_type and leave_type.requires_address and not data.get( 'address'): errors['address'] = [ f'{leave_type.name} requires Out of Station address.' ] if errors.keys(): raise VE(errors) return self.cleaned_data
def clean(self, *args, **kwargs): super(LeaveSegmentForm, self).clean(*args, **kwargs) data = self.cleaned_data errors = dict() def check_special_leave_overlap(start_date, end_date, leave_type_id): leave_type = LeaveType.objects.get(id=leave_type_id) if leave_type.name.lower() in ['restricted']: count = get_special_leave_count(start_date, end_date, leave_type.name.lower()) if count < 0: return 'The period for this leave doesn\'t match with restricted holiday calendar' \ '. Check Academic Calendar.' elif leave_type.name.lower() in ['vacation']: count = get_vacation_leave_count(start_date, end_date, leave_type.name.lower()) if count < 0: return 'The period for this leave doesn\'t match with vacation holidays' \ '. Check Academic Calendar.' return '' if data['start_date'] < data['end_date']: error = check_special_leave_overlap(data.get('start_date'), data.get('end_date'), data.get('leave_type')) if error: if 'leave_type' in errors: errors['leave_type'].append(error) else: errors['leave_type'] = [ error, ] elif data['start_date'] == data['end_date']: #if data.get('leave_type')==2: # restricted_holidays=list(res.date for res in RestrictedHoliday.objects.all()) # print(restricted_holidays) # print(data.get('start_date')) if data['start_half'] and data['end_half']: errors['start_half'] = ['Invalid Input'] errors['end_half'] = ['Invalid Input'] else: error = check_special_leave_overlap(data.get('start_date'), data.get('end_date'), data.get('leave_type')) if error: if 'leave_type' in errors: errors['leave_type'].append(error) else: errors['leave_type'] = [ error, ] else: errors['start_date'] = [ 'Start date must not be more than End date.' ] #now = timezone.now().date() """if data['start_date'] < now: error = 'You have inserted past date in Start Date Field' if 'start_date' in errors: errors['start_date'].append(error) else: errors['start_date'] = error if data['end_date'] < now: error = 'You have inserted past date in End Date Field' if 'end_date' in errors: errors['end_date'].append(error) else: errors['end_date'] = error """ leave_type = LeaveType.objects.filter(id=data['leave_type']).first() if leave_type and leave_type.requires_proof and not data.get( 'document'): errors['document'] = [ f'{leave_type.name} requires a document for proof.' ] #leave_type = LeaveType.objects.filter(id=data['leave_type']).first() if leave_type and leave_type.requires_address and not data.get( 'address'): errors['address'] = [ f'{leave_type.name} requires Out of Station address.' ] if errors.keys(): raise VE(errors) return self.cleaned_data
def validate(password): if len(password) < n: raise VE(_("It must contain at least %d characters.") % n)