def advertiser_messages_welcome_email_edit(request):
    ''' View to edit this Advertiser's Welcome Email '''
    from forms import WelcomeEmailForm
    from atrinsic.base.models import PrivateMessage
    from django.db.models import Q
    # Folder totals for sidebar
    inbox = PrivateMessage.objects.filter(receiver=request.organization.id, receiver_status=1).order_by('-date_sent')
    sent = PrivateMessage.objects.filter(sender=request.organization.id, sender_status=1).order_by('-date_sent')
    trash =  PrivateMessage.objects.filter(Q(sender=request.organization.id, sender_status=3) | Q(receiver=request.organization.id, receiver_status=3)).order_by('-date_sent')

    if request.method == 'POST':
        form = WelcomeEmailForm(request.POST)

        if form.is_valid():
            request.organization.publisher_welcome_mail_subject = form.cleaned_data['subject']
            request.organization.publisher_welcome_mail = form.cleaned_data['body']
            request.organization.publisher_welcome_mail_html = form.cleaned_data['html_body']
            request.organization.save()

            return HttpResponseRedirect('/advertiser/messages/welcome_email/')
    else:
        form = WelcomeEmailForm(initial={'body' : request.organization.publisher_welcome_mail,
                                         'html_body' : request.organization.publisher_welcome_mail_html, 
                                         'subject' : request.organization.publisher_welcome_mail_subject, })

    return AQ_render_to_response(request, 'advertiser/messages/welcome-email-edit.html', {
                'inbox' : inbox,
                'sent' : sent,
                'trash' : trash,
                'form' : form,
            }, context_instance=RequestContext(request))
 def post(self, request, pk, *args, **kwargs):
     obj = get_object_or_404(WelcomeEmail, id=pk)
     form = WelcomeEmailForm(request.POST, instance=obj)
     if form.is_valid():
         obj = form.save(commit=False)
         if obj.enabled:
             for email in WelcomeEmail.objects.filter(company=request.user.profile.company, category=obj.category):
                 email.enabled = False
                 email.save()
         obj.save()
         return HttpResponseRedirect(reverse('welcome_email'))
     else:
         return render(request, self.template_name, {'form': form})
 def post(self, request, *args, **kwargs):
     form = WelcomeEmailForm(request.POST)
     if form.is_valid():
         message = form.save(commit=False)
         message.company = request.user.profile.company
         if message.enabled:
             for email in WelcomeEmail.objects.filter(company=request.user.profile.company,
                                                      category=message.category):
                 email.enabled = False
                 email.save()
         message.save()
         return HttpResponseRedirect(reverse('welcome_email'))
     else:
         return render(request, self.template_name, {'form': form})
def advertiser_messages_welcome_email_edit(request):
    ''' View to edit this Advertiser's Welcome Email '''
    from forms import WelcomeEmailForm
    from atrinsic.base.models import PrivateMessage
    from django.db.models import Q
    # Folder totals for sidebar
    inbox = PrivateMessage.objects.filter(
        receiver=request.organization.id,
        receiver_status=1).order_by('-date_sent')
    sent = PrivateMessage.objects.filter(
        sender=request.organization.id, sender_status=1).order_by('-date_sent')
    trash = PrivateMessage.objects.filter(
        Q(sender=request.organization.id, sender_status=3)
        | Q(receiver=request.organization.id, receiver_status=3)).order_by(
            '-date_sent')

    if request.method == 'POST':
        form = WelcomeEmailForm(request.POST)

        if form.is_valid():
            request.organization.publisher_welcome_mail_subject = form.cleaned_data[
                'subject']
            request.organization.publisher_welcome_mail = form.cleaned_data[
                'body']
            request.organization.publisher_welcome_mail_html = form.cleaned_data[
                'html_body']
            request.organization.save()

            return HttpResponseRedirect('/advertiser/messages/welcome_email/')
    else:
        form = WelcomeEmailForm(
            initial={
                'body': request.organization.publisher_welcome_mail,
                'html_body': request.organization.publisher_welcome_mail_html,
                'subject': request.organization.publisher_welcome_mail_subject,
            })

    return AQ_render_to_response(request,
                                 'advertiser/messages/welcome-email-edit.html',
                                 {
                                     'inbox': inbox,
                                     'sent': sent,
                                     'trash': trash,
                                     'form': form,
                                 },
                                 context_instance=RequestContext(request))
def advertiser_messages_welcome_email(request):
    ''' View this Advertisers Welcome Email.  If the Welcome Email has not been set yet, go 
        directly to the Editing page '''
    from forms import WelcomeEmailForm
    from atrinsic.util.mail import *
    from atrinsic.base.models import PrivateMessage
    from django.db.models import Q

    if request.organization.publisher_welcome_mail is None or request.organization.publisher_welcome_mail_html is None or len(
            request.organization.publisher_welcome_mail) < 1 or len(
                request.organization.publisher_welcome_mail_html) < 1:
        return HttpResponseRedirect('/advertiser/messages/welcome_email/edit/')

    # Folder totals for sidebar
    inbox = PrivateMessage.objects.filter(
        receiver=request.organization.id,
        receiver_status=1).order_by('-date_sent')
    sent = PrivateMessage.objects.filter(
        sender=request.organization.id, sender_status=1).order_by('-date_sent')
    trash = PrivateMessage.objects.filter(
        Q(sender=request.organization.id, sender_status=3)
        | Q(receiver=request.organization.id, receiver_status=3)).order_by(
            '-date_sent')

    form = WelcomeEmailForm(
        initial={
            'body':
            render_text(request.organization.publisher_welcome_mail,
                        request.organization, None),
            'html_body':
            render_html(request.organization.publisher_welcome_mail_html,
                        request.organization, None),
            'subject':
            request.organization.publisher_welcome_mail_subject,
        })

    return AQ_render_to_response(
        request,
        'advertiser/messages/welcome-email.html', {
            'inbox': inbox,
            'sent': sent,
            'trash': trash,
            'body': request.organization.publisher_welcome_mail,
            'html_body': request.organization.publisher_welcome_mail_html,
            'subject': request.organization.publisher_welcome_mail_subject,
        },
        context_instance=RequestContext(request))