def send_message(request, username): data = {'status': 'FAIL'} try: profile_user = UserProfile.objects.get(username=username) except UserProfile.DoesNotExist: raise Http404 form_mess = MessageForm(request.POST) if form_mess.is_valid(): user_to = profile_user content = form_mess.cleaned_data['content'] mess = Messaging(user=request.user, user_to=user_to, content=content) mess.save() data['status'] = 'OK' return HttpResponse(json.dumps(data), "application/json")
def send_message(request, username): data = {"status": "FAIL"} try: profile_user = UserProfile.objects.get(username=username) except UserProfile.DoesNotExist: raise Http404 form_mess = MessageForm(request.POST) if form_mess.is_valid(): user_to = profile_user content = form_mess.cleaned_data["content"] mess = Messaging(user=request.user, user_to=user_to, content=content) mess.save() data["status"] = "OK" return HttpResponse(json.dumps(data), "application/json")
def profile(request, username): try: profile_user = UserProfile.objects.get(username=username) except UserProfile.DoesNotExist: raise Http404 form_mess = MessageForm() cover_form = ImageCoverForm() form_mess.fields['content'].widget.attrs['rows'] = 7 if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if request.user.is_authenticated() \ and 'image' in request.POST \ and form.is_valid(): image = form.save(profile_user) image.make_activity() image.generate_thumbnail(200, 200) image.change_orientation() # try: # pil_object = pilImage.open(image.image.path) # w, h = pil_object.size # x, y = 0, 0 # if w > h: # x, y, w, h = int((w-h)/2), 0, h, h # elif h > w: # x, y, w, h = 0, int((h-w)/2), w, w # new_pil_object = pil_object \ # .crop((x, y, x+w, y+h)) \ # .resize((200, 200)) # new_pil_object.save(image.image.thumb_path) # except: # pass return redirect('profile.views.profile', username=profile_user.username) if 'message' in request.POST: form_mess = MessageForm(request.POST) if form_mess.is_valid(): user_to = profile_user content = form_mess.cleaned_data['content'] mess = Messaging(user=request.user, user_to=user_to, content=content) mess.save() return HttpResponseRedirect(request.path) else: form = ImageForm() if request.method == 'GET' and 'albums' in request.GET: """Albums view""" return render_to_response( 'profile/albums.html', { 'profile_user': profile_user, 'form_mess': form_mess, 'albums': request.user.albums_set.all().order_by('position'), }, RequestContext(request)) data_uri = '' restrict_height = 300 target_width = 900 resize = False if request.method == 'POST' \ and 'cover_image' in request.POST: cover_form = ImageCoverForm(request.POST, request.FILES) if cover_form.is_valid(): image = cover_form.cleaned_data['cover_photo'] # save to memory f = StringIO(image.read()) # PIL image img = pilImage.open(f) # reading and applying orientation for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': break try: exif = dict(img._getexif().items()) if exif[orientation] == 3: img = img.rotate(180, expand=True) elif exif[orientation] == 6: img = img.rotate(270, expand=True) elif exif[orientation] == 8: img = img.rotate(90, expand=True) except: pass (width, height) = img.size if width < target_width: target_height = int(height * (1.0 * target_width / width)) img = img.resize((target_width, target_height)) elif width > target_width: target_height = int(height * (1.0 * target_width / width)) img.thumbnail((target_width, target_height), pilImage.ANTIALIAS) else: pass (new_width, new_height) = img.size if new_height != restrict_height: resize = True # save to memory thumb = StringIO() img.save(thumb, 'JPEG') thumb.seek(0) thumb_file = InMemoryUploadedFile(thumb, None, image.name, image.content_type, thumb.len, image.charset) # we can save it #if page.cover_photo and page.cover_photo.name != page.cover_photo.field.default: #page.cover_photo.delete() if not resize: request.user.cover_photo = thumb_file request.user.save() # or we can return it to template class DataURI: def __init__(self): self.width = 0 self.height = 0 self.data_uri = None def __repr__(self): return self.data_uri data_uri = DataURI() data_uri.data_uri = 'data:image/jpg;base64,' data_uri.data_uri += thumb.getvalue().encode('base64').replace( '\n', '') data_uri.width = new_width data_uri.height = new_height image_height = data_uri.height if resize: cover_offset = (image_height - restrict_height - 45 - 95) * -1 return render_to_response( 'profile/profile_cover.html', { 'profile_user': profile_user, 'form': form, 'cover_form': cover_form, 'form_mess': form_mess, 'cover_offset': cover_offset, 'data_uri': data_uri, 'profile_view': True, }, RequestContext(request)) else: return render_to_response( 'profile/profile.html', { 'profile_user': profile_user, 'form': form, 'cover_form': cover_form, 'form_mess': form_mess, 'show_cover_form': True, 'profile_view': True, }, RequestContext(request))
def profile(request, username): try: profile_user = UserProfile.objects.get(username=username) except UserProfile.DoesNotExist: raise Http404 form_mess = MessageForm() cover_form = ImageCoverForm() form_mess.fields["content"].widget.attrs["rows"] = 7 if request.method == "POST": form = ImageForm(request.POST, request.FILES) if request.user.is_authenticated() and "image" in request.POST and form.is_valid(): image = form.save(profile_user) image.make_activity() image.generate_thumbnail(200, 200) image.change_orientation() # try: # pil_object = pilImage.open(image.image.path) # w, h = pil_object.size # x, y = 0, 0 # if w > h: # x, y, w, h = int((w-h)/2), 0, h, h # elif h > w: # x, y, w, h = 0, int((h-w)/2), w, w # new_pil_object = pil_object \ # .crop((x, y, x+w, y+h)) \ # .resize((200, 200)) # new_pil_object.save(image.image.thumb_path) # except: # pass return redirect("profile.views.profile", username=profile_user.username) if "message" in request.POST: form_mess = MessageForm(request.POST) if form_mess.is_valid(): user_to = profile_user content = form_mess.cleaned_data["content"] mess = Messaging(user=request.user, user_to=user_to, content=content) mess.save() return HttpResponseRedirect(request.path) else: form = ImageForm() if request.method == "GET" and "albums" in request.GET: """Albums view""" return render_to_response( "profile/albums.html", { "profile_user": profile_user, "form_mess": form_mess, "albums": request.user.albums_set.all().order_by("position"), }, RequestContext(request), ) data_uri = "" restrict_height = 300 target_width = 900 resize = False if request.method == "POST" and "cover_image" in request.POST: cover_form = ImageCoverForm(request.POST, request.FILES) if cover_form.is_valid(): image = cover_form.cleaned_data["cover_photo"] # save to memory f = StringIO(image.read()) # PIL image img = pilImage.open(f) # reading and applying orientation for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == "Orientation": break try: exif = dict(img._getexif().items()) if exif[orientation] == 3: img = img.rotate(180, expand=True) elif exif[orientation] == 6: img = img.rotate(270, expand=True) elif exif[orientation] == 8: img = img.rotate(90, expand=True) except: pass (width, height) = img.size if width < target_width: target_height = int(height * (1.0 * target_width / width)) img = img.resize((target_width, target_height)) elif width > target_width: target_height = int(height * (1.0 * target_width / width)) img.thumbnail((target_width, target_height), pilImage.ANTIALIAS) else: pass (new_width, new_height) = img.size if new_height != restrict_height: resize = True # save to memory thumb = StringIO() img.save(thumb, "JPEG") thumb.seek(0) thumb_file = InMemoryUploadedFile(thumb, None, image.name, image.content_type, thumb.len, image.charset) # we can save it # if page.cover_photo and page.cover_photo.name != page.cover_photo.field.default: # page.cover_photo.delete() if not resize: request.user.cover_photo = thumb_file request.user.save() # or we can return it to template class DataURI: def __init__(self): self.width = 0 self.height = 0 self.data_uri = None def __repr__(self): return self.data_uri data_uri = DataURI() data_uri.data_uri = "data:image/jpg;base64," data_uri.data_uri += thumb.getvalue().encode("base64").replace("\n", "") data_uri.width = new_width data_uri.height = new_height image_height = data_uri.height if resize: cover_offset = (image_height - restrict_height - 45 - 95) * -1 return render_to_response( "profile/profile_cover.html", { "profile_user": profile_user, "form": form, "cover_form": cover_form, "form_mess": form_mess, "cover_offset": cover_offset, "data_uri": data_uri, "profile_view": True, }, RequestContext(request), ) else: return render_to_response( "profile/profile.html", { "profile_user": profile_user, "form": form, "cover_form": cover_form, "form_mess": form_mess, "show_cover_form": True, "profile_view": True, }, RequestContext(request), )