# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from udata import mail
from udata.i18n import lazy_gettext as _
from udata.models import Organization, FollowOrg, Activity, Metrics
from udata.tasks import job, task, get_logger

log = get_logger(__name__)


@job('purge-organizations')
def purge_organizations(self):
    for organization in Organization.objects(deleted__ne=None):
        log.info('Purging organization "{0}"'.format(organization))
        # Remove followers
        FollowOrg.objects(following=organization).delete()
        # Remove activity
        Activity.objects(related_to=organization).delete()
        Activity.objects(organization=organization).delete()
        # Remove metrics
        Metrics.objects(object_id=organization.id).delete()
        # Remove
        organization.delete()


@task
def notify_membership_request(org, request):
    recipients = [m.user for m in org.by_role('admin')]
    mail.send(
        _('New membership request'), recipients, 'membership_request',
        org=org, request=request)


@task
def notify_membership_response(org, request):
    if request.status == 'accepted':
        subject, template = _(
            'You are now a member of the organization "%(org)s"', org=org),
        'new_member'
    else:
        subject, template = _('Membership refused'), 'membership_refused'
    mail.send(subject, request.user, template, org=org, request=request)


@task
def notify_new_member(org, member):
    subject = _('You are now a member of the organization "%(org)s"', org=org)
    mail.send(subject, member.user, 'new_member', org=org)

@@ -1160,36 +1160,13 @@
 ject
-, template = _(%0A            
+ = _(
 'You
@@ -1206,32 +1206,52 @@
 tion %22%25(org)s%22',
+%0A                   
  org=org),%0A     
@@ -1243,25 +1243,24 @@
 org=org)
-,
 %0A       
  'new_me
@@ -1251,16 +1251,27 @@
 %0A       
+ template =
  'new_me

Fix membership response mail