Exemplo n.º 1
0
    def book_changes(cls, request=None, since=0, until=None, fields=None):
        since = datetime.fromtimestamp(int(since))
        until = cls.until(until)

        changes = {"time_checked": timestamp(until)}

        if not fields:
            fields = cls.fields(request, "book_fields")

        added = []
        updated = []
        deleted = []

        last_change = since
        for book in Book.objects.filter(changed_at__gte=since, changed_at__lt=until).iterator():
            book_d = cls.book_dict(book, fields)
            updated.append(book_d)
        if updated:
            changes["updated"] = updated

        for book in Deleted.objects.filter(
            content_type=Book, deleted_at__gte=since, deleted_at__lt=until, created_at__lt=since
        ).iterator():
            deleted.append(book.id)
        if deleted:
            changes["deleted"] = deleted

        return changes
Exemplo n.º 2
0
    def changes(cls, request=None, since=0, until=None, book_fields=None, tag_fields=None, tag_categories=None):
        until = cls.until(until)
        since = int(since)

        if not since:
            cache = get_cache("api")
            key = hash((book_fields, tag_fields, tag_categories, tuple(sorted(request.GET.items()))))
            value = cache.get(key)
            if value is not None:
                return value

        changes = {"time_checked": timestamp(until)}

        changes_by_type = {
            "books": cls.book_changes(request, since, until, book_fields),
            "tags": cls.tag_changes(request, since, until, tag_fields, tag_categories),
        }

        for model in changes_by_type:
            for field in changes_by_type[model]:
                if field == "time_checked":
                    continue
                changes.setdefault(field, {})[model] = changes_by_type[model][field]

        if not since:
            cache.set(key, changes)

        return changes
Exemplo n.º 3
0
    def book_changes(cls, request=None, since=0, until=None, fields=None):
        since = datetime.fromtimestamp(int(since))
        until = cls.until(until)

        changes = {'time_checked': timestamp(until)}

        if not fields:
            fields = cls.fields(request, 'book_fields')

        added = []
        updated = []
        deleted = []

        last_change = since
        for book in Book.objects.filter(changed_at__gte=since,
                                        changed_at__lt=until).iterator():
            book_d = cls.book_dict(book, fields)
            updated.append(book_d)
        if updated:
            changes['updated'] = updated

        for book in Deleted.objects.filter(content_type=Book,
                                           deleted_at__gte=since,
                                           deleted_at__lt=until,
                                           created_at__lt=since).iterator():
            deleted.append(book.id)
        if deleted:
            changes['deleted'] = deleted

        return changes
 def handle(self, **options):
     # those should be versioned
     last_checked = timestamp(datetime.now())
     db = init_db(last_checked)
     for b in Book.objects.all():
         add_book(db, b)
     for t in Tag.objects.exclude(
             category__in=('book', 'set', 'theme')).exclude(book_count=0):
         # only add non-empty tags
         add_tag(db, t)
     db.commit()
     db.close()
     current(last_checked)
Exemplo n.º 5
0
 def handle(self, **options):
     # those should be versioned
     last_checked = timestamp(datetime.now())
     db = init_db(last_checked)
     for b in Book.objects.all():
         add_book(db, b)
     for t in Tag.objects.exclude(
             category__in=('book', 'set', 'theme')).exclude(items=None):
         # only add non-empty tags
         add_tag(db, t)
     db.commit()
     db.close()
     current(last_checked)
Exemplo n.º 6
0
    def tag_changes(cls,
                    request=None,
                    since=0,
                    until=None,
                    fields=None,
                    categories=None):
        since = datetime.fromtimestamp(int(since))
        until = cls.until(until)

        changes = {'time_checked': timestamp(until)}

        if not fields:
            fields = cls.fields(request, 'tag_fields')
        if not categories:
            categories = cls.fields(request, 'tag_categories')

        all_categories = ('author', 'epoch', 'kind', 'genre')
        if categories:
            categories = (c for c in categories if c in all_categories)
        else:
            categories = all_categories

        updated = []
        deleted = []

        for tag in Tag.objects.filter(category__in=categories,
                                      changed_at__gte=since,
                                      changed_at__lt=until).iterator():
            # only serve non-empty tags
            if tag.book_count:
                tag_d = cls.tag_dict(tag, fields)
                updated.append(tag_d)
            elif tag.created_at < since:
                deleted.append(tag.id)
        if updated:
            changes['updated'] = updated

        for tag in Deleted.objects.filter(category__in=categories,
                                          content_type=Tag,
                                          deleted_at__gte=since,
                                          deleted_at__lt=until,
                                          created_at__lt=since).iterator():
            deleted.append(tag.id)
        if deleted:
            changes['deleted'] = deleted

        return changes
Exemplo n.º 7
0
    def tag_changes(cls, request=None, since=0, until=None, fields=None, categories=None):
        since = datetime.fromtimestamp(int(since))
        until = cls.until(until)

        changes = {
            'time_checked': timestamp(until)
        }

        if not fields:
            fields = cls.fields(request, 'tag_fields')
        if not categories:
            categories = cls.fields(request, 'tag_categories')

        all_categories = ('author', 'epoch', 'kind', 'genre')
        if categories:
            categories = (c for c in categories if c in all_categories)
        else:
            categories = all_categories

        updated = []
        deleted = []

        for tag in Tag.objects.filter(category__in=categories, 
                    changed_at__gte=since,
                    changed_at__lt=until).iterator():
            # only serve non-empty tags
            if tag.book_count:
                tag_d = cls.tag_dict(tag, fields)
                updated.append(tag_d)
            elif tag.created_at < since:
                deleted.append(tag.id)
        if updated:
            changes['updated'] = updated

        for tag in Deleted.objects.filter(category__in=categories,
                content_type=Tag, 
                    deleted_at__gte=since,
                    deleted_at__lt=until,
                    created_at__lt=since).iterator():
            deleted.append(tag.id)
        if deleted:
            changes['deleted'] = deleted

        return changes
Exemplo n.º 8
0
    def changes(cls,
                request=None,
                since=0,
                until=None,
                book_fields=None,
                tag_fields=None,
                tag_categories=None):
        until = cls.until(until)
        since = int(since)

        if not since:
            cache = get_cache('api')
            key = hash((book_fields, tag_fields, tag_categories,
                        tuple(sorted(request.GET.items()))))
            value = cache.get(key)
            if value is not None:
                return value

        changes = {'time_checked': timestamp(until)}

        changes_by_type = {
            'books':
            cls.book_changes(request, since, until, book_fields),
            'tags':
            cls.tag_changes(request, since, until, tag_fields, tag_categories),
        }

        for model in changes_by_type:
            for field in changes_by_type[model]:
                if field == 'time_checked':
                    continue
                changes.setdefault(field,
                                   {})[model] = changes_by_type[model][field]

        if not since:
            cache.set(key, changes)

        return changes