예제 #1
0
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())
예제 #2
0
파일: piece.py 프로젝트: nickryand/Frog
def delete(self, request, obj):
    obj.deleted = True
    obj.save()
    res = Result()
    res.isSuccess = True
    res.value = obj.json()

    return JsonResponse(res)
예제 #3
0
파일: piece.py 프로젝트: davideilering/Frog
def delete(self, request, obj):
    obj.deleted = True
    obj.save()
    res = Result()
    res.isSuccess = True
    res.value = obj.json()

    return JsonResponse(res)
예제 #4
0
def manage(request):
    if request.method == 'GET':
        guids = request.GET.get('guids', '').split(',')
        guids = filter(None, guids)

        objects = getObjectsFromGuids(guids)
        ids = [o.id for o in objects]

        imgtags = list(
            Tag.objects.filter(image__id__in=ids).exclude(artist=True))
        vidtags = list(
            Tag.objects.filter(video__id__in=ids).exclude(artist=True))
        tags = list(set(imgtags + vidtags))

        if request.GET.get('json', False):
            res = Result()
            data = {
                'queries': connection.queries,
            }

            res.value = data

            res.isSuccess = True

            return JsonResponse(res)

        return render(request, 'frog/tag_manage.html', {'tags': tags})
    else:
        add = request.POST.get('add', '').split(',')
        rem = request.POST.get('rem', '').split(',')
        guids = request.POST.get('guids', '').split(',')

        add = filter(None, add)
        rem = filter(None, rem)
        addList = []

        for t in add:
            try:
                addList.append(int(t))
            except ValueError:
                tag, created = Tag.objects.get_or_create(name=t)
                tag.save()
                addList.append(tag.id)

        objects = getObjectsFromGuids(guids)
        addTags = Tag.objects.filter(id__in=addList)
        remTags = Tag.objects.filter(id__in=rem)

        for o in objects:
            for a in addTags:
                o.tags.add(a)
            for r in remTags:
                o.tags.remove(r)

        res = Result()
        res.isSuccess = True

        return JsonResponse(res)
예제 #5
0
파일: tag.py 프로젝트: nickryand/Frog
def manage(request):
    if request.method == "GET":
        guids = request.GET.get("guids", "").split(",")
        guids = filter(None, guids)

        objects = getObjectsFromGuids(guids)
        ids = [o.id for o in objects]

        imgtags = list(Tag.objects.filter(image__id__in=ids).exclude(artist=True))
        vidtags = list(Tag.objects.filter(video__id__in=ids).exclude(artist=True))
        tags = list(set(imgtags + vidtags))

        if request.GET.get("json", False):
            res = Result()
            data = {"queries": connection.queries}

            res.value = data

            res.isSuccess = True

            return JsonResponse(res)

        return render(request, "frog/tag_manage.html", {"tags": tags})
    else:
        add = request.POST.get("add", "").split(",")
        rem = request.POST.get("rem", "").split(",")
        guids = request.POST.get("guids", "").split(",")

        add = filter(None, add)
        rem = filter(None, rem)
        addList = []

        for t in add:
            try:
                addList.append(int(t))
            except ValueError:
                tag, created = Tag.objects.get_or_create(name=t)
                tag.save()
                addList.append(tag.id)

        objects = getObjectsFromGuids(guids)
        addTags = Tag.objects.filter(id__in=addList)
        remTags = Tag.objects.filter(id__in=rem)

        for o in objects:
            for a in addTags:
                o.tags.add(a)
            for r in remTags:
                o.tags.remove(r)

        res = Result()
        res.isSuccess = True

        return JsonResponse(res)
