# Path: dwitter/main/utils.py
# def emit_to_channel(channel, event, *data):
#     r = redis_connection()
#     args = list(data)
#     r.publish('socketio_%s' % channel, json.dumps({'name': event, 'args': args}))
# 
# Path: dwitter/main/models.py
# class DPF(models.Model):    #Dwit Per Follower
#     member = models.ForeignKey(Member, db_index=True)
#     dwit = models.ForeignKey(Dwit)
#     stamp = models.DateTimeField()
# 
#     class Meta:
#         ordering = ['-stamp']
# 
# class Dwit(models.Model):
#     content = models.TextField()
#     member = models.ForeignKey(Member)
#     stamp = models.DateTimeField(auto_now_add=True)
#     tags = TaggableManager(blank=True)
#     redwit = models.ForeignKey(Member, blank=True, null=True, related_name='redwitby')
#     replyto = models.ForeignKey('self', blank=True, null=True, related_name='replyfrom')
#     direct = models.BooleanField(blank=True)
# 
#     def _dwitter_stamp(self):
#         t = datetime.now() - self.stamp
#         if t.days > 0:
#             return _(u'%dd') % t.days
#         else:
#             h = t.seconds / 3600
#             if h > 0:
#                 return _(u'%dh') % h
#             else:
#                 m = t.seconds / 60
#                 if m == 0: m = 1
#                 return _(u'%dm') % m
# 
#     dwitter_stamp = property(_dwitter_stamp)
# 
#     class Meta:
#         ordering = ['-stamp']


from django.core.mail import send_mail
from django.conf import settings
from celery import task
from dwitter.main.utils import emit_to_channel
from dwitter.main.models import DPF, Dwit
import json
import re
import subprocess
# -*- coding: utf-8 -*- 


@task
def celery_dwitsaved(dwit, rendered):
    if dwit.direct:
        fers = dwit.member.followers.filter(user__username=re.match('^\@\w+',dwit.content).group(0)[1:])
    else:
        fers = dwit.member.followers.all()
    for fer in fers:
        DPF.objects.create(member = fer, dwit = dwit, stamp = dwit.stamp)
        emit_to_channel(fer.user.username, 'newdwit', json.dumps({'content':rendered}))
    tags = dwit.tags.all()
    if tags:
        for tag in tags:

# Path: dwitter/main/utils.py
# def emit_to_channel(channel, event, *data):
#     r = redis_connection()
#     args = list(data)
#     r.publish('socketio_%s' % channel, json.dumps({'name': event, 'args': args}))
# 
# Path: dwitter/main/models.py
# class DPF(models.Model):    #Dwit Per Follower
#     member = models.ForeignKey(Member, db_index=True)
#     dwit = models.ForeignKey(Dwit)
#     stamp = models.DateTimeField()
# 
#     class Meta:
#         ordering = ['-stamp']
# 
# class Dwit(models.Model):
#     content = models.TextField()
#     member = models.ForeignKey(Member)
#     stamp = models.DateTimeField(auto_now_add=True)
#     tags = TaggableManager(blank=True)
#     redwit = models.ForeignKey(Member, blank=True, null=True, related_name='redwitby')
#     replyto = models.ForeignKey('self', blank=True, null=True, related_name='replyfrom')
#     direct = models.BooleanField(blank=True)
# 
#     def _dwitter_stamp(self):
#         t = datetime.now() - self.stamp
#         if t.days > 0:
#             return _(u'%dd') % t.days
#         else:
#             h = t.seconds / 3600
#             if h > 0:
#                 return _(u'%dh') % h
#             else:
#                 m = t.seconds / 60
#                 if m == 0: m = 1
#                 return _(u'%dm') % m
# 
#     dwitter_stamp = property(_dwitter_stamp)
# 
#     class Meta:
#         ordering = ['-stamp']


# Path: dwitter/main/tasks.py
from django.core.mail import send_mail
from django.conf import settings
from celery import task
from dwitter.main.utils import emit_to_channel
from dwitter.main.models import DPF, Dwit
import json
import re
import subprocess

# -*- coding: utf-8 -*- 


@task
def celery_dwitsaved(dwit, rendered):
    if dwit.direct:
        fers = dwit.member.followers.filter(user__username=re.match('^\@\w+',dwit.content).group(0)[1:])
    else:
        fers = dwit.member.followers.all()
    for fer in fers:
        DPF.objects.create(member = fer, dwit = dwit, stamp = dwit.stamp)
        emit_to_channel(fer.user.username, 'newdwit', json.dumps({'content':rendered}))
    tags = dwit.tags.all()
    if tags:
        for tag in tags:

            emit_to_channel(tag.name, 'newdwit', json.dumps({'content':rendered}))