Coverage for users\views.py : 65%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# import the logging library
List, Reading, Fanfic
# Get an instance of a logger
""" View that shows sign up form """
""" View shown when sign up was successfull """
def check_username(request): """ Checks if the username is already in use """ else: return HttpResponse("Method not Allowed", status=405)
else:
""" Go to a user's profile edit page """
""" Get the modify page """ raise PermissionDenied()
""" Update a user profile settings """ raise PermissionDenied() # form is ok # email has changed raise CustomError( "This email is already in the database, " "choose another one.")
act_user.set_password(password) else: raise CustomError("Some of the fields are not valid")
# the user has uploaded their profile picture imageFile = request.FILES.get('profileImage') if imageFile.size > 1200000: # more than 1MB raise CustomError("The image size cannot" + "be more than 1MB.")
if (imageFile.name.endswith(".jpg") or imageFile.name.endswith(".png") or imageFile.name.endswith(".jpeg") or imageFile.name.endswith(".gif")): fs = FileSystemStorage() name = 'profiles/' + imageFile.name filename = fs.save(name, imageFile)
# delete current avatar image old_image = act_user.avatar.name if old_image != "profiles/default.png": fs.delete(old_image)
act_user.avatar = filename else: raise CustomError( "Only .jpg, .jpeg, .png and .gif are " + "accepted for the profile image.")
request, "The profile was successfully updated.") except CustomError as e: logger.error("Error updating a profile. {}".format(e)) messages.error(request, e) args = {'form': form, 'section': 'profile'} return render(request, 'edit-user.html', args) except Exception as e: logger.error("Error updating a profile. {}".format(e)) messages.error(request, "There was an error updating the profile.") args = {'form': form, 'section': 'profile'} return render(request, 'edit-user.html', args)
""" Go to a user's general settings edit page """
""" Get the modify page """ raise PermissionDenied()
""" Update a user general settings """ raise PermissionDenied() # error raise CustomError( "There was an error trying to save the new settings.")
request, "The profile was successfully updated.") except CustomError as e: logger.error("Error updating a profile. {}".format(e)) messages.error(request, e) args = {'section': 'general'} return render(request, 'edit-user.html', args) except Exception as e: logger.error("Error updating a profile. {}".format(e)) messages.error(request, "There was an error updating the profile.") args = {'section': 'profile'} return render(request, 'edit-user.html', args)
""" Get a user's data backup """
""" Get the user file """ act_user = request.user if int(act_user.id) != int(user_id): raise PermissionDenied()
app_list = {}
app_list['chapters_readings'] = list(Reading.objects.filter( user=act_user).values('chapter__fanfic__name', 'private_notes', 'read', 'chapter__num_chapter', 'chapter__url_chapter')) added_fanfics = FanficList.objects.filter( list__user=act_user).values_list('fanfic__id', flat=True) app_list['fanfics_in_lists'] = list(FanficList.objects.filter( list__user=act_user).values('list__name', 'fanfic__name')) app_list['fanfics'] = list(Fanfic.objects.filter( id__in=added_fanfics).values( 'name', 'web'))
response = HttpResponse(json.dumps( app_list), content_type='application/json') response['Content-Disposition'] = 'attachment; ' \ 'filename="fickeeper_backup{' \ '}.json"'.format( str(datetime.datetime.now().strftime( '%Y%m%d%H%M%S%f')) )
return response
""" A user's profile """
Q(subject_user=u) | Q(target=u)).order_by('-when')
id=user_id) if user.privacy == 3: # nobody can see this user's fanfics see = False elif user.privacy == 1: # if the propietary of the library follows the request user if user.follows(self.request.user) is False: see = False
def follow_user(request): """ Follow a user """
return HttpResponse("Method not Allowed", status=405) else: return HttpResponse("Method not Allowed", status=405)
return HttpResponse("Method not Allowed", status=405)
user_one=act_user, user_two=target_user).exists()
user_one=act_user, user_two=target_user).delete() else: user_one=act_user, user_two=target_user)
# send notification subject=source, verb="has followed", target=target, subject_user=source, link=source.get_link(), in_top_bar=True, in_feed=True)
status=200)
""" Show fanfics of user """
if self.user.privacy == 3: # nobody can see this user's fanfics raise PermissionDenied() elif self.user.privacy == 1: # if the propietary of the library follows the request user if self.user.follows(self.request.user) is False: raise PermissionDenied()
# get the queryset filtered
'fandom_id')) raise Exception("There was an unexpected error.")
fandom=self.fandom).values_list('fanfic__id', flat=True))
# FILTER BY LIST list_id = int(self.request.GET.get('list')) if list_id == 0 or list_id == "0": # all the fanfics, doesn't matter the list fanfics = FanficList.objects.filter( list__user=self.user, fanfic__id__in=fanfics_ids) else: list_obj = List.objects.filter(id=list_id, user=self.user) if list_obj.exists() is False: # this list does not exist, or the user is not the # propietary raise PermissionDenied()
list_obj = list_obj.first() self.list_chose = list_obj.id fanfics = FanficList.objects.filter( list=list_obj, fanfic__id__in=fanfics_ids) else: list__user=self.user, fanfic__id__in=fanfics_ids)
# LANGUAGE fanfic__language__iexact=language)
# GENRE fanfic__genre1__iexact=genre) | Q( fanfic__genre2__iexact=genre) | Q( fanfic__genre3__iexact=genre) | Q( fanfic__genre4__iexact=genre))
# LENGTH pass # < 1K words words = 1000 fanfics = fanfics.filter( fanfic__num_words__lt=words) else: # > 10K words words = 10000 # > 40K words words = 40000 # > 60K words words = 60000 # > 100K words words = 100000 fanfic__num_words__gte=words)
# STATUS # complete elif status == 2 or status == "2": # in progress fanfics = fanfics.exclude(fanfic__complete=True)
# RATING # K fanfics = fanfics.filter(fanfic__rating='K') # K+ fanfics = fanfics.filter(fanfic__rating='K+') # T fanfics = fanfics.filter(fanfic__rating='T') # M fanfics = fanfics.filter(fanfic__rating='M')
# CHARACTER A & B and character_b != "0" and character_b != 0: character__id=character_a) | Q( character__id=character_b)).values_list( 'fanfic__id', flat=True) elif character_a != 0: fanfics_ids = CharacterFanfic.objects.filter( character__id=character_a).values_list( 'fanfic__id', flat=True) elif character_b != 0: fanfics_ids = CharacterFanfic.objects.filter( character__id=character_b).values_list( 'fanfic__id', flat=True) fanfic__id__in=fanfics_ids)
# SORT BY # sort by last updated fanfics = fanfics.order_by( '-fanfic__last_time_updated') # sort by number of reviews fanfics = sorted(fanfics, key=lambda t: t.fanfic.get_num_reviews(), reverse=True) # sort by number of followers fanfics = sorted(fanfics, key=lambda t: t.fanfic.get_num_of_users(), reverse=True) else:
else: raise Exception("There was an unexpected error.")
fanfics = fanfics.order_by('id') return fanfics
else:
except Exception: messages.error(self.request, "There was an unexpected error " "trying to filter the fanfics.")
return []
query_string_params_two = re.compile("^page=\d+&?").split( query_string_params) if len(query_string_params_two) == 2: query_string_params_two = query_string_params_two[1] else: query_string_params_two = '' else:
|