예제 #6
0
파일: gallery.py 프로젝트: nickryand/Frog
def _filter(request, object_, tags=None, models=(Image, Video), rng=None, more=False):
    """Filters Piece objects from self based on filters, search, and range

    :param tags: List of tag IDs to filter
    :type tags: list
    :param models: List of model classes to filter on
    :type models: list
    :param rng: Range of objects to return. i.e. 0:100
    :type rng: str
    :param more -- bool, Returns more of the same filtered set of images based on session range

    return list, Objects filtered
    """
    
    NOW = time.clock()

    res = Result()
    
    idDict = {}
    objDict = {}
    lastIDs = {}
    data = {}

    LOGGER.debug('init: %f' % (time.clock() - NOW))
    if request.user.is_anonymous():
        gRange = 300
    else:
        Prefs = json.loads(UserPref.objects.get(user=request.user).data)
        gRange = Prefs['batchSize']
    request.session.setdefault('frog_range', '0:%i' % gRange)

    if rng:
        s, e = [int(x) for x in rng.split(':')]
    else:
        if more:
            s = int(request.session.get('frog_range', '0:%i' % gRange).split(':')[1])
            e = s + gRange
            s, e = 0, gRange
        else:
            s, e = 0, gRange

    ## -- Gat all IDs for each model
    for m in models:
        indexes = list(m.model_class().objects.all().values_list('id', flat=True))
        if not indexes:
            continue

        lastIndex = indexes[0]
        if more:
            ## -- This is a request for more results
            idx = request.session.get('last_%s_id' % m.model, lastIndex + 1)
            lastIDs.setdefault('last_%s_id' % m.model, idx)
        else:
            lastIDs['last_%s_id' % m.model] = lastIndex + 1
        
        ## -- Start with objects within range
        idDict[m.model] = m.model_class().objects.filter(gallery=object_, id__lt=lastIDs['last_%s_id' % m.model])
        LOGGER.debug(m.model + '_initial_query: %f' % (time.clock() - NOW))

        if tags:
            for bucket in tags:
                searchQuery = ""
                o = None
                for item in bucket:
                    ## -- filter by tag
                    if isinstance(item, int) or isinstance(item, long):
                        if not o:
                            o = Q()
                        o |= Q(tags__id=item)
                    ## -- add to search string
                    else:
                        searchQuery += item + ' '
                        if not HAYSTACK:
                            if not o:
                                o = Q()
                            ## -- use a basic search
                            LOGGER.debug('search From LIKE')
                            o |= Q(title__icontains=item)
                if HAYSTACK and searchQuery != "":
                    ## -- once all tags have been filtered, filter by search
                    searchIDs = search(searchQuery, m.model_class())
                    if searchIDs:
                        if not o:
                            o = Q()
                        LOGGER.debug('searchFrom haystack:' + str(searchIDs))
                        o |= Q(id__in=searchIDs)

                if o:
                    ## -- apply the filters
                    idDict[m.model] = idDict[m.model].filter(o)
                else:
                    idDict[m.model] = idDict[m.model].none()

            LOGGER.debug(m.model + '_added_buckets(%i): %f' % (len(tags), time.clock() - NOW))
        
        ## -- Get all ids of filtered objects, this will be a very fast query
        idDict[m.model] = list(idDict[m.model].values_list('id', flat=True))
        LOGGER.debug(m.model + '_queried_ids: %f' % (time.clock() - NOW))

        res.message = str(s) + ':' + str(e)
        
        ## -- perform the main query to retrieve the objects we want
        objDict[m.model] = m.model_class().objects.filter(id__in=idDict[m.model]).select_related('author').prefetch_related('tags')
        if not rng:
            objDict[m.model] = objDict[m.model][:gRange]
        objDict[m.model] = list(objDict[m.model])
        LOGGER.debug(m.model + '_queried_obj: %f' % (time.clock() - NOW))
    
    
    ## -- combine and sort all objects by date
    objects = _sortObjects(**objDict) if len(models) > 1 else objDict.values()[0]
    objects = objects[s:e]
    LOGGER.debug('sorted: %f' % (time.clock() - NOW))

    ## -- serialize objects
    for i in objects:
        for m in models:
            if isinstance(i, m.model_class()):
                ## -- set the last ID per model for future lookups
                lastIDs['last_%s_id' % m.model] = i.id
                data['last_%s_id' % m.model] = i.id
        res.append(i.json())
    LOGGER.debug('serialized: %f' % (time.clock() - NOW))

    request.session['frog_range'] = ':'.join((str(s),str(e)))

    LOGGER.debug('total: %f' % (time.clock() - NOW))
    request.session['last_image_id'] = lastIDs.get('last_image_id', 0)
    request.session['last_video_id'] = lastIDs.get('last_video_id', 0)
    
    data['count'] = len(objects)
    data['queries'] = connection.queries

    res.value = data

    res.isSuccess = True

    return JsonResponse(res)
