def upload_file(user, filepath, private=False): try: myfile = open(filepath, 'rb') f = ContentFile(myfile.read()) f.name = filepath.split("/")[1] f.size = os.path.getsize(filepath) ext = filepath.split(".")[-1] supported_thumbnail = ext.lower() in [ "gif", "png", "jpg", "webm", "mp4", "jpeg" ] f = File.objects.get_or_create( user=user, ip='127.0.0.1', file_content=f, file_thumbnail=f if supported_thumbnail else None, generated_filename=get_id_gen(), is_private=private)[0] f.save() if private: print( '\t* New PRIVATE file uploaded by ' + user.username + ':', f.original_filename) else: print('\t* New file uploaded by ' + user.username + ':', f.original_filename) except Exception: print("\t* Could not upload file: " + filepath)
def saveExtras(self,json,product): if json.get('images'): for image in json.get('images'): try: image = ContentFile(b64decode(image.replace('data:image/jpeg;base64,','')), name='{}.{}'.format(image[27:42],'jpg')) image = Gallery(image=image,product = product) image.save() except: continue product.add_model.all().delete() if json.get('add_model'): for model in json.get('add_model'): product.add_model.create(model=model)
def saveExtras(self, json, product): if json.get('images'): for image in json.get('images'): try: image = sub(r'data:image/[a-z]+;base64,', '', image) image = ContentFile(b64decode(image), name='{}.{}'.format( search(r'[a-z0-9A-Z]+', image[27:42])[0], 'jpg')) image = Gallery(image=image, product=product) image.save() except Exception as e: continue product.add_model.all().delete() if json.get('add_model'): for model in json.get('add_model'): product.add_model.create(model=model)
def add_custom_pic(request): try: submission = UserSubmission.objects.get(pk=request.session["user"]) except UserSubmission.DoesNotExist: return redirect(email) name = str(len(UserImage.objects.filter(submission=submission))) image = UserImage.objects.create(submission=submission) if 'file' in request.POST: file_content = ContentFile( str(request.POST['file']).split(',')[1].decode('base64')) file_content = sharpen(Image.open(file_content)) thumb_io = cStringIO.StringIO() file_content.save(thumb_io, format='JPEG') file_content = ContentFile(thumb_io.getvalue()) image.image.save("final-" + name + ".png", file_content) image.tags = request.POST.getlist('tags[]') tags = image.tags tags_submit = [] already_tagged = [] if isinstance(tags, list): for tag in tags: split_tag = tag.split(',') if split_tag[0] not in already_tagged: tags_submit.append({ 'tag_uid': split_tag[0], 'x': split_tag[1], 'y': split_tag[2] }) already_tagged.append(split_tag[0]) submission.num_tags = len(tags_submit) submissions = Submission.objects.filter(user_id=submission.user_id) user = submissions.latest('last_submitted') user.eligible = False user.save() submission.save() image.save() # Submission.objects.create(user_id=submission.user_id, email=submission.email) if image.image: context = {'image': "/media/" + image.image.name, "tags": tags_submit} else: context = {"tags": tags_submit} return HttpResponse(json.dumps(context), content_type="application/json")
def save(self, *args, **kwargs): created = self._state.adding # created or updated image_updated = False if not created: # Store the new image image = self.image if image and image.name != self._curr_image.name: image_updated = True # Deleting the previous image and its thumnail self._curr_image.delete(False) self.image_thumb.delete(False) # Assigning the image field with the new image self.image = image image_name = image.name.rsplit("/", 1)[-1] # Create a new image for thumbnail thumb_image = ContentFile(image.read()) # Save the thumbnail but do not commit to the database self.image_thumb.save(image_name, thumb_image, False) # Save the model super(CustomUser, self).save(*args, **kwargs) if image_updated: # Get the thumbnail image from its path to resize it thumb_image = Image.open(self.image.path) if thumb_image.height > 140 or thumb_image.height > 140: output_size = (140, 140) thumb_image.thumbnail(output_size) # Save the resized image to its path thumb_image.save(self.image_thumb.path)
def add_custom_pic(request): try: submission = UserSubmission.objects.get(pk=request.session["user"]) except UserSubmission.DoesNotExist: return redirect(email) name = str(len(UserImage.objects.filter(submission=submission))) image = UserImage.objects.create(submission=submission) if "file" in request.POST: file_content = ContentFile(str(request.POST["file"]).split(",")[1].decode("base64")) file_content = sharpen(Image.open(file_content)) thumb_io = cStringIO.StringIO() file_content.save(thumb_io, format="JPEG") file_content = ContentFile(thumb_io.getvalue()) image.image.save("final-" + name + ".png", file_content) image.tags = request.POST.getlist("tags[]") tags = image.tags tags_submit = [] already_tagged = [] if isinstance(tags, list): for tag in tags: split_tag = tag.split(",") if split_tag[0] not in already_tagged: tags_submit.append({"tag_uid": split_tag[0], "x": split_tag[1], "y": split_tag[2]}) already_tagged.append(split_tag[0]) submission.num_tags = len(tags_submit) submissions = Submission.objects.filter(user_id=submission.user_id) user = submissions.latest("last_submitted") user.eligible = False user.save() submission.save() image.save() # Submission.objects.create(user_id=submission.user_id, email=submission.email) if image.image: context = {"image": "/media/" + image.image.name, "tags": tags_submit} else: context = {"tags": tags_submit} return HttpResponse(json.dumps(context), content_type="application/json")