Esempio n. 1
0
    def form_valid(self, form):
        news = form.save()


        photos_formset = NewsPhotoImageSet(self.request.POST,
            self.request.FILES,
            instance=news)
        is_valid = all([photos_formset.is_valid()])
        if is_valid:
            photos_formset.save()
            #save preview to redis
            photos = news.news_photos.all()
            preview = NewsPreview(news.id,news.title, photos[0].thumbnail.url if photos.count() > 0 else '', news.slug,
                                  news.event_date, news.deadline_date,news.description,
                                  photos[0].preview.url if photos.count() > 0 else '')
            preview_serialized = pickle.dumps(preview)
            redis_adapter.set(RedisKeys.preview_news_key % news.id, preview_serialized)
            return HttpResponseRedirect(self.get_success_url())

        messages.error(self.request,
            _("Your submitted data was not valid - please "
              "correct the below errors"))


        news.delete()
        ctx = self.get_context_data(form=form, photo_formset=photos_formset)
        return self.render_to_response(ctx)
Esempio n. 2
0
    def form_valid(self, form):
        news = form.save()

        photos_formset = NewsPhotoImageSet(self.request.POST,
                                           self.request.FILES,
                                           instance=news)
        is_valid = all([photos_formset.is_valid()])
        if is_valid:
            photos_formset.save()
            #save preview to redis
            photos = news.news_photos.all()
            preview = NewsPreview(news.id,news.title, photos[0].thumbnail.url if photos.count() > 0 else '', news.slug,
                                  news.event_date, news.deadline_date,news.description,
                                  photos[0].preview.url if photos.count() > 0 else '')
            preview_serialized = pickle.dumps(preview)
            redis_adapter.set(RedisKeys.preview_news_key % news.id, preview_serialized)

            if not news.is_displayed or news.is_archive:
                redis_adapter.zrem(RedisKeys.pop_news, news.id)
            elif news.date_added + timedelta(days=settings.TOP_NEWS_LIVETIME) > utc.localize(datetime.today()):
                redis_adapter.zadd(RedisKeys.pop_news, news.id, news.views_count)
            return HttpResponseRedirect(self.get_success_url())

        messages.error(self.request,
                       _("Your submitted data was not valid - please "
                         "correct the below errors"))


        news.delete()
        ctx = self.get_context_data(form=form, photo_formset=photos_formset)
        return self.render_to_response(ctx)