Esempio n. 1
0
 def _post_delete(self, sender, instance, **kwargs):
     """
     Invalidation upon object deletion.
     """
     # NOTE: this will behave wrong if someone changed object fields
     #       before deletion (why anyone will do that?)
     invalidate_obj(instance)
Esempio n. 2
0
def author_edit(request):
    author = request.user
    data = {}
    data['page_title'] = 'Настройки профиля'
    if request.POST:
        invalidate_obj(author)
        if 'save_profile' in request.POST:
            profile_form = AuthorEditProfileForm(request.POST, instance=author, prefix='profile_form')
            if profile_form.is_valid():
                profile_form.save()
                data['profile_ok'] = True            
        if 'save_email' in request.POST:
            email_form = AuthorEditEmailForm(request.POST, author=author, prefix='email_form')
            if email_form.is_valid():
                email_form.save()
                data['email_ok'] = True
        if 'save_password' in request.POST:
            password_form = AuthorEditPasswordForm(request.POST, author=author, prefix='password_form')
            if password_form.is_valid():
                password_form.save()
                data['password_ok'] = True
            else:
                data['password_form_nfe'] = password_form.non_field_errors()
        if 'save_prefs' in request.POST:
            prefs_form = AuthorEditPrefsForm(request.POST, author=author, prefix='prefs_form')
            if prefs_form.is_valid():
                prefs_form.save()
                data['prefs_ok'] = True
                
    profile_form = AuthorEditProfileForm(instance=author, prefix='profile_form')
    email_form = AuthorEditEmailForm(author=author, prefix='email_form')
    password_form = AuthorEditPasswordForm(author=author, prefix='password_form')
    prefs_form = AuthorEditPrefsForm(author=author, prefix='prefs_form')
    data.update({'profile_form': profile_form, 'email_form': email_form, 'password_form': password_form, 'prefs_form': prefs_form})
    return render(request, 'author_profile_edit.html', data)
Esempio n. 3
0
def invalidate_m2m(sender=None, instance=None, model=None, action=None, pk_set=None, **kwargs):
    """
    Invoke invalidation on m2m changes.
    """
    if action in ('post_add', 'post_remove', 'post_clear'):
        invalidate_model(sender) # NOTE: this is harsh, but what's the alternative?
        invalidate_obj(instance)
Esempio n. 4
0
 def _post_delete(self, sender, instance, **kwargs):
     """
     Invalidation upon object deletion.
     """
     # NOTE: this will behave wrong if someone changed object fields
     #       before deletion (why anyone will do that?)
     invalidate_obj(instance)
Esempio n. 5
0
 def delete(self, request, *args, **kwargs):
     self.chapter = self.get_object()
     self.story.chapter_set.filter(order__gt=self.chapter.order).update(order=F('order')-1)
     for chapter in self.story.chapter_set.filter(order__gt=self.chapter.order):
         invalidate_obj(chapter)
     self.chapter.delete()
     return redirect('story_edit', self.story.id)
Esempio n. 6
0
def story_bookmark(request, pk):
    from ponyFiction.models import Bookmark
    story = get_object_or_404(Story.objects.accessible(user=request.user), pk=pk)
    (bookmark, created) = Bookmark.objects.get_or_create(story=story, author=request.user)
    if not created:
        bookmark.delete()
    invalidate_obj(story)
    return redirect('bookmarks')
Esempio n. 7
0
def story_favorite(request, pk):
    from ponyFiction.models import Favorites
    story = get_object_or_404(Story.objects.accessible(user=request.user), pk=pk)
    (favorite, created) = Favorites.objects.get_or_create(story=story, author=request.user)
    if not created:
        favorite.delete()
    invalidate_obj(story)
    return redirect('favorites', request.user.id)
Esempio n. 8
0
def story_bookmark_ajax(request, story_id):
    """ Добавление рассказа в закладки """
    
    story = get_object_or_404(Story.objects.accessible(user=request.user), pk=story_id)
    (bookmark, created) = Bookmark.objects.get_or_create(story=story, author=request.user)
    if not created:
        bookmark.delete()
    invalidate_obj(story)
    return HttpResponse(story_id)
