def get_artist_user_profile(name): """Return existing artist profile or create a new one and return it""" if not name: return _ARTIST.user_profile name = name.strip() if not name: return _ARTIST.user_profile is_dirty = False try: user_profile = UserProfile.objects.get(full_artistname=name) _log.debug("Reusing existing artist profile") except UserProfile.DoesNotExist: # create user profile from last fm data n = name.split(' ') if len(n) == 2: first_name, last_name = n elif len(n) > 2: first_name = n[0] last_name = u' '.join(n[1:]) else: first_name = n last_name = '' username = name_to_username(name) if not username: return _ARTIST.user_profile username = username.strip() if not is_username_available(username): username = username + "-" a, created = User.objects.get_or_create( username=username, defaults = dict( first_name=first_name[:30], last_name=last_name[:30], email='*****@*****.**' % username.lower(), is_staff=False, is_active=True, ) ) user_profile = a.get_profile() if created: user_profile.send_reminders = False user_profile.send_favorites = False user_profile.is_verified = True user_profile.permission = 'everyone' is_dirty = True _log.debug("Using newly created artist profile") if not user_profile.full_artistname: user_profile.full_artistname = name is_dirty = True if not user_profile.avatar: # download profile image from last.fm info = get_artist_info(name) if info: imagelist = info.get('artist', {}).get('image', []) if imagelist: url = get_image_url(imagelist) copy_avatar_from_url_to_profile(url, user_profile) is_dirty = False if is_dirty: user_profile.save() return user_profile
def clean_username(self): username = self.cleaned_data["username"].strip() if not is_username_available(username.lower(), user_profile=self.instance): raise forms.ValidationError(_("This username is not available.")) return username