def boost(request): ''' Lets a user boost an artist ''' user = functions.get_authenticated_user(request) if user != None: if request.method == "POST": user_id = request.POST.get('user_id') else: messages.error(request, 'Something went wrong.') return my_profile(request) try: target_user = User.objects.get(pk=user_id) profile_type = functions.profile_type(target_user) if profile_type != 'artist': messages.error(request, 'You can only boost other artists') return profile_id(request, user_id) except User.DoesNotExist: messages.error(request, 'This profile does not exist anymore') return profile_id(request, user_id) if profile_type == '': messages.error(request, 'You do not have the authority to boost artists') return profile_id(request, user_id) else: if user == target_user: messages.error(request, 'You cannot boost yourself') return profile_id(request, user_id) else: artist_obj = Artist.objects.get(user=target_user.id) try: boost_obj = Boost.objects.get(artist=target_user.artist, user=user, genre=artist_obj.genres) if boost_obj == None: Boost.objects.create(artist=target_user.artist, user=user, genre=artist_obj.genres) else: boost_obj.is_active = True boost_obj.save() except: Boost.objects.create(artist=target_user.artist, user=user, genre=artist_obj.genres) messages.success( request, 'You are now bosting %s.' % (target_user.artist.name)) return profile_id(request, user_id) else: messages.error(request, 'You are not logged in') return redirect('account_login')
def follow(request): ''' Lets a user follow an artist ''' user = functions.get_authenticated_user(request) if user != None: if request.method == "POST": user_id = request.POST.get('user_id') else: messages.error(request, 'Something went wrong.') return my_profile(request) try: target_user = User.objects.get(pk=user_id) profile_type = functions.profile_type(target_user) if profile_type != 'artist': messages.error(request, 'You can only follow other artists') return profile_id(request, user_id) except User.DoesNotExist: messages.error(request, 'This profile does not exist anymore') return profile_id(request, user_id) try: Follow.objects.get(artist=target_user.artist, follower=user) messages.error( request, 'You are already following %s.' % (target_user.artist.name)) return profile_id(request, user_id) except Follow.DoesNotExist: pass if profile_type == '': messages.error(request, 'You do not have the authority to follow artists') return profile_id(request, user_id) else: if user == target_user: messages.error(request, 'You cannot follow yourself') return profile_id(request, user_id) else: Follow.objects.create(artist=target_user.artist, follower=user) messages.success( request, 'You are now following %s.' % (target_user.artist.name)) return profile_id(request, user_id) else: messages.error(request, 'You have to be logged in') return profile_id(request, user_id)
def unboost(request): ''' Unboosting artist ''' user = functions.get_authenticated_user(request) if user != None: if request.method == "POST": user_id = request.POST.get('user_id') else: messages.error(request, 'Something went wrong.') return my_profile(request) try: target_user = User.objects.get(pk=user_id) profile_type = functions.profile_type(target_user) if profile_type != 'artist': messages.error(request, 'You can only stop boosting other artists') return profile_id(request, user_id) except User.DoesNotExist: messages.error(request, 'This profile does not exist anymore') return profile_id(request, user_id) if profile_type == '': messages.error(request, 'You do not have the authority to boost artists') return profile_id(request, user_id) else: if user == target_user: messages.error(request, 'You cannot boost yourself') return profile_id(request, user_id) else: try: target_artist = Artist.objects.get(user=target_user) boost_obj = Boost.objects.get(artist=target_artist, user=user) boost_obj.is_active = False boost_obj.save() messages.success( request, 'You have now unbosted %s.' % (target_user.artist.name)) if boost_obj == None: raise Exception except Exception as msg: messages.error(request, msg) #messages.error(request,'You cannot unboost someone you are not boosting!') return profile_id(request, user_id) else: messages.error(request, 'You are not logged in') return redirect('account_login')
def edit_profile(request): ''' Present edit profile form for both fans and artists ''' user = functions.get_authenticated_user(request) if user != None: if functions.is_artist(request): form_type = ArtistForm form_obj = user.artist elif functions.is_fan(request): form_type = FanForm form_obj = user.fan else: return index(request) #If request method is POST, try to save the form data into the database if request.method == "POST": form = form_type(request.POST, request.FILES, instance=form_obj) if form.is_valid(): form.save() form_obj.valid_profile = True form_obj.save() messages.success(request, 'Your profile has been saved') request.method = "GET" return my_profile(request) else: messages.error(request, 'Your profile was not changed') return render(request, 'edit_profile_page.html', context={'form': form}) #If it is not POST, generate a new form with all the infromation already in the form else: form = form_type(None, instance=form_obj) return render(request, 'edit_profile_page.html', context={'form': form}) else: messages.error(request, 'You are not logged in') return redirect('account_login')
def unfollow(request): ''' Lets a user unfollow an artist ''' user = functions.get_authenticated_user(request) if user != None: if request.method == "POST": user_id = request.POST.get('user_id') else: messages.error(request, 'Something went wrong.') return my_profile(request) try: target_user = User.objects.get(pk=user_id) except User.DoesNotExist: messages.error(request, 'This profile does not exist anymore') return profile_id(request, user_id) if user == target_user: messages.error(request, 'You cannot unfollow yourself') return profile_id(request, user_id) try: instance = Follow.objects.get(artist=target_user.artist, follower=user) instance.delete() messages.success( request, 'You have now unfollowed %s.' % (target_user.artist.name)) return profile_id(request, user_id) except Follow.DoesNotExist: messages.error( request, 'You are already not following %s.' % (target_user.artist.name)) return profile_id(request, user_id) else: messages.error(request, 'You have to be logged in') return profile_id(request, user_id)
def settings(request): user = functions.get_authenticated_user(request) if user != None: return render(request, 'settings_page.html', {'this_user': user}) else: return index(request)