Esempio n. 9
0
 def delete(self, request, *args, **kwargs):
     self.chapter = self.get_object()
     self.story.chapter_set.filter(order__gt=self.chapter.order).update(
         order=F('order') - 1)
     for chapter in self.story.chapter_set.filter(
             order__gt=self.chapter.order):
         invalidate_obj(chapter)
     self.chapter.delete()
     return redirect('story_edit', self.story.id)
Esempio n. 10
0
def invalidate_m2m(sender=None, instance=None, model=None, action=None, pk_set=None, **kwargs):
    """
    Invoke invalidation on m2m changes.
    """
    if action in ("post_add", "post_remove", "post_clear"):
        invalidate_model(sender)  # NOTE: this is harsh, but what's the alternative?
        invalidate_obj(instance)
        # TODO: more granular invalidation for referenced models
        invalidate_model(model)
Esempio n. 11
0
def story_favorite_ajax(request, story_id):
    """ Добавление рассказа в избранное """

    story = get_object_or_404(Story.objects.accessible(user=request.user), pk=story_id)
    (favorite, created) = Favorites.objects.get_or_create(story=story, author=request.user)
    if not created:
        favorite.delete()
    invalidate_obj(story)
    return HttpResponse(story_id)
Esempio n. 12
0
def story_favorite_ajax(request, story_id):
    """ Добавление рассказа в избранное """

    story = get_object_or_404(Story.objects.accessible(user=request.user),
                              pk=story_id)
    (favorite, created) = Favorites.objects.get_or_create(story=story,
                                                          author=request.user)
    if not created:
        favorite.delete()
    invalidate_obj(story)
    return HttpResponse(story_id)
Esempio n. 13
0
def story_bookmark_ajax(request, story_id):
    """ Добавление рассказа в закладки """

    story = get_object_or_404(Story.objects.accessible(user=request.user),
                              pk=story_id)
    (bookmark, created) = Bookmark.objects.get_or_create(story=story,
                                                         author=request.user)
    if not created:
        bookmark.delete()
    invalidate_obj(story)
    return HttpResponse(story_id)
Esempio n. 14
0
def invalidate_m2m(sender=None,
                   instance=None,
                   model=None,
                   action=None,
                   pk_set=None,
                   **kwargs):
    """
    Invoke invalidation on m2m changes.
    """
    if action in ('post_add', 'post_remove', 'post_clear'):
        invalidate_model(
            sender)  # NOTE: this is harsh, but what's the alternative?
        invalidate_obj(instance)
Esempio n. 15
0
    def _post_save(self, sender, instance, **kwargs):
        """
        Invokes invalidations for both old and new versions of saved object
        """
        old = _old_objs[sender].pop(instance.pk, None)
        if old:
            invalidate_obj(old)
        invalidate_obj(instance)

        # Enabled cache_on_save makes us write saved object to cache.
        # Later it can be retrieved with .get(<cache_on_save_field>=<value>)
        # <cache_on_save_field> is pk unless specified.
        # This sweet trick saves a db request and helps with slave lag.
        cache_on_save = instance._cacheprofile.get('cache_on_save')
        if cache_on_save:
            # HACK: We get this object "from field" so it can contain
            #       some undesirable attributes or other objects attached.
            #       RelatedField accessors do that, for example.
            #
            #       So we strip down any _*_cache attrs before saving
            #       and later reassign them
            # Stripping up undesirable attributes
            unwanted_attrs = [
                k for k in instance.__dict__.keys()
                if k.startswith('_') and k.endswith('_cache')
            ]
            unwanted_dict = dict(
                (k, instance.__dict__[k]) for k in unwanted_attrs)
            for k in unwanted_attrs:
                del instance.__dict__[k]

            key = cache_on_save if isinstance(cache_on_save,
                                              basestring) else 'pk'
            # Django doesn't allow filters like related_id = 1337.
            # So we just hacky strip _id from end of a key
            # TODO: make it right, _meta.get_field() should help
            filter_key = key[:-3] if key.endswith('_id') else key

            cond = {filter_key: getattr(instance, key)}
            qs = sender.objects.inplace().filter(**cond).order_by()
            if MAX_GET_RESULTS:
                qs = qs[:MAX_GET_RESULTS + 1]
            qs._cache_results(qs._cache_key(), [instance])

            # Reverting stripped attributes
            instance.__dict__.update(unwanted_dict)
