Skip to content

Commit

Permalink
Merge pull request #3 from jeanmask/master
Browse files Browse the repository at this point in the history
Create redis wrapper to prevent errors on add events.
  • Loading branch information
lerrua committed Oct 31, 2014
2 parents de6563b + 259e6dc commit 137c4b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
20 changes: 11 additions & 9 deletions opps/liveblogging/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.text import slugify
Expand All @@ -13,7 +15,7 @@

class EventType(models.Model):
title = models.CharField(_("Name"), max_length=30, unique=True)
slug = models.SlugField(_(u"Identifier"), db_index=True, max_length=35)
slug = models.SlugField(_("Identifier"), db_index=True, max_length=35)
template_suffix = models.SlugField(
_("Template suffix"),
max_length=35,
Expand All @@ -35,8 +37,8 @@ def __unicode__(self):
return self.title

class Meta:
verbose_name = _(u'Event Type')
verbose_name_plural = _(u'Event Types')
verbose_name = _('Event Type')
verbose_name_plural = _('Event Types')


class Event(Container):
Expand All @@ -45,21 +47,21 @@ class Event(Container):
help_text=_("Leave blank to use default event template"))

class Meta:
verbose_name = _(u'Event')
verbose_name_plural = _(u'Events')
verbose_name = _('Event')
verbose_name_plural = _('Events')
ordering = ['-date_available', 'title', 'channel_long_slug']


class Message(Publishable):
event = models.ForeignKey('liveblogging.Event',
verbose_name=_(u'Event'))
message = models.TextField(_(u'Message'))
verbose_name=_('Event'))
message = models.TextField(_('Message'))

def save(self, *args, **kwargs):
channel = Channel.objects.get(slug=settings.OPPS_LIVEBLOGGING_CHANNEL)
self.channel = channel
super(Message, self).save(*args, **kwargs)

class Meta:
verbose_name = _(u'Message')
verbose_name_plural = _(u'Messages')
verbose_name = _('Message')
verbose_name_plural = _('Messages')
42 changes: 31 additions & 11 deletions opps/liveblogging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import json
import time
import logging
from django.db import connections
from django.http import HttpResponse
from django.http import StreamingHttpResponse
Expand All @@ -14,11 +15,27 @@
from opps.views.generic.list import ListView
from opps.views.generic.detail import DetailView
from opps.db import Db
from redis.exceptions import RedisError

import signals
from .models import Event, Message
from .forms import MessageForm


logger = logging.getLogger(__name__)


def redis_wrapper(instance, method, *args, **kwargs):
try:
return getattr(instance, method)(*args, **kwargs)
except RedisError as e:
logger.error(
'Redis error: {}'.format(e), exc_info=True, extra={
'exception': e,
}
)


class EventDetail(DetailView):
model = Event

Expand Down Expand Up @@ -136,7 +153,7 @@ def post(self, request, *args, **kwargs):
redis = Db(self.__class__.__name__, self.get_object().id)

if request.POST.get('stream', None):
redis.publish(json.dumps({"event": "stream"}))
redis_wrapper(redis, 'publish', json.dumps({"event": "stream"}))
return HttpResponse('stream')

_list = {}
Expand All @@ -146,14 +163,16 @@ def post(self, request, *args, **kwargs):
if id:
obj = Message.objects.get(id=id)

published = request.POST.get('published', True)
if published == 'false':
published = True

if request.POST.get('published') == 'false':
published = False
else:
published = True

obj.published = published

if msg:
obj.message = msg

obj.user = request.user

if published:
Expand All @@ -164,15 +183,15 @@ def post(self, request, *args, **kwargs):
sender=self.__class__, instance=obj, extra_data=_list)

obj.save()
redis.publish(json.dumps({

redis_wrapper(redis, 'publish', json.dumps({
"event": "update",
"id": id,
"published": published,
"msg": msg,
"date": obj.date_available
},
cls=DjangoJSONEncoder)
)
cls=DjangoJSONEncoder))

if published:
signals.message_post_save.send(
Expand All @@ -193,14 +212,15 @@ def post(self, request, *args, **kwargs):

signals.message_pre_save
id = obj.id
redis.publish(json.dumps({

redis_wrapper(redis, 'publish', json.dumps({
"event": "message",
"id": id,
"msg": msg,
"date": obj.date_available
},
cls=DjangoJSONEncoder)
)
cls=DjangoJSONEncoder))

_list['id_message'] = id

signals.message_post_save.send(
Expand Down

0 comments on commit 137c4b0

Please sign in to comment.