##
#    Copyright (C) 2013 Jessica Tallon & Matt Molyneaux
#   
#    This file is part of Inboxen.
#
#    Inboxen is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Inboxen is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with Inboxen.  If not, see <http://www.gnu.org/licenses/>.
##

from django.utils.translation import ugettext as _
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.db.models import F

from inboxen.models import Email, Header

@login_required
def view(request, email_address, emailid):
    try:
        inbox = request.user.inbox_set.from_string(email=email_address)

        email = int(emailid, 16)
        email = Email.objects.get(id=email, flags=~Email.flags.deleted)
    except (Email.DoesNotExist, Inbox.DoesNotExist):
        return Http404

    headers = Header.objects.filter(part__email=email, part__parent=None)
    headers = headers.get_many("Subject", "From")

    email_obj = None #TODO

    email.update(flags=F('flags').bitand(Email.flags.read))

    context = {
        "page":email_obj.subject,
        "email":email_obj,
        "plain_message":plain_message,
        "user":request.user,
    }
 
    return render(request, "inbox/email.html", context)

@@ -1265,16 +1265,80 @@
 eleted)%0A
+        email.update(flags=F('flags').bitand(Email.flags.read))%0A
     exce
@@ -1551,79 +1551,1424 @@
 j = 
-None #TODO%0A%0A    email.update(flags=F('flags').bitand(Email.flags.read))
+%7B%7D%0A    email_obj%5B%22subject%22%5D = headers%5B%22Subject%22%5D%0A    email_obj%5B%22from%22%5D = headers%5B%22From%22%5D%0A    email_obj%5B%22date%22%5D = email.received_date%0A%0A    attachments = %5B%5D%0A    for part in email.parts:%0A        item = part.header_set.get_many(%22Content-Type%22, %22Content-Disposition%22)%0A        item%5B%22id%22%5D = part.id%0A        item%5B%22part%22%5D%0A        item%5B%22parent%22%5D = part.parent.id%0A        attachments.append(item)%0A%0A    # find first text/html and first text/plain%0A    html = False%0A    plain = False%0A    for part in attachments:%0A        if not html and part%5B%22Content-Type%22%5D == %22text/html%22:%0A            html = part%5B%22part%22%5D%0A        elif not plain and part%5B%22Content-Type%22%5D == %22text/plain%22:%0A            plain = part%5B%22part%22%5D%0A        del part%5B%22part%22%5D%0A%0A    # if siblings, use html_preference%0A    if html%5B%22parent%22%5D == plain%5B%22parent%22%5D:%0A        pref = request.user.userprofile.html_preference%0A        if pref == 1:%0A            plain_message = True%0A            email%5B%22body%22%5D = plain.body.data%0A        elif pref == 2:%0A            plain_message = False%0A            email%5B%22body%22%5D = html.body.data%0A%0A    # if not, which ever has the lower lft value%0A    elif not html and attachments.index(html) %3C attachments.index(plain):%0A        plain_message = False%0A        email%5B%22body%22%5D = html.body.data%0A    elif not plain and attachments.index(html) %3E attachments.index(plain):%0A        plain_message = True%0A        email%5B%22body%22%5D = plain.body.data%0A%0A    #TODO clean HTML body
 %0A%0A  
@@ -3009,16 +3009,19 @@
 _obj
-.
+%5B%22
 subject
+%22%5D
 ,%0A  

Use new models to grab email body