예제 #7
0
파일: piece.py 프로젝트: krashman/Frog
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())
예제 #8
0
파일: gallery.py 프로젝트: krashman/Frog
def _filter(request,
            object_,
            tags=None,
            models=(Image, Video),
            more=False,
            orderby='created'):
    """Filters Piece objects from self based on filters, search, and range

    :param tags: List of tag IDs to filter
    :type tags: list
    :param models: List of model classes to filter on
    :type models: list
    :param more -- bool, Returns more of the same filtered set of images based on session range

    return list, Objects filtered
    """
    res = Result()

    idDict = {}
    objDict = {}
    data = {}
    length = 300

    if more:
        # -- This is a request for more results
        mult = request.session.get('mult', 0) + 1
        request.session['mult'] = mult
    else:
        mult = 0
        request.session['mult'] = 0

    start = length * mult
    end = start + length

    LOGGER.debug('{} : {}'.format(start, end))

    # -- Gat all IDs for each model
    for m in models:
        idDict[m.model] = m.model_class().objects.filter(gallery=object_)

        if tags:
            for bucket in tags:
                searchQuery = ""
                o = None
                for item in bucket:
                    if item == 0:
                        # -- filter by tagless
                        idDict[m.model].annotate(num_tags=Count('tags'))
                        if not o:
                            o = Q()
                        o |= Q(num_tags__lte=1)
                        break
                    elif isinstance(item, six.integer_types):
                        # -- filter by tag
                        if not o:
                            o = Q()
                        o |= Q(tags__id=item)
                    else:
                        # -- add to search string
                        searchQuery += item + ' '
                        if not HAYSTACK:
                            if not o:
                                o = Q()
                            # -- use a basic search
                            o |= Q(title__icontains=item)

                if HAYSTACK and searchQuery != "":
                    # -- once all tags have been filtered, filter by search
                    searchIDs = search(searchQuery, m.model_class())
                    if searchIDs:
                        if not o:
                            o = Q()
                        o |= Q(id__in=searchIDs)

                if o:
                    # -- apply the filters
                    idDict[m.model] = idDict[m.model].annotate(
                        num_tags=Count('tags')).filter(o)
                else:
                    idDict[m.model] = idDict[m.model].none()

        # -- Get all ids of filtered objects, this will be a very fast query
        idDict[m.model] = list(idDict[m.model].order_by(
            '-{}'.format(orderby))[:end].values_list('id', flat=True))

        # -- perform the main query to retrieve the objects we want
        objDict[m.model] = m.model_class().objects.filter(
            id__in=idDict[m.model]).select_related('author').prefetch_related(
                'tags').order_by('-{}'.format(orderby))
        objDict[m.model] = objDict[m.model][start:end]
        objDict[m.model] = list(objDict[m.model])

    # -- combine and sort all objects by date
    objects = _sortObjects(orderby, **
                           objDict) if len(models) > 1 else objDict.values()[0]
    objects = objects[:length]

    # -- serialize objects
    for i in objects:
        res.append(i.json())

    data['count'] = len(objects)
    if settings.DEBUG:
        data['queries'] = connection.queries

    res.value = data

    return JsonResponse(res.asDict())
예제 #9
0
파일: gallery.py 프로젝트: theiviaxx/Frog
def _filter(request, object_, tags=None, more=False, orderby='created'):
    """Filters Piece objects from self based on filters, search, and range

    :param tags: List of tag IDs to filter
    :type tags: list
    :param more -- bool, Returns more of the same filtered set of images based on session range

    return list, Objects filtered
    """
    res = Result()

    models = QUERY_MODELS

    idDict = {}
    objDict = {}
    data = {}
    modelmap = {}
    length = 75

    # -- Get all IDs for each model
    for m in models:
        modelmap[m.model_class()] = m.model

        if object_:
            idDict[m.model] = m.model_class().objects.filter(gallery=object_)
        else:
            idDict[m.model] = m.model_class().objects.all()

        if idDict[m.model] is None:
            continue

        if tags:
            for bucket in tags:
                searchQuery = ""
                o = None
                for item in bucket:
                    if item == 0:
                        # -- filter by tagless
                        idDict[m.model].annotate(num_tags=Count('tags'))
                        if not o:
                            o = Q()
                        o |= Q(num_tags__lte=1)
                        break
                    elif isinstance(item, six.integer_types):
                        # -- filter by tag
                        if not o:
                            o = Q()
                        o |= Q(tags__id=item)
                    else:
                        # -- add to search string
                        searchQuery += item + ' '
                        if not HAYSTACK:
                            if not o:
                                o = Q()
                            # -- use a basic search
                            o |= Q(title__icontains=item)

                if HAYSTACK and searchQuery != "":
                    # -- once all tags have been filtered, filter by search
                    searchIDs = search(searchQuery, m.model_class())
                    if searchIDs:
                        if not o:
                            o = Q()
                        o |= Q(id__in=searchIDs)

                if o:
                    # -- apply the filters
                    idDict[m.model] = idDict[m.model].annotate(num_tags=Count('tags')).filter(o)
                else:
                    idDict[m.model] = idDict[m.model].none()

        # -- Get all ids of filtered objects, this will be a very fast query
        idDict[m.model] = list(idDict[m.model].order_by('-{}'.format(orderby)).values_list('id', flat=True))
        lastid = request.session.get('last_{}'.format(m.model), 0)
        if not idDict[m.model]:
            continue

        if not more:
            lastid = idDict[m.model][0]

        index = idDict[m.model].index(lastid)
        if more and lastid != 0:
            index += 1
        idDict[m.model] = idDict[m.model][index:index + length]

        # -- perform the main query to retrieve the objects we want
        objDict[m.model] = m.model_class().objects.filter(id__in=idDict[m.model])
        objDict[m.model] = objDict[m.model].select_related('author').prefetch_related('tags').order_by('-{}'.format(orderby))
        objDict[m.model] = list(objDict[m.model])

        # -- combine and sort all objects by date
    objects = _sortObjects(orderby, **objDict) if len(models) > 1 else objDict.values()[0]
    objects = objects[:length]

    # -- Find out last ids
    lastids = {}
    for obj in objects:
        lastids['last_{}'.format(modelmap[obj.__class__])] = obj.id

    for key, value in lastids.items():
        request.session[key] = value

    # -- serialize objects
    for i in objects:
        res.append(i.json())

    data['count'] = len(objects)
    if settings.DEBUG:
        data['queries'] = connection.queries

    res.value = data

    return JsonResponse(res.asDict())
