def post(self, request, *args, **kwargs): if self.valid: Profile.mark_valid_emails([self.profile.user.email]) else: Profile.mark_invalid_emails([self.profile.user.email]) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name, context={'view': self})
def post(self, request, *args, **kwargs): if self.valid: Profile.mark_valid_emails([self.user.email]) else: Profile.mark_invalid_emails([self.user.email]) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name)
def post(self, request, *args, **kwargs): # Spare the extra trip to the database to fetch the User object associated # with the profile, just to retrieve the email address in that record. email = User.objects.filter( pk=self.profile.user_id).values_list('email') if self.valid: Profile.mark_valid_emails(email) else: Profile.mark_invalid_emails(email) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name, context={'view': self})
def get_owner(self, object): try: return self.user.profile except Profile.DoesNotExist: # For users without a profile, we must create a dummy one, because AuthMixin # expects all owners to be instances of Profile (which is not unreasonable). return Profile(user=copy(self.user))
def get_success_url(self, *args, **kwargs): try: if hasattr(self.object, self.object._meta.get_field('profile').get_cache_name()): profile = self.object.profile else: profile = Profile.get_basic_data(user=self.object) return profile.get_edit_url() except Profile.DoesNotExist: return reverse_lazy('profile_create')
def get_authenticated_redirect_url(self): redirect_to = sanitize_next(self.request) if redirect_to: return redirect_to try: # When user is already authenticated, redirect to profile's page. profile = Profile.get_basic_data(user=self.request.user) return profile.get_absolute_url() except Profile.DoesNotExist: # If profile does not exist yet, redirect to home. return reverse_lazy('home')
def get_authenticated_redirect_url(self): redirect_to = sanitize_next(self.request) if redirect_to: return redirect_to try: # When user is already authenticated, redirect to profile edit page. profile = Profile.get_basic_data(user=self.request.user) return profile.get_edit_url() except Profile.DoesNotExist: # If profile does not exist yet, redirect to profile creation page. return self.success_url
def get(self, request, *args, **kwargs): try: profile = Profile.get_basic_data(user=request.user) return HttpResponseRedirect( reverse_lazy('profile_settings', kwargs={ 'pk': profile.pk, 'slug': profile.autoslug })) except Profile.DoesNotExist: # Cache the result for the reverse related descriptor, to spare further DB queries. setattr(request.user, request.user._meta.fields_map['profile'].get_cache_name(), None) return super().get(request, *args, **kwargs)
def get(self, request, *args, **kwargs): self.object = self.get_object() if not request.user.is_active: return HttpResponseRedirect(self.get_success_url()) else: try: profile = Profile.get_basic_data(user=request.user) except Profile.DoesNotExist: return super().get(request, *args, **kwargs) else: return HttpResponseRedirect( reverse_lazy('profile_delete', kwargs={ 'pk': profile.pk, 'slug': profile.autoslug }))
def get(self, request, *args, **kwargs): try: profile = Profile.get_basic_data(user=request.user) settings_url = reverse_lazy('profile_settings', kwargs={ 'pk': profile.pk, 'slug': profile.autoslug }) except Profile.DoesNotExist: settings_url = reverse_lazy('account_settings') return HttpResponseRedirect( format_lazy( '{settings_url}#{section_email}', settings_url=settings_url, section_email=pgettext_lazy("URL", "email-addr"), ))
def test_mark_identical_emails(self): # Having two profiles with the same public email address is # permitted. The `mark_invalid_emails` and `mark_valid_emails` # methods are expected to update both profile objects. profile_two = ProfileSansAccountFactory(with_email=True) Profile.all_objects.filter(pk=self.profile.pk).update( email=profile_two.email) email = profile_two.email invalid_email = f'{settings.INVALID_PREFIX}{email}' result = Profile.mark_invalid_emails([email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email, profile_two.email) self.assertTrue(self.profile.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(result[Profile], 2) # When one of the profiles has its email already marked as # invalid, only the other one is expected to be updated. Profile.all_objects.filter(pk=self.profile.pk).update(email=email) result = Profile.mark_invalid_emails([email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertTrue(self.profile.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 1) self.assertTrue(profile_two.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(profile_two.email.count(settings.INVALID_PREFIX), 1) self.assertEqual(result[Profile], 1) # Repeating the operation is not expected to update anything. result = Profile.mark_invalid_emails([email]) self.assertEqual(result[Profile], 0) # Both profile objects' emails are expected to have the # marker prefix removed. result = Profile.mark_valid_emails([invalid_email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email, profile_two.email) self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(result[Profile], 2) # When one of the profiles has its email already marked as # valid, only the other one is expected to be updated. Profile.all_objects.filter(pk=self.profile.pk).update( email=invalid_email) result = Profile.mark_valid_emails([invalid_email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(profile_two.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(result[Profile], 1) # Repeating the operation is not expected to update anything. result = Profile.mark_valid_emails([email]) self.assertEqual(result[Profile], 0)
def action(self, file_name, emails): results = Profile.mark_valid_emails(emails=emails) for model, updated in results.items(): print(updated, model.__name__, "emails marked as valid from", file_name)
def migrate(): # Connect to an existing database passwd = getpass("MySQL password for 'root': ") dr = mdb.connect('localhost', 'root', passwd, 'pasportaservo', charset='utf8') users = dr.cursor(mdb.cursors.DictCursor) users.execute(""" SELECT *, GROUP_CONCAT(DISTINCT content_field_kondicxoj.field_kondicxoj_value) conditions, GROUP_CONCAT(DISTINCT content_field_telefono1estas.field_telefono1estas_value) tel1_type, GROUP_CONCAT(DISTINCT content_field_telefono2estas.field_telefono2estas_value) tel2_type FROM users u INNER JOIN node n ON n.uid=u.uid AND n.type='profilo' INNER JOIN content_type_profilo p ON p.nid=n.nid LEFT JOIN location_instance ON u.uid = location_instance.uid LEFT JOIN location ON location_instance.lid = location.lid LEFT JOIN content_field_kondicxoj ON n.nid = content_field_kondicxoj.nid LEFT JOIN content_field_telefono1estas ON n.nid = content_field_telefono1estas.nid LEFT JOIN content_field_telefono2estas ON n.nid = content_field_telefono2estas.nid WHERE u.uid > 1 AND u.name <> 'testuser' AND ( (field_lando_value = 'Albanio' AND field_urbo_value is not NULL) OR (field_lando_value <> 'Albanio')) AND field_familia_nomo_value <> 12 GROUP BY u.uid """) user = users.fetchone() from django.contrib.auth.models import User from hosting.utils import title_with_particule from hosting.models import Profile, Place, Phone, Condition, Website django.setup() # Starting... print('Ignoring:') # Condition objects sleeping_bag = Condition( name=_("Sleeping bag"), abbr=_("sleeping bag"), slug="sleeping-bag", ) sleeping_bag.save() one_room = Condition( name=_("One room"), abbr=_("one room"), slug="one-room", ) one_room.save() dont_smoke = Condition( name=_("Don't smoke"), abbr=_("dont smoke"), slug="dont-smoke", ) dont_smoke.save() while user is not None: # User new_user = User(id=user['uid'], password=user['pass'], last_login=u2dt(user['login']), is_superuser=False, username=g.get_username(user['name'], user['mail']), email=user['mail'], is_staff=False, is_active=True, date_joined=u2dt(user['created'])) new_user.save() # Profile new_profile = Profile( id=user['uid'], user=new_user, title=g.get_title(user['field_sekso_value']), first_name=title_with_particule(user['field_persona_nomo_value']), last_name=title_with_particule(user['field_familia_nomo_value']), birth_date=g.get_birth_date(user['field_naskijaro_value']), description=user['field_pri_mi_value'], avatar=g.get_avatar(user['picture']), ) new_profile.save() # Place address = user['field_adreso_value'] details = user['field_detaloj_kaj_rimarkoj_value'] closest_city = user['field_proksima_granda_urbo_value'] city = user['field_urbo_value'] country = COUNTRIES.get(user['field_lando_value'], '') state_province = g.get_state_province( user['field_sxtato_provinco_value'], country) if address or user['latitude']: new_place = Place( id=user['uid'], owner=new_profile, address='' if not address else address.strip(), description='' if not details else details.strip(), city='' if not city else title_with_particule(city.strip()), closest_city='' if not closest_city else title_with_particule( closest_city.strip()), postcode=g.get_postcode( user['field_posxtokodo_logxadreso_value']), country=country, state_province=state_province, latitude=float(user['latitude']) if user['latitude'] else None, longitude=float(user['longitude']) if user['longitude'] else None, max_host=g.get_int_or_none( user['field_maksimuma_gastoj_value']), max_night=g.get_int_or_none( user['field_maksimuma_noktoj_value']), available=g.get_truefalse( user['field_mi_pretas_gastigi_value']), tour_guide=g.get_truefalse( user['field_mi_pretas_cxicxeroni_value']), have_a_drink=g.get_truefalse( user['field_mi_pretas_cxicxeroni_value']), in_book=g.get_in_book( user['field_mia_profilo_videbla_en_la_value']), contact_before=g.get_int_or_none( user['field_bonvolu_kontakti_min_value']), ) new_place.save() new_profile.places.add(new_place) # Conditions raw_conditions = user['conditions'] if raw_conditions: if "Kunportu dormosakon" in raw_conditions: new_place.conditions.add(sleeping_bag) if "Ne fumu" in raw_conditions: new_place.conditions.add(dont_smoke) if "ambra lo" in raw_conditions: new_place.conditions.add(one_room) # Phone number raw_number = user['field_telefono1_value'] number = g.get_phone_number(raw_number, country) if raw_number else '' if number: new_number = Phone( profile=new_profile, number=number, country=country if country else PHONE_CODES.get( number.country_code, ''), type=g.get_phone_type(user['tel1_type']), ) new_number.save() # Phone number 2 raw_number2 = user['field_telefono2_value'] number2 = g.get_phone_number(raw_number2, country) if raw_number2 else '' if number2: new_number2 = Phone( profile=new_profile, number=number2, country=country if country else PHONE_CODES.get( number2.country_code, ''), type=g.get_phone_type(user['tel2_type']), ) new_number.save() # Websites websites = g.get_websites(user['field_mia_ttt_pagxo_value']) for website in websites: new_website = Website( profile=new_profile, url=website, ) new_website.save() user = users.fetchone() users.close() dr.close() print(r'Success! \o/')
def migrate(): # Connect to an existing database passwd = getpass("MySQL password for 'root': ") dr = mdb.connect('localhost', 'root', passwd, 'pasportaservo', charset='utf8') users = dr.cursor(mdb.cursors.DictCursor) users.execute(""" SELECT u.uid id, field_nomo_kaj_familia_nomo_1_value name1, field_naskijaro_persono_1_value year1, field_sekso_persono_1_value sex1, field_nomo_kaj_familia_nomo_2_value name2, field_naskijaro_persono_2_value year2, field_sekso_persono_1_value sex2, field_nomo_kaj_familia_nomo_3_value name3, field_naskijaro_persono_3_value year3 -- There is no field_sekso_persono_3_value in the source database FROM users u INNER JOIN node n ON n.uid=u.uid AND n.type='profilo' INNER JOIN content_type_profilo p ON p.nid=n.nid WHERE u.uid > 1 AND u.name <> 'testuser' AND ( (field_lando_value = 'Albanio' AND field_urbo_value is not NULL) OR (field_lando_value <> 'Albanio')) AND field_familia_nomo_value <> 12 GROUP BY u.uid """) user = users.fetchone() from django.contrib.auth.models import User from hosting.utils import title_with_particule from hosting.models import Profile, Place django.setup() # Starting... print('Ignoring:') while user is not None: data1 = { 'first_name': title_with_particule(user['name1']), 'birth_date': g.get_birth_date(user['year1']), 'title': g.get_title(user['sex1']) } data2 = { 'first_name': title_with_particule(user['name2']), 'birth_date': g.get_birth_date(user['year2']), 'title': g.get_title(user['sex2']) } data3 = { 'first_name': title_with_particule(user['name3']), 'birth_date': g.get_birth_date(user['year3']) } try: place = Place.objects.get(pk=user['id']) except Place.DoesNotExist: place = None print(user['id'], data1['first_name'], data3['birth_date']) if place and (data1['birth_date'] or data1['first_name']): profile1 = Profile(**data1) profile1.save() place.family_members.add(profile1) if place and (data2['birth_date'] or data2['first_name']): profile2 = Profile(**data2) profile2.save() place.family_members.add(profile2) if place and (data3['birth_date'] or data3['first_name']): profile3 = Profile(**data3) profile3.save() place.family_members.add(profile3) user = users.fetchone() users.close() dr.close() print('\n Success! \\o/\n')
def action(self, file_name, emails): results = Profile.mark_invalid_emails(emails=emails) for model, updated in results.items(): print(updated, model.__name__, 'emails marked as invalid from', file_name)
def save(self, commit=True): super().save(commit) if commit: Profile.mark_valid_emails([self.user.email]) return self.user
def migrate(): # Connect to an existing database passwd = getpass("MySQL password for 'root': ") dr = mdb.connect('localhost', 'root', passwd, 'pasportaservo', charset='utf8') users = dr.cursor(mdb.cursors.DictCursor) users.execute(""" SELECT u.uid id, field_nomo_kaj_familia_nomo_1_value name1, field_naskijaro_persono_1_value year1, field_sekso_persono_1_value sex1, field_nomo_kaj_familia_nomo_2_value name2, field_naskijaro_persono_2_value year2, field_sekso_persono_1_value sex2, field_nomo_kaj_familia_nomo_3_value name3, field_naskijaro_persono_3_value year3 -- There is no field_sekso_persono_3_value in the source database FROM users u INNER JOIN node n ON n.uid=u.uid AND n.type='profilo' INNER JOIN content_type_profilo p ON p.nid=n.nid WHERE u.uid > 1 AND u.name <> 'testuser' AND ( (field_lando_value = 'Albanio' AND field_urbo_value is not NULL) OR (field_lando_value <> 'Albanio')) AND field_familia_nomo_value <> 12 GROUP BY u.uid """) user = users.fetchone() from django.contrib.auth.models import User from hosting.utils import title_with_particule from hosting.models import Profile, Place django.setup() # Starting... print('Ignoring:') while user is not None: data1 = {'first_name': title_with_particule(user['name1']), 'birth_date': g.get_birth_date(user['year1']), 'title': g.get_title(user['sex1'])} data2 = {'first_name': title_with_particule(user['name2']), 'birth_date': g.get_birth_date(user['year2']), 'title': g.get_title(user['sex2'])} data3 = {'first_name': title_with_particule(user['name3']), 'birth_date': g.get_birth_date(user['year3'])} try: place = Place.objects.get(pk=user['id']) except Place.DoesNotExist: place = None print(user['id'], data1['first_name'], data3['birth_date']) if place and (data1['birth_date'] or data1['first_name']): profile1 = Profile(**data1) profile1.save() place.family_members.add(profile1) if place and (data2['birth_date'] or data2['first_name']): profile2 = Profile(**data2) profile2.save() place.family_members.add(profile2) if place and (data3['birth_date'] or data3['first_name']): profile3 = Profile(**data3) profile3.save() place.family_members.add(profile3) user = users.fetchone() users.close() dr.close() print('\n Success! \\o/\n')