def clean(self): if any(self.errors): # Don't bother validating the formset unless each form is valid on # its own return if len(self.forms) > DonationBidFormSetBase.max_bids: self.forms[0].errors['__all__'] = self.error_class( ["Error, cannot submit more than " + str(DonationBidFormSetBase.max_bids) + " bids."]) raise forms.ValidationError( "Error, cannot submit more than " + str(DonationBidFormSetBase.max_bids) + " bids.") sumAmount = Decimal('0.00') bids = set() for form in self.forms: if 'bid' in form.cleaned_data: if form.cleaned_data.get('amount', None): sumAmount += form.cleaned_data['amount'] if sumAmount > self.amount: form.errors['__all__'] = form.error_class( ["Error, total bid amount cannot exceed donation amount."]) raise forms.ValidationError( "Error, total bid amount cannot exceed donation amount.") if form.cleaned_data['bid'] in bids: form.errors['__all__'] = form.error_class( ["Error, cannot bid more than once for the same bid in the same donation."]) raise forms.ValidationError( "Error, cannot bid more than once for the same bid in the same donation.") bids.add(form.cleaned_data['bid'])
def clean(self): if not self.check_token(): raise forms.ValidationError('User token pair is not valid.') if 'password' in self.cleaned_data and 'passwordconfirm' in self.cleaned_data: if self.cleaned_data['password'] != self.cleaned_data['passwordconfirm']: raise forms.ValidationError('Passwords must match.') return self.cleaned_data
def clean(self): if any(self.errors): # Don't bother validating the formset unless each form is valid on # its own return if len(self.forms) > PrizeTicketFormSetBase.max_tickets: self.forms[0].errors['__all__'] = self.error_class( ["Error, cannot submit more than " + str(PrizeTicketFormSetBase.max_tickets) + " prize tickets per donation."]) raise forms.ValidationError("Error, cannot submit more than " + str( PrizeTicketFormSetBase.max_tickets) + " prize tickets.") sumAmount = Decimal('0.00') currentPrizes = set() for form in self.forms: if 'prize' in form.cleaned_data: if form.cleaned_data['prize'] in currentPrizes: form.errors['__all__'] = form.error_class( ["Error, cannot bid more than once for the same bid in the same donation."]) raise forms.ValidationError( "Error, cannot bid more than once for the same bid in the same donation.") if form.cleaned_data.get('amount', None): sumAmount += form.cleaned_data['amount'] if sumAmount > self.amount: form.errors['__all__'] = form.error_class( ["Error, total ticket amount cannot exceed donation amount."]) raise forms.ValidationError( "Error, total ticket amount cannot exceed donation amount.") currentPrizes.add(form.cleaned_data['prize'])
def get_user(self): AuthUser = get_user_model() email = self.cleaned_data['email'] userSet = AuthUser.objects.filter(email__iexact=email, is_active=True) if not userSet.exists(): raise forms.ValidationError( 'User with email {0} does not exist.'.format(email)) elif userSet.count() != 1: raise forms.ValidationError( 'More than one user has the e-mail {0}. Ideally this would be a db constraint, but django is stupid.'.format(email)) return userSet[0]
def clean_username(self): if 'username' in self.cleaned_data: username = self.cleaned_data['username'] if not re.match(r'^[a-zA-Z0-9_]+$', username): raise forms.ValidationError( _("Usernames can only contain letters, numbers, and the underscore")) if username[:10] == 'openiduser': raise forms.ValidationError( _("Username may not start with 'openiduser'")) if User.objects.filter(username=username).count() > 0: raise forms.ValidationError(_("Username already in use")) return self.cleaned_data['username']
def clean_prize(self): try: prize = self.cleaned_data['prize'] if not prize: prize = None else: prize = models.Prize.objects.get(id=prize) if prize.maxed_winners(): raise forms.ValidationError( "This prize has already been drawn.") except Exception as e: raise forms.ValidationError("Prize does not exist.") return prize
def clean_bid(self): try: bid = self.cleaned_data['bid'] if not bid: bid = None else: bid = models.Bid.objects.get(id=bid) if bid.state == 'CLOSED': raise forms.ValidationError( "This bid not open for new donations anymore.") except Exception as e: raise forms.ValidationError("Bid does not exist.") return bid
def impl_clean_run(self, data): if not data: return None try: return models.SpeedRun.objects.get(id=data) except: raise forms.ValidationError("Invalid Run id.")
def clean(self): if self.cleaned_data['requestedvisibility'] == 'ALIAS' and not self.cleaned_data['requestedalias']: raise forms.ValidationError( _("Must specify an alias with 'ALIAS' visibility")) if self.cleaned_data['requestedalias'] and self.cleaned_data['requestedalias'].lower() == 'anonymous': self.cleaned_data['requestedalias'] = '' self.cleaned_data['requestedvisibility'] = 'ANON' return self.cleaned_data
def clean_username(self): AuthUser = get_user_model() usersWithName = AuthUser.objects.filter( username__iexact=self.cleaned_data['username']) if not usersWithName.exists() or (usersWithName.count() == 1 and usersWithName[0] == self.user): return self.cleaned_data['username'] raise forms.ValidationError( 'Username {0} is already taken'.format(self.cleaned_data['username']))
def clean(self): if 'amount' not in self.cleaned_data: self.cleaned_data['amount'] = None if 'bid' not in self.cleaned_data: self.cleaned_data['bid'] = None if self.cleaned_data['amount'] and not self.cleaned_data['bid']: raise forms.ValidationError(_("Error, did not specify a bid")) if self.cleaned_data['bid'] and not self.cleaned_data['amount']: raise forms.ValidationError(_("Error, did not specify an amount")) if self.cleaned_data['bid']: if self.cleaned_data['bid'].allowuseroptions: if not self.cleaned_data['customoptionname']: raise forms.ValidationError( _('Error, did not specify a name for the custom option.')) elif self.cleaned_data['amount'] < Decimal('1.00'): raise forms.ValidationError( _('Error, you must bid at least one dollar for a custom bid.')) return self.cleaned_data
def save(self, commit=True): if self.user: self.user.username = self.cleaned_data['username'] self.user.set_password(self.cleaned_data['password']) self.user.is_active = True if commit == True: self.user.save() else: raise forms.ValidationError('Could not save user.') return self.user
def clean_amount(self): try: amount = self.cleaned_data['amount'] if not amount: amount = None else: amount = Decimal(amount) except Exception as e: raise forms.ValidationError('Could not parse amount.') return amount
def get_existing_user(self): AuthUser = get_user_model() email = self.cleaned_data['email'] userSet = AuthUser.objects.filter(email__iexact=email) if userSet.count() > 1: raise forms.ValidationError( 'More than one user has the e-mail {0}. Ideally this would be a db constraint, but django is stupid. Contact SMK to get this sorted out.'.format(email)) if userSet.exists(): return userSet[0] else: return None
def clean(self): cleaned_data = super().clean() errors = {} if self.bids: for bid in self.bids: # If user is entering a new option, both the amount and value must be present. if bid.allowuseroptions: amt_field = 'bid_amt_{}'.format(bid.id) opt_field = 'bid_new_option_name_{}'.format(bid.id) if self.cleaned_data.get(amt_field) and not self.cleaned_data.get(opt_field): errors[opt_field] = "{} -- {}: New value must be specified".format( bid.speedrun.name, bid.name) elif self.cleaned_data.get(opt_field) and not self.cleaned_data.get(amt_field): errors[amt_field] = "{} -- {}: Amount for new value must be specified".format( bid.speedrun.name, bid.name) # Make sure total bid amount doesn't exceed the donation amount. if self.amount is not None: bid_total = Decimal('0.00') for bid in self.bids: amt_field = 'bid_amt_{}'.format(bid.id) if (bid.allowuseroptions or bid.istarget) and self.cleaned_data.get(amt_field, None): bid_total += self.cleaned_data[amt_field] # Add amount fields for any bid war options. for option in bid.options.all(): option_amt_field = 'bid_amt_{}'.format(option.id) if option.istarget and self.cleaned_data.get(option_amt_field, None): bid_total += self.cleaned_data[option_amt_field] if bid_total > self.amount: errors[NON_FIELD_ERRORS] = forms.ValidationError("Total bid amount cannot exceed donation amount") if errors: raise forms.ValidationError(errors) return cleaned_data
def clean(self): if not self.cleaned_data['startrun']: self.cleaned_data['startrun'] = self.cleaned_data.get( 'endrun', None) if not self.cleaned_data['endrun']: self.cleaned_data['endrun'] = self.cleaned_data.get( 'startrun', None) if self.cleaned_data['startrun'] and self.cleaned_data['startrun'].starttime > self.cleaned_data['endrun'].starttime: self.errors['startrun'] = "Start run must be before the end run" self.errors['endrun'] = "Start run must be before the end run" raise forms.ValidationError( "Error, Start run must be before the end run") return self.cleaned_data
def clean_name(self): basename = self.cleaned_data['name'] prizes = models.Prize.objects.filter(name=basename) if not prizes.exists(): return basename name = basename count = 1 while prizes.exists(): count += 1 name = basename + ' ' + str(count) prizes = models.Prize.objects.filter(name=name) raise forms.ValidationError( 'Prize name taken. Suggestion: "{0}"'.format(name))
def clean(self): if self.accepted == False: self.cleaned_data['count'] = 0 self.cleaned_data['accept'] = False elif self.accepted == None: raise forms.ValidationError( 'The way you presented your decision was odd. Please make sure you click one of the two buttons.') else: if self.cleaned_data['count'] == 0: raise forms.ValidationError( 'You chose accept with 0 prizes. Perhaps you meant to click the other button? If you do not want any of your prizes, simply click the deny button.') self.cleaned_data['accept'] = True if self.instance.pendingcount < self.cleaned_data['total']: raise forms.ValidationError( 'There was a data inconsistency, please try again.') count = self.cleaned_data['count'] total = self.cleaned_data['total'] self.instance.acceptcount += count self.instance.declinecount += total - count self.instance.pendingcount -= total if self.cleaned_data['comments']: self.instance.winnernotes += self.cleaned_data['comments'] + '\n' return self.cleaned_data
def save(self, email_template=None, use_https=False, token_generator=default_token_generator, from_email=None, request=None, domain=None, **kwargs): if not email_template: email_template = auth.default_registration_template() user = self.get_existing_user() if user is None: email = self.cleaned_data['email'] username = email if len(username) > 30: username = email[:30] AuthUser = get_user_model() tries = 0 while user is None and tries < 5: try: user = AuthUser.objects.create( username=username, email=email, is_active=False) except django.db.utils.IntegrityError as e: tries += 1 username = tracker.util.random_num_replace(username, 8, max_length=30) if tries >= 5: raise forms.ValidationError('Something horrible happened, please try again') if domain is None: domain = viewutil.get_request_server_url(request) return auth.send_registration_mail(domain, user, template=email_template, sender=from_email, token_generator=token_generator)
def clean_count(self): count = int(self.cleaned_data['count']) if count > self.instance.pendingcount: raise forms.ValidationError('Error, count cannot exceed total') return count
def clean_total(self): if self.instance.pendingcount != self.cleaned_data['total']: raise forms.ValidationError( 'It seems something changed in your status since you loaded the page. Please review and try again.') return self.instance.pendingcount
def clean(self): if self.cleaned_data['amount'] and (not ('prize' in self.cleaned_data) or not self.cleaned_data['prize']): raise forms.ValidationError(_("Error, did not specify a prize")) if self.cleaned_data['prize'] and not self.cleaned_data['amount']: raise forms.ValidationError(_("Error, did not specify an amount")) return self.cleaned_data
def clean_password(self): if not self.cleaned_data['password']: raise forms.ValidationError('Password must not be blank.') return self.cleaned_data['password']
def clean_agreement(self): value = self.cleaned_data['agreement'] if not value: raise forms.ValidationError( "You must agree with this statement to submit a prize.") return value
def clean_email(self): user = self.get_existing_user() if user is not None and user.is_active: raise forms.ValidationError( 'This email is already registered. Please log in, (or reset your password if you forgot it).') return self.cleaned_data['email']