#should contain doctest sequence for the whole app
"""
Andy's notes:
============


>>> 2 + 2
4


#db will be initialised with roles and languages.
#create leads for ReportLab staff
#tag some of them
#go through mail-sending sequence
#verify in the queue
#call management command to send mails
#verify queue empty, inspect mails in fake outbox

#now repeat with somebody unsubscribed.


# Create 6 leads @reportlab.com
>>> from models import Lead, CRMTag

>>> Lead.objects.count()
5



# create a user with staff rights using ORM, so we can log in later
>>> from django.contrib.auth.models import User
>>> user = User(username="fred", is_staff=True)
>>> user.set_password("secret")
>>> user.save()
>>> from django.test.client import Client
>>> c = Client()
>>> c.login(username="fred", password="secret")
True
>>> response = c.get('/admin/')
>>> response.status_code
200
>>> c.logout()

# Send test email, check and clean
>>> from django.core import mail
>>> mail.outbox
[]
>>> mail.send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)
1
>>> len(mail.outbox)
1
>>> mail.outbox = []
>>> len(mail.outbox)
0


# Tag leads that contain an 'd' and count
>>> CRMTag.objects.count()
1
>>> tag = CRMTag(id='contains_d', description="Leads that have 'd' in their email address")
>>> tag.save()
>>> CRMTag.objects.count()
2

>>> for lead in Lead.objects.all():
...     if lead.email.find('d') >= 0:
...         lead.tags.add(tag)
...         lead.save()
...
>>> leads = Lead.objects.filter(tags=tag)
>>> leads.count()
3


# Create communication object
>>> from models import Communication
>>> communication = Communication(subject="Communication test", message_plain="Email content")
>>> communication.save()
>>> comm_id = communication.id

>>> #add staff_required; try to access screen without login, verify rejected;
>>> resp = c.get('/admin/crm/send_emails/', {'tag_id':'contains_d', 'id':comm_id, 'send':'Send'})
>>> resp.status_code
302
>>> #log in; step through all stages
>>> c.login(username="fred", password="secret")
True
>>> resp = c.get('/admin/crm/send_emails/', {'tag_id':'contains_d', 'id':comm_id, 'send':'Send'})
>>> resp.status_code
200

>>> 'Emails have been queued to send to 3 recipients.' in resp.content
True
>>> from mailer.models import Message, MessageLog   #should be in the queue
>>> Message.objects.count()
3
>>> from django.core.management import call_command
>>> call_command('send_mail')
...
>>> len(mail.outbox)
3
>>> mail.outbox = []
>>> len(mail.outbox)
0
>>> Message.objects.count()
0
>>> MessageLog.objects.count()
3


#now repeat with somebody unsubscribed.
>>> from mailer.models import DontSendEntry
>>> DontSendEntry.objects.all().count()
0
>>> from django.utils.timezone import now
>>> dont_send = DontSendEntry(to_address="damian@reportlab.com", when_added=now())
>>> dont_send.save()
>>> DontSendEntry.objects.all().count()
1
>>> resp = c.get('/admin/crm/send_emails/', {'tag_id':'contains_d', 'id':comm_id, 'send':'Send'})
>>> resp.status_code
200

>>> 'Emails have been queued to send to 3 recipients.' in resp.content
True
>>> from mailer.models import Message, MessageLog   #should be in the queue
>>> Message.objects.count()
3
>>> from django.core.management import call_command
>>> call_command('send_mail')
...
>>> len(mail.outbox)    # You need to have 'CRM_SEND_EMAILS = True' in your localsettings.py
2
>>> Message.objects.count()
0
>>> MessageLog.objects.count()
6
>>> MessageLog.objects.filter(result=1).count()
5
>>> MessageLog.objects.filter(result=2).count()
1


"""

