def isUnique(request): path = request.GET.get('path', None) res = Result() if path: if request.user.is_anonymous(): username = request.GET.get('user', 'noauthor') user = User.objects.get(username=username) else: user = request.user uniqueID = Piece.getUniqueID(path, user) img = Image.objects.filter(unique_id=uniqueID) vid = Video.objects.filter(unique_id=uniqueID) if img: res.append(img[0].json()) elif vid: res.append(vid[0].json()) else: res.append(True) res.isSuccess = True else: res.isError = True res.message = "No path provided" return JsonResponse(res)
def isUnique(request): data = request.POST or json.loads(request.body)['body'] paths = data.get('paths', []) res = Result() if data.get('user'): user = User.objects.get(username=data['user']) elif request.user.is_anonymous(): raise HttpResponseForbidden else: user = request.user for path in paths: uniqueid = Piece.getUniqueID(path, user) img = Image.objects.filter(unique_id=uniqueid) vid = Video.objects.filter(unique_id=uniqueid) if img: res.append(img[0].json()) elif vid: res.append(vid[0].json()) else: res.append(True) return JsonResponse(res.asDict())
def isUnique(request): data = request.POST or json.loads(request.body)["body"] paths = data.get("paths", []) res = Result() if data.get("user"): user = User.objects.get(username=data["user"]) elif request.user.is_anonymous: raise HttpResponseForbidden else: user = request.user for path in paths: uniqueid = Piece.getUniqueID(path, user) img = Image.objects.filter(unique_id=uniqueid) vid = Video.objects.filter(unique_id=uniqueid) if img: res.append(img[0].json()) elif vid: res.append(vid[0].json()) else: res.append(True) 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 like(request, guid): obj = Piece.fromGuid(guid) res = Result() if obj.like(request): emailLike(request, obj) else: res.message = 'Cannot "like" things more than once' res.append(obj.json()) return JsonResponse(res.asDict())
def like(request, guid): obj = Piece.fromGuid(guid) res = Result() if obj.like(request): emailLike(request, obj) else: res.isError = True res.message = 'Cannot "like" things more than once' res.append(obj.json()) return JsonResponse(res.asDict())
def group(request, obj_id=None): res = Result() if request.method == "GET": try: group = Group.objects.get(pk=obj_id) res.append(group.json()) except ObjectDoesNotExist as err: res.isError = True res.message = str(err) elif request.method == "POST": data = json.loads(request.body)["body"] user = getUser(request) if user: items = getObjectsFromGuids(data["guids"]) gallery = data.get("gallery") g = Group() g.author = user g.title = data.get("title", items[0].title) g.thumbnail = items[0].thumbnail g.description = data.get("description", items[0].description) g.save() g.guid = g.getGuid().guid g.save() if gallery: Gallery.objects.get(pk=gallery).groups.add(g) for item in items: g.appendChild(item) res.append(g.json()) else: res.isError = True res.message = "No user found to create group" elif request.method == "PUT": data = json.loads(request.body)["body"] g = Group.objects.get(pk=obj_id) action = data["action"] index = data.get("index") item = Piece.fromGuid(data["guid"]) if action == "append": g.appendChild(item) elif action == "insert": g.insertChild(index, item) else: g.removeChild(item) res.append(g.json()) else: g = Group.objects.get(pk=obj_id) g.delete() return JsonResponse(res.asDict())
def data(request, guid): obj = Piece.fromGuid(guid) if request.method == "GET": res = Result() res.append(obj.json()) return JsonResponse(res.asDict()) elif request.method == "POST": return post(request, obj) elif request.method == "PUT": return put(request, obj) elif request.method == "DELETE": return delete(request, obj)
def data(request, guid): obj = Piece.fromGuid(guid) if request.method == 'GET': res = Result() res.append(obj.json()) return JsonResponse(res.asDict()) elif request.method == 'POST': return post(request, obj) elif request.method == 'PUT': getPutData(request) return put(request, obj) elif request.method == 'DELETE': getPutData(request) return delete(request, obj)
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 upload(request): res = Result() uploadfile = request.FILES.get("file") if uploadfile: filename = uploadfile.name path = request.POST.get("path", None) if path: foreignPath = path.replace("'", '"') else: foreignPath = filename galleries = request.POST.get("galleries", "1").split(",") tags = [ _.strip() for _ in request.POST.get("tags", "").split(",") if _ ] title = request.POST.get("title") description = request.POST.get("description", "") force = request.POST.get("force") try: username = request.POST.get("user", False) if username: user = User.objects.get(username=username) else: user = request.user uniqueName = request.POST.get("uid", Piece.getUniqueID(foreignPath, user)) if galleries and Gallery.objects.filter( pk__in=[int(g) for g in galleries], uploads=False): raise PermissionDenied() extension = Path(filename).ext.lower() if extension in FILE_TYPES["image"]: model = Image elif extension in FILE_TYPES["video"]: model = Video elif extension in FILE_TYPES["marmoset"]: model = Marmoset else: raise MediaTypeError( "{} is not a supported file type".format(extension)) obj, created = model.objects.get_or_create(unique_id=uniqueName, defaults={ "author": user, "hidden": False }) guid = obj.getGuid() hashVal = getHashForFile(uploadfile) if hashVal == obj.hash and not force: for gal in galleries: g = Gallery.objects.get(pk=int(gal)) obj.gallery_set.add(g) res.append(obj.json()) res.message = "Files were the same" return JsonResponse(res.asDict()) objPath = getRoot() / guid.guid[-2:] / guid.guid / filename hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() # Save uploaded files to asset folder for key, uploadfile in request.FILES.items(): if key == "file": handle_uploaded_file(hashPath, uploadfile) else: dest = objPath.parent / uploadfile.name handle_uploaded_file(dest, uploadfile) if key == "thumbnail": thumbnail = saveAsPng(dest) # Resize image = pilImage.open(thumbnail) width, height = squareCropDimensions(*image.size) image.thumbnail((width, height), pilImage.ANTIALIAS) # Crop from center box = cropBox(*image.size) image.crop(box).save(thumbnail) obj.custom_thumbnail = obj.getPath( True) / thumbnail.name obj.save() obj.hash = hashVal obj.foreign_path = foreignPath obj.title = title or objPath.namebase obj.description = description obj.export(hashVal, hashPath, tags=tags, galleries=galleries) res.append(obj.json()) except MediaTypeError as err: res.isError = True res.message = str(err) return JsonResponse(res.asDict()) else: res.isError = True res.message = "No file found" return JsonResponse(res.asDict())