예제 #10
0
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())
예제 #11
0
파일: gallery.py 프로젝트: ptrr/frog
def _filter(request, object_, tags=None, more=False, orderby="created"):
    """Filters Piece objects from self based on filters, search, and range

    :param tags: List of tag IDs to filter
    :type tags: list
    :param more -- bool, Returns more of the same filtered set of images based on session range

    return list, Objects filtered
    """
    res = Result()

    idDict = {}
    objDict = {}
    data = {}
    modelmap = {}

    # Get all IDs for each model
    for m in QUERY_MODELS:
        modelmap[m.model_class()] = m.model

        if object_:
            idDict[m.model] = m.model_class().objects.filter(gallery=object_)
        else:
            idDict[m.model] = m.model_class().objects.all()

        if idDict[m.model] is None:
            continue

        if tags:
            for bucket in tags:
                searchQuery = ""
                o = None
                for item in bucket:
                    if item == 0:
                        # filter by tagless
                        idDict[m.model].annotate(num_tags=Count("tags"))
                        if not o:
                            o = Q()
                        o |= Q(num_tags__lte=1)
                        break
                    elif isinstance(item, six.integer_types):
                        # filter by tag
                        if not o:
                            o = Q()
                        o |= Q(tags__id=item)
                    else:
                        # add to search string
                        searchQuery += item + " "
                        if not HAYSTACK:
                            if not o:
                                o = Q()
                            # use a basic search
                            o |= Q(title__icontains=item)

                if HAYSTACK and searchQuery != "":
                    # once all tags have been filtered, filter by search
                    searchIDs = search(searchQuery, m.model_class())
                    if searchIDs:
                        if not o:
                            o = Q()
                        o |= Q(id__in=searchIDs)

                if o:
                    # apply the filters
                    idDict[m.model] = (idDict[m.model].annotate(
                        num_tags=Count("tags")).filter(o))
                else:
                    idDict[m.model] = idDict[m.model].none()

        # Remove hidden items before slicing so we get an accurate count
        idDict[m.model] = idDict[m.model].exclude(hidden=True)

        # Remove deleted items before slicing so we get an accurate count
        idDict[m.model] = idDict[m.model].exclude(deleted=True)

        # Get all ids of filtered objects, this will be a very fast query
        idDict[m.model] = list(idDict[m.model].order_by(
            "-{}".format(orderby)).values_list("id", flat=True))
        lastid = request.session.get("last_{}".format(m.model), 0)
        if not idDict[m.model]:
            continue

        if not more:
            lastid = idDict[m.model][0]

        try:
            index = idDict[m.model].index(lastid)
        except ValueError:
            index = 0

        if more and lastid != 0:
            index += 1
        idDict[m.model] = idDict[m.model][index:index + BATCH_LENGTH]

        # perform the main query to retrieve the objects we want
        objDict[m.model] = m.model_class().objects.filter(
            id__in=idDict[m.model])
        objDict[m.model] = (objDict[m.model].select_related(
            "author").prefetch_related("tags").order_by("-{}".format(orderby)))
        objDict[m.model] = list(objDict[m.model])

    # combine and sort all objects by date
    objects = _sortObjects(orderby, **objDict)
    objects = objects[:BATCH_LENGTH]

    # Find out last ids
    lastids = {}
    for obj in objects:
        lastids["last_{}".format(modelmap[obj.__class__])] = obj.id

    for key, value in lastids.items():
        request.session[key] = value

    # serialize objects
    for i in objects:
        res.append(i.json())

    data["count"] = len(objects)
    if settings.DEBUG:
        data["queries"] = connection.queries

    res.value = data

    return JsonResponse(res.asDict())