Beispiel #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)

            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)
Beispiel #2
0
 def setAnalitic(self):
     city = self.object.city
     if self.object:
         pk = self.object.id
         if settings.UNIC_TMP_USER_ID in self.request.COOKIES:
             unicId = self.request.COOKIES[settings.UNIC_TMP_USER_ID]
             redis_adapter.sadd(RedisKeys.news_views % pk, unicId)
         views_count = redis_adapter.scard(RedisKeys.news_views % pk)
         if self.object.is_displayed and not self.object.is_archive and self.object.date_added + timedelta(days=settings.TOP_NEWS_LIVETIME) > utc.localize(datetime.today()):
             redis_adapter.zadd(RedisKeys.pop_news, pk, views_count)
             redis_adapter.zadd(RedisKeys.pop_news_by_group % self.object.type, pk, views_count)
             redis_adapter.zadd(RedisKeys.pop_news_by_city % city, pk, views_count)
         else:
             redis_adapter.zrem(RedisKeys.pop_news, pk)
             redis_adapter.zrem(RedisKeys.pop_news_by_group % self.object.type, pk)
             redis_adapter.zrem(RedisKeys.pop_news_by_city % city, pk)
         return views_count
     return 0;