Esempio n. 16
0
    def _post_save(self, sender, instance, **kwargs):
        """
        Invokes invalidations for both old and new versions of saved object
        """
        old = _old_objs[sender].pop(instance.pk, None)
        if old:
            invalidate_obj(old)
        invalidate_obj(instance)

        # Enabled cache_on_save makes us write saved object to cache.
        # Later it can be retrieved with .get(<cache_on_save_field>=<value>)
        # <cache_on_save_field> is pk unless specified.
        # This sweet trick saves a db request and helps with slave lag.
        cache_on_save = instance._cacheprofile.get('cache_on_save')
        if cache_on_save:
            # HACK: We get this object "from field" so it can contain
            #       some undesirable attributes or other objects attached.
            #       RelatedField accessors do that, for example.
            #
            #       So we strip down any _*_cache attrs before saving
            #       and later reassign them
            # Stripping up undesirable attributes
            unwanted_attrs = [k for k in instance.__dict__.keys()
                                if k.startswith('_') and k.endswith('_cache')]
            unwanted_dict = dict((k, instance.__dict__[k]) for k in unwanted_attrs)
            for k in unwanted_attrs:
                del instance.__dict__[k]

            key = 'pk' if cache_on_save is True else cache_on_save
            # Django doesn't allow filters like related_id = 1337.
            # So we just hacky strip _id from end of a key
            # TODO: make it right, _meta.get_field() should help
            filter_key = key[:-3] if key.endswith('_id') else key

            cond = {filter_key: getattr(instance, key)}
            qs = sender.objects.inplace().filter(**cond).order_by()
            if MAX_GET_RESULTS:
                qs = qs[:MAX_GET_RESULTS + 1]
            qs._cache_results(qs._cache_key(), [instance])

            # Reverting stripped attributes
            instance.__dict__.update(unwanted_dict)
Esempio n. 17
0
    def _post_save(self, sender, instance, **kwargs):
        """
        Invokes invalidations for both old and new versions of saved object
        """
        old = _old_objs[get_model_name(instance.__class__)].pop(instance.pk, None)
        if old:
            invalidate_obj(old)
        invalidate_obj(instance)

        # Enabled cache_on_save makes us write saved object to cache.
        # Later it can be retrieved with .get(<cache_on_save_field>=<value>)
        # <cache_on_save_field> is pk unless specified.
        # This sweet trick saves a db request and helps with slave lag.
        cache_on_save = instance._cacheprofile.get('cache_on_save')
        if cache_on_save:
            # HACK: We get this object "from field" so it can contain
            #       some undesirable attributes or other objects attached.
            #       RelatedField accessors do that, for example.
            #
            #       So we strip down any _*_cache attrs before saving
            #       and later reassign them
            # Stripping up undesirable attributes
            unwanted_attrs = [k for k in instance.__dict__.keys() if k.startswith('_') and k.endswith('_cache')]
            unwanted_dict = dict((k, instance.__dict__[k]) for k in unwanted_attrs)
            for k in unwanted_attrs:
                del instance.__dict__[k]

            key = cache_on_save if isinstance(cache_on_save, basestring) else 'pk'
            # Django doesn't allow filters like related_id = 1337.
            # So we just hacky strip _id from end of a key
            # TODO: make it right, _meta.get_field() should help
            filter_key = key[:-3] if key.endswith('_id') else key
            cacheoped_as(instance.__class__.objects \
                .filter(**{filter_key: getattr(instance, key)}), extra='') \
                (lambda: [instance])()

            # Reverting stripped attributes
            instance.__dict__.update(unwanted_dict)