# Path: dwitter/main/models.py
# class Member(models.Model):
#     user = models.OneToOneField(User)
#     following = models.ManyToManyField('self', blank=True, symmetrical=False, related_name='followers')
#     nick = models.CharField(_(u'Name'), max_length=50)
#     descr = models.TextField(_(u'Description'),blank=True)
#     image = models.ImageField(_(u'Image'), upload_to=uploadto, default='none.png')
#     language = models.CharField(_(u'Language'), max_length=5, default='en')
# 
#     def __unicode__(self):
#         return self.user.username
# 
# 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']
# 
# 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']
# 
# Path: dwitter/main/tasks.py
# @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}))
# 
# @task
# def celery_follow(me, m):
#     dwits = Dwit.objects.filter(member=m).exclude(direct=True)
#     for dwit in dwits:
#         DPF.objects.create(member = me, dwit = dwit, stamp = dwit.stamp)
# 
# @task
# def celery_unfollow(me, m):
#     DPF.objects.filter(member = me, dwit__member=m).delete()
# 
# Path: dwitter/main/forms.py
# class MemberForm(ModelForm):
#     image = forms.ImageField(label=u'Εικόνα', required=False)
#     class Meta:
#         model = Member
#         exclude = ('user','following')
#         widgets = {'descr':widgets.Textarea(attrs={'rows':'3','class':'input-xlarge'})}
# 
# class SearchForm(forms.Form):
#     member = forms.CharField(label=_(u'Users'), required=False, help_text=_(u'Words seperated with spaces.'),
#             widget = widgets.TextInput(attrs={'placeholder':_(u'Users'),'class':'input-xlarge'}))
#     hash = forms.CharField(label=_(u'Hash tags'), required=False, help_text=_(u'Tags without # seperated with spaces.'),
#             widget = widgets.TextInput(attrs={'placeholder':_(u'Hash tags'),'class':'input-xlarge'}))


import re
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import ensure_csrf_cookie
from django.conf import settings
from django.utils.html import escape
from django.utils.translation import ugettext as _
from django.utils.translation import get_language
from dwitter.main.models import Member, Dwit, DPF
from dwitter.main.tasks import celery_dwitsaved, celery_follow, celery_unfollow
from dwitter.main.forms import MemberForm, SearchForm
from django.utils import translation
            form = MemberForm(instance=m)
            context_vars.update({'form':form,'current_lang':m.language})
    template_name='profile.html'
    context = RequestContext(request)
    return render_to_response(template_name,context_vars,context_instance=context)

@login_required
def follow(request, username):
    m = get_object_or_404(Member, user__username=username)
    me = request.user.member
    me.following.add(m)
    celery_follow.delay(me, m)
    response = HttpResponse(mimetype="text/html")
    response['content-type']="text/html; charset=UTF-8"
    response.write(_('You follow %s') % username)
    return response

@login_required
def unfollow(request, username):
    m = get_object_or_404(Member, user__username=username)
    me = request.user.member
    me.following.remove(m)
    celery_unfollow.delay(me,m)
    response = HttpResponse(mimetype="text/html")
    response['content-type']="text/html; charset=UTF-8"
    response.write(_('You stopped following %s') % username)
    return response

@login_required
def search(request):

# Path: dwitter/main/models.py
# class Member(models.Model):
#     user = models.OneToOneField(User)
#     following = models.ManyToManyField('self', blank=True, symmetrical=False, related_name='followers')
#     nick = models.CharField(_(u'Name'), max_length=50)
#     descr = models.TextField(_(u'Description'),blank=True)
#     image = models.ImageField(_(u'Image'), upload_to=uploadto, default='none.png')
#     language = models.CharField(_(u'Language'), max_length=5, default='en')
# 
#     def __unicode__(self):
#         return self.user.username
# 
# 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']
# 
# 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']
# 
# Path: dwitter/main/tasks.py
# @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}))
# 
# @task
# def celery_follow(me, m):
#     dwits = Dwit.objects.filter(member=m).exclude(direct=True)
#     for dwit in dwits:
#         DPF.objects.create(member = me, dwit = dwit, stamp = dwit.stamp)
# 
# @task
# def celery_unfollow(me, m):
#     DPF.objects.filter(member = me, dwit__member=m).delete()
# 
# Path: dwitter/main/forms.py
# class MemberForm(ModelForm):
#     image = forms.ImageField(label=u'Εικόνα', required=False)
#     class Meta:
#         model = Member
#         exclude = ('user','following')
#         widgets = {'descr':widgets.Textarea(attrs={'rows':'3','class':'input-xlarge'})}
# 
# class SearchForm(forms.Form):
#     member = forms.CharField(label=_(u'Users'), required=False, help_text=_(u'Words seperated with spaces.'),
#             widget = widgets.TextInput(attrs={'placeholder':_(u'Users'),'class':'input-xlarge'}))
#     hash = forms.CharField(label=_(u'Hash tags'), required=False, help_text=_(u'Tags without # seperated with spaces.'),
#             widget = widgets.TextInput(attrs={'placeholder':_(u'Hash tags'),'class':'input-xlarge'}))


# Path: dwitter/main/views.py
import re
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import ensure_csrf_cookie
from django.conf import settings
from django.utils.html import escape
from django.utils.translation import ugettext as _
from django.utils.translation import get_language
from dwitter.main.models import Member, Dwit, DPF
from dwitter.main.tasks import celery_dwitsaved, celery_follow, celery_unfollow
from dwitter.main.forms import MemberForm, SearchForm
from django.utils import translation

            form = MemberForm(instance=m)
            context_vars.update({'form':form,'current_lang':m.language})
    template_name='profile.html'
    context = RequestContext(request)
    return render_to_response(template_name,context_vars,context_instance=context)

@login_required
def follow(request, username):
    m = get_object_or_404(Member, user__username=username)
    me = request.user.member
    me.following.add(m)
    celery_follow.delay(me, m)
    response = HttpResponse(mimetype="text/html")
    response['content-type']="text/html; charset=UTF-8"
    response.write(_('You follow %s') % username)
    return response

@login_required
def unfollow(request, username):
    m = get_object_or_404(Member, user__username=username)
    me = request.user.member
    me.following.remove(m)
    celery_unfollow.delay(me,m)
    response = HttpResponse(mimetype="text/html")
    response['content-type']="text/html; charset=UTF-8"
    response.write(_('You stopped following %s') % username)
    return response

@login_required
def search(request):

    form = SearchForm()