def post(request, obj): try: data = request.POST or json.loads(request.body)['body'] except RawPostDataException: data = request.POST tags = data.get('tags', '').split(',') resetthumbnail = data.get('reset-thumbnail', False) res = Result() for tag in tags: try: t = Tag.objects.get(pk=int(tag)) except ValueError: t, created = Tag.objects.get_or_create(name=tag) if created: res.append(t.json()) obj.tags.add(t) if request.FILES: # -- Handle thumbnail upload f = request.FILES.get('file') relativedest = Path(obj.source.name).parent / f.name dest = getRoot() / relativedest handle_uploaded_file(dest, f) obj.custom_thumbnail = relativedest obj.save() if resetthumbnail: obj.custom_thumbnail = None obj.save() res.value = obj.json() return JsonResponse(res.asDict())
def handle(self, *args, **options): if options['topdir'] == '.': options['topdir'] = options['path'] files = path.Path(options['path']).walk() extensions = EXT['image'] + EXT['video'] baseuser = User.objects.get_or_create( username=options['user'], defaults={'first_name': 'No', 'last_name': 'Author', 'email': '*****@*****.**'} )[0] gallery = Gallery.objects.get(pk=1) for file_ in files: if options['date']: d = datetime.datetime.fromtimestamp(file_.mtime) if d <= options['date']: self.stdout.write('Skipping {} as it is too old: {}'.format(file_, d)) continue if file_.ext.lower() in extensions: try: user = getAuthor(file_.owner) except NotImplementedError: user = baseuser uniqueName = Piece.getUniqueID(file_, user) if file_.ext.lower() in EXT['image']: model = Image else: model = Video obj = model.objects.get_or_create(unique_id=uniqueName, defaults={'author': user})[0] guid = obj.getGuid() hashVal = file_.read_hexhash('sha1') objPath = getRoot() / guid.guid[-2:] / guid.guid / file_.name.lower() hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() file_.copy(hashPath) obj.hash = hashVal obj.foreign_path = file_ obj.title = objPath.namebase obj.created = datetime.datetime.fromtimestamp(file_.mtime) obj.gallery_set.add(gallery) tags = [] if options['dirnames']: tags = file_.parent.replace(options['topdir'], '').replace('\\', '/').split('/') tags = filter(None, tags) obj.export(hashVal, hashPath, tags=tags) self.stdout.write('Added %s\n' % file_)
def handle(self, *args, **options): if options['topdir'] == '.': options['topdir'] = args[0] FILES = Path(args[0]).walk() ALL_EXT = EXT['image'] + EXT['video'] USER = User.objects.get_or_create( username='******', defaults={'first_name': 'No', 'last_name': 'Author', 'email': '*****@*****.**'} )[0] GALLERY = Gallery.objects.get(pk=1) for file_ in FILES: if file_.ext.lower() in ALL_EXT: uniqueName = Piece.getUniqueID(file_, USER) if file_.ext.lower() in EXT['image']: model = Image else: model = Video obj = model.objects.get_or_create(unique_id=uniqueName, defaults={'author': USER})[0] guid = obj.getGuid() hashVal = file_.read_hexhash('sha1') objPath = getRoot() / guid.guid[-2:] / guid.guid / file_.name.lower() hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() file_.copy(hashPath) obj.hash = hashVal obj.foreign_path = file_ obj.title = objPath.namebase obj.gallery_set.add(GALLERY) tags = [] if options['dirnames']: tags = file_.parent.replace(options['topdir'], '').replace('\\', '/').split('/') tags = filter(None, tags) obj.export(hashVal, hashPath, tags=tags) self.stdout.write('Added %s\n' % file_)
def post(request, obj): try: data = request.POST or json.loads(request.body)['body'] except RawPostDataException: data = request.POST tags = data.get('tags', '').split(',') resetthumbnail = data.get('reset-thumbnail', False) crop = data.get('crop') res = Result() for tag in tags: try: t = Tag.objects.get(pk=int(tag)) except ValueError: t, created = Tag.objects.get_or_create(name=tag) if created: res.append(t.json()) obj.tags.add(t) if obj.custom_thumbnail and (crop or request.FILES or resetthumbnail): try: os.unlink(getRoot() / obj.custom_thumbnail.name) except OSError: pass if crop: box = [int(_) for _ in crop] # -- Handle thumbnail upload source = Path(obj.source.name) relativedest = source.parent / '{:.0f}{}'.format( time.time(), source.ext) dest = getRoot() / relativedest source = getRoot() / source source.copy(dest) obj.custom_thumbnail = relativedest image = pilImage.open(dest) # Crop from center image = image.crop(box) image.load() # Resize size = abs(box[2] - box[0]) image.thumbnail((FROG_THUMB_SIZE, FROG_THUMB_SIZE), pilImage.ANTIALIAS) image.resize((size, size)).save(dest) obj.save() if request.FILES: # -- Handle thumbnail upload f = request.FILES.get('file') relativedest = Path(obj.source.name).parent / f.name dest = getRoot() / relativedest handle_uploaded_file(dest, f) obj.custom_thumbnail = relativedest image = pilImage.open(dest) sizeinterface = namedtuple('sizeinterface', 'width,height') size = sizeinterface(*image.size) box, width, height = cropBox(size) # Resize image.thumbnail((width, height), pilImage.ANTIALIAS) # Crop from center image.crop(box).save(dest) obj.save() if resetthumbnail: obj.custom_thumbnail = None obj.save() res.value = obj.json() return JsonResponse(res.asDict())
def post(request, obj): try: data = request.POST or json.loads(request.body)["body"] except RawPostDataException: data = request.POST tags = data.get("tags", "").split(",") resetthumbnail = data.get("reset-thumbnail", False) crop = data.get("crop") res = Result() for tag in tags: try: t = Tag.objects.get(pk=int(tag)) except ValueError: t, created = Tag.objects.get_or_create(name=tag) if created: res.append(t.json()) obj.tags.add(t) if obj.custom_thumbnail and (crop or request.FILES or resetthumbnail): try: os.unlink(getRoot() / obj.custom_thumbnail.name) except OSError: pass if crop: box = [int(_) for _ in crop] # -- Handle thumbnail upload source = Path(obj.source.name) relativedest = obj.getPath(True) / "{:.0f}{}".format( time.time(), source.ext ) dest = getRoot() / relativedest source = getRoot() / source if not dest.parent.exists(): dest.parent.makedirs() source.copy(dest) obj.custom_thumbnail = relativedest image = pilImage.open(dest) # Crop from center image = image.crop(box) image.load() # Resize image.thumbnail( (FROG_THUMB_SIZE, FROG_THUMB_SIZE), pilImage.ANTIALIAS ) image.save(dest) obj.save() if request.FILES: # -- Handle thumbnail upload f = request.FILES.get("file") relativedest = obj.getPath(True) / f.name dest = getRoot() / relativedest handle_uploaded_file(dest, f) obj.custom_thumbnail = relativedest try: if dest.ext == ".psd": image = psd_tools.PSDLoad(dest).as_PIL() else: image = pilImage.open(dest) except IOError as err: res.isError = True res.message = "{} is not a supported thumbnail image type".format( f.name ) return JsonResponse(res.asDict()) box, width, height = cropBox(*image.size) # Resize image.thumbnail((width, height), pilImage.ANTIALIAS) # Crop from center box = cropBox(*image.size)[0] image.crop(box).save(dest) obj.save() if resetthumbnail: obj.custom_thumbnail = None obj.save() res.value = obj.json() return JsonResponse(res.asDict())