def test_find_username_skeleton(self): usernames = [ { "username": "******", "skeleton": "adr1", "reason": "no special add" }, { "username": "******", "skeleton": "adr1", "reason": "add normal ascii space" }, { "username": "******", "skeleton": "adr1", "reason": "add space (U+00A0)" }, { "username": "******", "skeleton": "adr1", "reason": "add space (U+2004)" }, { "username": "******", "skeleton": "adr1", "reason": "add space (U+00A0)" }, { "username": "******", "skeleton": "xp123.", "reason": "cyrilic X and P" }, { "username": "******", "skeleton": "adr-1", "reason": "add U+2013" }, { "username": "******", "skeleton": "adr1", "reason": "add U+2014" }, ] for username in usernames: name = Profile.find_username_skeleton(username["username"]) skeleton = Profile.find_username_skeleton(username["skeleton"]) self.assertEqual( name, skeleton, f"Username {username['username']} give unexpected skeleton " f"'{name}' instead of '{skeleton}' : {username['reason']}", )
def validate_zds_username(value, check_username_available=True): """ Check if username is used by another user :param value: value to validate (str or None) :return: """ msg = None user_count = User.objects.filter(username=value).count() skeleton_user_count = Profile.objects.filter( username_skeleton=Profile.find_username_skeleton(value)).count() if ',' in value: msg = _('Le nom d\'utilisateur ne peut contenir de virgules') elif contains_utf8mb4(value): msg = _( 'Le nom d\'utilisateur ne peut pas contenir des caractères utf8mb4' ) elif check_username_available and user_count > 0: msg = _('Ce nom d\'utilisateur est déjà utilisé') elif check_username_available and skeleton_user_count > 0: msg = _( 'Un nom d\'utilisateur visuellement proche du votre existe déjà') elif not check_username_available and user_count == 0: msg = _('Ce nom d\'utilisateur n\'existe pas') if msg is not None: raise ValidationError(msg)
def update_profile(self, profile, form): profile.show_email = "show_email" in form.cleaned_data.get("options") new_username = form.cleaned_data.get("username") previous_username = form.cleaned_data.get("previous_username") new_email = form.cleaned_data.get("email") previous_email = form.cleaned_data.get("previous_email") if new_username and new_username != previous_username: # Add a karma message for the staff bot = get_object_or_404( User, username=settings.ZDS_APP["member"]["bot_account"]) KarmaNote( user=profile.user, moderator=bot, note=_("{} s'est renommé {}").format(profile.user.username, new_username), karma=0, ).save() # Change the username profile.user.username = new_username # update skeleton profile.username_skeleton = Profile.find_username_skeleton( new_username) if new_email and new_email != previous_email: profile.user.email = new_email # Create an alert for the staff if it's a new provider provider = provider = new_email.split("@")[-1].lower() if (not NewEmailProvider.objects.filter( provider=provider).exists() and not User.objects.filter( email__iendswith=f"@{provider}").exclude( pk=profile.user.pk).exists()): NewEmailProvider.objects.create(user=profile.user, provider=provider, use=EMAIL_EDIT)
def test_find_username_skeleton(self): usernames = [{ 'username': '******', 'skeleton': 'adr1', 'reason': 'no special add' }, { 'username': '******', 'skeleton': 'adr1', 'reason': 'add normal ascii space' }, { 'username': '******', 'skeleton': 'adr1', 'reason': 'add space (U+00A0)' }, { 'username': '******', 'skeleton': 'adr1', 'reason': 'add space (U+2004)' }, { 'username': '******', 'skeleton': 'adr1', 'reason': 'add space (U+00A0)' }, { 'username': '******', 'skeleton': 'xp123.', 'reason': 'cyrilic X and P' }, { 'username': '******', 'skeleton': 'adr-1', 'reason': 'add U+2013' }, { 'username': '******', 'skeleton': 'adr1', 'reason': 'add U+2014' }] for username in usernames: name = Profile.find_username_skeleton(username['username']) skeleton = Profile.find_username_skeleton(username['skeleton']) self.assertEqual( name, skeleton, f"Username {username['username']} give unexpected skeleton " f"'{name}' instead of '{skeleton}' : {username['reason']}")
def form_valid(self, form): profile = self.create_profile(form.data) profile.last_ip_address = get_client_ip(self.request) profile.username_skeleton = Profile.find_username_skeleton(profile.user.username) self.save_profile(profile) token = self.generate_token(profile.user) try: self.send_email(token, profile.user) except Exception as e: logging.getLogger(__name__).warning("Mail not sent", exc_info=e) messages.warning(self.request, _("Impossible d'envoyer l'email.")) self.object = None return self.form_invalid(form) return render(self.request, self.get_success_template())
def create_profile(self, data): """ Creates an inactive profile in the database. :param data: Array about a user. :type data: array :return: instance of a profile inactive :rtype: Profile object """ username = data.get('username') email = data.get('email') password = data.get('password') user = User.objects.create_user(username, email, password) user.set_password(password) user.is_active = False user.backend = 'django.contrib.auth.backends.ModelBackend' profile = Profile(user=user) return profile
def create_profile(self, data): """ Creates an inactive profile in the database. :param data: Array about an user. :type data: array :return: instance of a profile inactive :rtype: Profile object """ username = data.get('username') email = data.get('email') password = data.get('password') user = User.objects.create_user(username, email, password) user.set_password(password) user.is_active = False user.backend = 'django.contrib.auth.backends.ModelBackend' profile = Profile(user=user, show_email=False, show_sign=True, hover_or_click=True, allow_temp_visual_changes=True, email_for_answer=False) return profile
def init_skeleton(apps, schema_editor): profiles = Profile.objects.all() for profile in profiles: profile.username_skeleton = Profile.find_username_skeleton( profile.user.username) profile.save()