def save_artist_profile(self, profile, commit=True): """`profile` is an instance of UserProfile""" user = profile.user profile.is_artist = True profile.phone_number = self.cleaned_data['phone_number'] if 'has_opted_in' in self.fields: profile.has_opted_in = self.cleaned_data.get('has_opted_in', False) if commit: profile.save() user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] if commit: user.save() adr1 = self.cleaned_data.get('address1', '') city = self.cleaned_data.get('city', '') state = self.cleaned_data.get('state', '') postal_code = self.cleaned_data.get('postal_code', '') if adr1 and city and state and postal_code: adr = Address(user_profile=profile) adr.address1 = adr1 adr.address2 = self.cleaned_data.get('address2', '') adr.city = city adr.state = state adr.postal_code = postal_code if commit: adr.save() artist_profile = ArtistProfile(user_profile=profile) artist_profile.name = self.cleaned_data['name'] artist_profile.num_members = self.cleaned_data.get('num_members', 2) artist_profile.url = self.cleaned_data['url'] artist_profile.website = self.cleaned_data.get('website', None) if commit: artist_profile.save() artist_profile.genres = list(Genre.objects.filter(id__in=self.cleaned_data['genres'])) return artist_profile
def edit_address(request): page_name = 'editar perfil' form = AddressForm(request.POST or None) success_message = False user = request.user address = Address.objects.filter(user=user).first() if address != None: form.fields['zip_code'].initial = address.zip_code form.fields['street'].initial = address.street form.fields['number'].initial = address.number form.fields['complement'].initial = address.complement form.fields['district'].initial = address.district form.fields['city'].initial = address.city form.fields['state'].initial = address.state if request.method == 'POST' and form.is_valid(): if address is None: address = Address() address.zip_code = form.cleaned_data['zip_code'] address.street = form.cleaned_data['street'] address.number = form.cleaned_data['number'] address.complement = form.cleaned_data['complement'] address.district = form.cleaned_data['district'] address.city = form.cleaned_data['city'] address.state = form.cleaned_data['state'] address.user = user address.save() success_message = True return render(request, 'edit-address.html', {'form': form, 'page_name': page_name, 'success_message':success_message})
def save(self, commit=True): show_fields = self.show_fields if self.instance.is_sso: self.instance.send_reminders = False self.instance.send_favorites = False else: self.instance.send_reminders = self.cleaned_data.get( 'send_reminders', False) self.instance.send_favorites = self.cleaned_data.get( 'send_favorites', False) user_dirty = False if 'email' in show_fields: email = self.cleaned_data.get('email', '').lower() if email: self.instance.user.email = email self.instance.is_sso = False user_dirty = True if 'name' in show_fields: self.instance.user.username = self.cleaned_data['username'] self.instance.user.first_name = self.cleaned_data['first_name'] self.instance.user.last_name = self.cleaned_data['last_name'] if self.cleaned_data.get('has_opted_in', None) is not None: self.instance.has_opted_in = self.cleaned_data.get( 'has_opted_in', False) self.instance.permission = self.cleaned_data['permission'] user_dirty = True if user_dirty and commit: self.instance.user.save() if 'birth_date' in show_fields: self.instance.birth_date = self.cleaned_data['birth_date'] if 'phone_number' in show_fields: self.instance.phone_number = self.cleaned_data['phone_number'] if 'image' in show_fields: avatar_img = self.cleaned_data.get('image', None) if avatar_img: avatar_img_field = self.instance._meta.get_field( 'avatar_image') avatar_img_field.save_form_data(self.instance, avatar_img) if commit: self.instance.save() if 'image' in show_fields: self.instance._create_resized_images(raw_field=None, save=True) if 'address' in show_fields: adr = Address(user_profile=self.instance) adr.address1 = self.cleaned_data['address1'] adr.address2 = self.cleaned_data.get('address2', '') adr.city = self.cleaned_data['city'] adr.state = self.cleaned_data['state'] adr.postal_code = self.cleaned_data['postal_code'] #adr.country = self.cleaned_data['country'] if commit: adr.save() return self.instance
def save(self, commit=True): show_fields = self.show_fields if self.instance.is_sso: self.instance.send_reminders = False self.instance.send_favorites = False else: self.instance.send_reminders = self.cleaned_data.get('send_reminders', False) self.instance.send_favorites = self.cleaned_data.get('send_favorites', False) user_dirty = False if 'email' in show_fields: email = self.cleaned_data.get('email', '').lower() if email: self.instance.user.email = email self.instance.is_sso = False user_dirty = True if 'name' in show_fields: self.instance.user.username = self.cleaned_data['username'] self.instance.user.first_name = self.cleaned_data['first_name'] self.instance.user.last_name = self.cleaned_data['last_name'] if self.cleaned_data.get('has_opted_in', None) is not None: self.instance.has_opted_in = self.cleaned_data.get('has_opted_in', False) self.instance.permission = self.cleaned_data['permission'] user_dirty = True if user_dirty and commit: self.instance.user.save() if 'birth_date' in show_fields: self.instance.birth_date = self.cleaned_data['birth_date'] if 'phone_number' in show_fields: self.instance.phone_number = self.cleaned_data['phone_number'] if 'image' in show_fields: avatar_img = self.cleaned_data.get('image', None) if avatar_img: avatar_img_field = self.instance._meta.get_field('avatar_image') avatar_img_field.save_form_data(self.instance, avatar_img) if commit: self.instance.save() if 'image' in show_fields: self.instance._create_resized_images(raw_field=None, save=True) if 'address' in show_fields: adr = Address(user_profile=self.instance) adr.address1 = self.cleaned_data['address1'] adr.address2 = self.cleaned_data.get('address2', '') adr.city = self.cleaned_data['city'] adr.state = self.cleaned_data['state'] adr.postal_code = self.cleaned_data['postal_code'] #adr.country = self.cleaned_data['country'] if commit: adr.save() return self.instance
def save_artist_profile(self, profile, commit=True): """`profile` is an instance of UserProfile""" user = profile.user profile.is_artist = True profile.phone_number = self.cleaned_data['phone_number'] if 'has_opted_in' in self.fields: profile.has_opted_in = self.cleaned_data.get('has_opted_in', False) if commit: profile.save() user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] if commit: user.save() adr1 = self.cleaned_data.get('address1', '') city = self.cleaned_data.get('city', '') state = self.cleaned_data.get('state', '') postal_code = self.cleaned_data.get('postal_code', '') if adr1 and city and state and postal_code: adr = Address(user_profile=profile) adr.address1 = adr1 adr.address2 = self.cleaned_data.get('address2', '') adr.city = city adr.state = state adr.postal_code = postal_code if commit: adr.save() artist_profile = ArtistProfile(user_profile=profile) artist_profile.name = self.cleaned_data['name'] artist_profile.num_members = self.cleaned_data.get('num_members', 2) artist_profile.url = self.cleaned_data['url'] artist_profile.website = self.cleaned_data.get('website', None) if commit: artist_profile.save() artist_profile.genres = list( Genre.objects.filter(id__in=self.cleaned_data['genres'])) return artist_profile
def register_unapproved_stay(postdata): ''' When a calendar form is submitted, give the resulting request.POST data to this function. This function handles stripping the data down and validating/creating the resulting objects in the database. If the post data is validated successfully and the objects are created, then the returned dictionary will have a key value pair of 'success': True. If the validation is not successful or the objects are not created, then the returned dictionary will have a key value pair of 'success': False, and the errors will be reported in a list key value mapping of 'error_message': ['...', '...', ..] containing the list of error messages. ''' name = postdata['name'].strip() age = int(postdata['age'].strip()) phone = postdata['phone_contact_0'] + postdata['phone_contact_1'].strip() email = postdata['email_contact'].strip() num_guests = int(postdata['number_of_guests'].strip()) in_date = parser.parse(postdata['in_date'].split('(')[0].strip()) out_date = parser.parse(postdata['out_date'].split('(')[0].strip()) additional = postdata['additional_questions_or_concerns'] street = postdata['street_number'].strip() route = postdata['route'].strip() city = postdata['city'].strip() state = postdata['state'].strip() zip_code = postdata['zip_code'].strip() country = postdata['country'].strip() try: new_address = Address(street_number=street, route=route, city=city, state=state, zip_code=zip_code, country=country) new_address.save() existing_guest = Guest.objects.filter(phone_contact=phone)[0] guest = None if not existing_guest: guest = Guest(name=name, age=age, phone_contact=phone, email_contact=email, address=new_address) guest.save() else: existing_guest.name = name existing_guest.age = age existing_guest.email_contact = email existing_guest.address = new_address existing_guest.save() guest = existing_guest new_stay = Stay(guest=guest, in_date=in_date, out_date=out_date, number_of_guests=num_guests, additional_questions_or_concerns=additional) new_stay.full_clean() new_stay.save() result = {'success': True, 'stay': new_stay} except ValidationError as e: result = { 'success': False, 'error_source': 'registration.utils.register_unapproved_stay: Validation Failed', 'error_details': e.message_dict, } return result