from datetime import datetime

from cornerwise.utils import make_absolute_url
from django.template.loader import render_to_string

from . import changes


def _make_user_context(user, context):
    context = context.copy() if context else {}
    context["user"] = user.profile.addressal
    context["unsubscribe_url"] = make_absolute_url(user.profile.unsubscribe_url)
    return context


def updates_context(user, updates):
    """Constructs the context for the subscription updates email.
    """
    updates_html = render_to_string("changes.djhtml",
                                    {"changes": updates["changes"]})

    datefmt = lambda dt: datetime.fromtimestamp(dt).strftime("%A, %B %-d") if dt else ""
    fmt = "from {start} to {end}" if updates["end"] else "since {start}"
    date_range = fmt.format(
        start=datefmt(updates["start"]), end=datefmt(updates["end"]))

    return _make_user_context(user, {
        "updates": updates_html,
        "update_summary": changes.summary_line(updates),
        "date_range": date_range
    })


def welcome_context(subscription):
    """Constructs the context for rendering the welcome email.
    """
    user = subscription.user
    profile = user.profile
    if not profile.token:
        profile.generate_token()

    return {
        "confirm_url": make_absolute_url(profile.confirm_url),
        "minimap_src": subscription.minimap_src,
        "subscription-preview": subscription.minimap_src
    }


def confirmation_context(subscription):
    try:
        user = subscription.user
        existing = user.subscriptions.filter(active=True).exists()
    except IndexError:
        # The user doesn't have any active Subscriptions
        return
    return {
        "subscription": subscription.readable_description,
        "minimap_src": subscription.minimap_src,
        "subscription-preview": subscription.minimap_src,
        "old_minimap_src": existing.minimap_src,
        "confirmation_link": make_absolute_url(subscription.confirm_url)
    }

@@ -145,17 +145,25 @@
  changes
+, models
 %0A
-
 %0A%0Adef _m
@@ -243,16 +243,30 @@
 else %7B%7D%0A
+%0A    try:%0A    
     cont
@@ -302,16 +302,20 @@
 dressal%0A
+    
     cont
@@ -383,24 +383,164 @@
 scribe_url)%0A
+    except models.UserProfile.DoesNotExist:%0A        context%5B%22user%22%5D = %22Unknown%22%0A        context%5B%22unsubscribe_url%22%5D = make_absolute_url(%22/%22)%0A
     return c

Create email context for anonymous or admin users