def post(self, request, username): """Handles upload of profile picture by user.""" if not is_user_current_user(request, username): self.error = 'You are not permitted to do this.' self.status = 405 return render(request, 'error.html', { 'error': self.error }, status=self.status) else: user = User.objects.get(username=username) p = user.profile form = ProfilePictureUploadForm(request.POST, request.FILES) if form.is_valid(): try: image = request.FILES['image'] w, h = get_image_dimensions(image) if w < 200 or h < 200 or w > 1000 or h > 1000: error = """Image dimension should be between 200x200 and 1000x1000""" raise if p.picture: os.remove(p.picture.path) p.picture = image p.save() print(p.picture.path) returnpath = '/user/' + \ user.username + '/crop_profilepicture' return HttpResponseRedirect(returnpath) except: return render(request, 'uploadprofilepicture.html', {'user': user, 'error': error}) else: return render(request, 'uploadprofilepicture.html', {'user': user})
def get(self, request, username): if not request.user.is_authenticated() or \ not is_user_current_user(request, username): return render(request, 'error.html', { 'error': 'You are not permitted to do this.' }, status=404) else: return render(request, 'cropprofilepicture.html', {'user': request.user})
def get(self, request, username): if not request.user.is_authenticated() or \ not is_user_current_user(request, username): self.error = 'You are not permitted to do this.' self.status = 405 return render(request, 'error.html', { 'error': self.error }, status=self.status) else: return render(request, 'uploadprofilepicture.html', {'user': request.user})
def get(self, request, username): if is_user_current_user(request, username): user = request.user if is_user_hod_or_teacher(request): self.subject_list = user.teachingsubjects.all() else: self.subject_list = user.subscribedsubjects.all() if not self.subject_list: self.error = 'You are not subscribed to any subjects' self.status = 404 else: self.error = 'You are not logged in or not allowed to access \ this page.' self.status = 404 return render(request, 'my_subjects.html', { 'subject_list': self.subject_list, 'error': self.error }, status=self.status)
def post(self, request, username): if not request.user.is_authenticated() or \ not is_user_current_user(request, username): return render(request, 'error.html', { 'error': 'You are not permitted to do this.' }, status=404) user = request.user if user.profile.picture: form = ProfilePictureCropForm(request.POST) if form.is_valid(): x1 = int(float(form.cleaned_data['x1'])) y1 = int(float(form.cleaned_data['y1'])) x2 = int(float(form.cleaned_data['x2'])) y2 = int(float(form.cleaned_data['y2'])) image = Image.open(user.profile.picture.path) cropped_image = image.crop((x1, y1, x2, y2)) cropped_image.save(user.profile.picture.path) return HttpResponseRedirect('/user/' + user.username) return HttpResponseRedirect('/user/' + user.username)