def validate(self, request=None, add_messages=False): valid = True if not validator.name(self.name): if add_messages: messages.error(request, "Du har glemt å skrive inn navnet ditt.") valid = False if not validator.address(self.address): if add_messages: messages.error(request, "Du må oppgi din egen adresse da vi sender faktura og medlemskort hjem til deg.") valid = False if not validator.zipcode(self.zipcode) or self.area == '': # Empty area defines invalid zipcode, as stated in __init__ if add_messages: messages.error(request, "Postnummeret ditt ser ikke riktig ut. Du må oppgi et gyldig postnummer da vi sender faktura og medlemskort hjem til deg.") valid = False if not validator.memberid(self.memberid, req=False): if add_messages: messages.error(request, "Medlemsnummeret ditt kan kun bestå av tall. Du trenger ikke være medlem for å bestille gavemedlemskap, da kan du la medlemsnummerfeltet stå tomt.") valid = False if not validator.phone(self.phone, req=False): if add_messages: messages.error(request, "Telefonnummeret ditt må være minst 8 siffer. Du trenger ikke oppgi telefonnummeret ditt, men vi anbefaler at du gir oss minst én måte å kontakte deg.") valid = False if not validator.email(self.email, req=False): if add_messages: messages.error(request, "E-postadressen din er ikke en gyldig adresse. Du trenger ikke oppgi e-postadressen din, men vi anbefaler at du gir oss minst én måte å kontakte deg.") valid = False return valid
def validate(self, request=None, add_messages=False): valid = True if self.type_index < 0 or self.type_index >= len(membership_types): if add_messages: messages.error(request, "Du har på en eller annen måte klart å angi en ugyldig medlemskapstype. Vennligst bruk select-boksen til å velge medlemskapstype.") valid = False if not validator.name(self.name): if add_messages: if len(self.name) > 0: messages.error(request, "Du må angi fullt navn til %s." % self.name) else: messages.error(request, "En av mottakerne mangler navn.") valid = False if not isinstance(self.dob, datetime): if add_messages: messages.error(request, "Fødselsdatoen til %s er ugyldig." % self.name) valid = False if isinstance(self.dob, datetime) and self.dob >= datetime.now(): if add_messages: messages.error(request, "Fødselsdatoen til %s kan ikke være i fremtiden." % self.name) valid = False if not validator.address(self.address): if add_messages: messages.error(request, "%s mangler adresse." % self.name) valid = False if not validator.zipcode(self.zipcode) or self.area == '': if add_messages: messages.error(request, "Postnummeret til %s er mangler eller er feil." % self.name) valid = False if not validator.phone(self.phone, req=False): if add_messages: messages.error(request, "Telefonnummeret til %s må bestå av minst 8 siffer." % self.name) valid = False if not validator.email(self.email, req=False): if add_messages: messages.error(request, "E-postadressen til %s er ikke gyldig." % self.name) valid = False return valid
def update_account(request): if not request.user.is_member(): if request.method == 'GET': context = { 'user_password_length': settings.USER_PASSWORD_LENGTH } return render(request, 'common/user/account/update_account_nonmember.html', context) elif request.method == 'POST': if not Settings.get_cached().focus_writes: return redirect('user:account') errors = False if not validator.email(request.POST['email']): messages.error(request, 'invalid_email_address') errors = True if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST and not validator.email(request.POST['sherpa-email'], req=False): messages.error(request, 'invalid_sherpa_email_address') errors = True if User.objects.filter(identifier=request.POST['email']).exclude(id=request.user.id).exists(): messages.error(request, 'duplicate_email_address') errors = True if errors: return redirect('user:update_account') if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST: user = request.user user.sherpa_email = request.POST['sherpa-email'] user.save() if 'phone_mobile' in request.POST: request.user.phone_mobile = request.POST['phone_mobile'] if all([key in request.POST for key in ['b_day', 'b_month', 'b_year']]): try: request.user.birth_date = datetime.strptime( "%s-%s-%s" % (request.POST['b_year'], request.POST['b_month'], request.POST['b_day']), "%Y-%m-%d", ).date() except ValueError: request.user.birth_date = None request.user.identifier = request.POST['email'] request.user.email = request.POST['email'] request.user.save() messages.info(request, 'update_success') return redirect('user:account') else: if request.method == 'GET': context = { 'address_field_max_length': ADDRESS_FIELD_MAX_LENGTH, 'settings': Settings.get_cached(), } return render(request, 'common/user/account/update_account.html', context) elif request.method == 'POST': errors = False if not validator.email(request.POST['email']): messages.error(request, 'invalid_email_address') errors = True if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST and not validator.email(request.POST['sherpa-email'], req=False): messages.error(request, 'invalid_sherpa_email_address') errors = True if not validator.phone(request.POST['phone_home'], req=False): messages.error(request, 'invalid_phone_home') errors = True if not validator.phone(request.POST['phone_mobile'], req=False): messages.error(request, 'invalid_phone_mobile') errors = True if request.user.address.country.code == 'NO' and not request.user.is_related_member(): if not validator.address(request.POST['address']): messages.error(request, 'invalid_address') errors = True if len(request.POST['address']) >= ADDRESS_FIELD_MAX_LENGTH: messages.error(request, 'too_long_address') errors = True try: zipcode = Zipcode.get_by_zipcode(zipcode=request.POST['zipcode']) except Zipcode.DoesNotExist: messages.error(request, 'invalid_zipcode') errors = True if errors: return redirect('user:update_account') if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST: user = request.user user.sherpa_email = request.POST['sherpa-email'] user.save() attributes = { 'email': request.POST['email'], 'phone_home': request.POST['phone_home'], 'phone_mobile': request.POST['phone_mobile'] } address_attributes = None if request.user.address.country.code == 'NO' and not request.user.is_related_member(): address_attributes = {} address_attributes['a1'] = request.POST['address'] if 'address2' in request.POST: address_attributes['a2'] = request.POST['address2'] if 'address3' in request.POST: address_attributes['a3'] = request.POST['address3'] address_attributes['zipcode_id'] = zipcode.zipcode address_attributes['area'] = zipcode.area request.user.set_contact_info(attributes, address_attributes, update_changedby=True) messages.info(request, 'update_success') return redirect('user:account')
def update_account(request): if not request.user.is_member(): if request.method == 'GET': context = { 'user_password_length': settings.USER_PASSWORD_LENGTH } return render(request, 'common/user/account/update_account_nonmember.html', context) elif request.method == 'POST': errors = False if not validator.email(request.POST['email']): messages.error(request, 'invalid_email_address') errors = True if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST and not validator.email(request.POST['sherpa-email'], req=False): messages.error(request, 'invalid_sherpa_email_address') errors = True if User.objects.filter(identifier=request.POST['email']).exclude(id=request.user.id).exists(): messages.error(request, 'duplicate_email_address') errors = True if errors: return redirect('user.views.update_account') if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST: user = request.user user.sherpa_email = request.POST['sherpa-email'] user.save() request.user.identifier = request.POST['email'] request.user.email = request.POST['email'] request.user.save() messages.info(request, 'update_success') return redirect('user.views.account') else: if request.method == 'GET': context = { 'address_field_max_length': ADDRESS_FIELD_MAX_LENGTH } return render(request, 'common/user/account/update_account.html', context) elif request.method == 'POST': errors = False if not validator.email(request.POST['email']): messages.error(request, 'invalid_email_address') errors = True if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST and not validator.email(request.POST['sherpa-email'], req=False): messages.error(request, 'invalid_sherpa_email_address') errors = True if not validator.phone(request.POST['phone_home'], req=False): messages.error(request, 'invalid_phone_home') errors = True if not validator.phone(request.POST['phone_mobile'], req=False): messages.error(request, 'invalid_phone_mobile') errors = True if request.user.get_address().country.code == 'NO' and not request.user.is_household_member(): if not validator.address(request.POST['address']): messages.error(request, 'invalid_address') errors = True if len(request.POST['address']) >= ADDRESS_FIELD_MAX_LENGTH: messages.error(request, 'too_long_address') errors = True try: zipcode = Zipcode.objects.get(zipcode=request.POST['zipcode']) except Zipcode.DoesNotExist: messages.error(request, 'invalid_zipcode') errors = True if errors: return redirect('user.views.update_account') if request.user.has_perm('sherpa') and 'sherpa-email' in request.POST: user = request.user user.sherpa_email = request.POST['sherpa-email'] user.save() attributes = { 'email': request.POST['email'], 'phone_home': request.POST['phone_home'], 'phone_mobile': request.POST['phone_mobile'] } address_attributes = None if request.user.get_address().country.code == 'NO' and not request.user.is_household_member(): address_attributes = {} address_attributes['a1'] = request.POST['address'] if 'address2' in request.POST: address_attributes['a2'] = request.POST['address2'] if 'address3' in request.POST: address_attributes['a3'] = request.POST['address3'] address_attributes['zipcode'] = zipcode.zipcode address_attributes['area'] = zipcode.area request.user.update_personal_data(attributes, address_attributes, update_changedby=True) messages.info(request, 'update_success') return redirect('user.views.account')