Exemplo n.º 1
0
def send_template_email(subject, template_name, data, recipient):

    mail = render_to_string('email/%s.txt'%template_name, data)
    if settings.SEND_EMAIL:
        send_mail(subject, mail, settings.DEFAULT_FROM_EMAIL, [recipient], fail_silently=False)
    else:
        print mail
Exemplo n.º 2
0
def desktop_view_list(request, username="", desktop_slug=""):

    user = request.user
    anon = request.user.is_anonymous()
    staff = "staff" in request.GET and user.is_staff

    rev_id = Desktop.get_revision_id(username, desktop_slug)
    desktop_cache_key = get_desktop_cache_key("getdesktop", username, desktop_slug, rev_id=rev_id)
    desktop = cache.get(desktop_cache_key, None)

    if desktop is None:
        try:
            desktop = Desktop.objects.get(owner__username=username, slug=desktop_slug)
        except Desktop.DoesNotExist:
            raise Http404

        cache.set(desktop_cache_key, desktop)

    desktop_owner_user = desktop.owner

    if not request.user.is_staff:
        if desktop.private:
            if anon or request.user != desktop.owner:
                raise Http404

    if request.user.is_anonymous():
        action = "viewlist_anon"
    elif request.user.username == username:
        action = "viewlist_own"
    else:
        action = "viewlist_another"

    if staff:
        action += "_staff"

    view_cache_key = get_desktop_cache_key(action, username, desktop_slug, rev_id=rev_id)

    # cached_html = cache.get(view_cache_key, None)
    # if cached_html is not None:
    #    return HttpResponse(cached_html)

    td = {}
    td["settings"] = settings
    td["desktop"] = desktop
    td["desktop_name"] = desktop.name
    td["theme"] = desktop.theme
    td["tagline"] = possessive(desktop.owner.get_full_name() or desktop.owner.username) + " desktop"

    desktop_def = json.loads(desktop.definition_json)

    icons = [ListIcon(icon_def) for icon_def in desktop_def["icons"] if icon_def["type"] == "icon"]

    stacks = dict(
        (icon_def["icon_id"], ListIcon(icon_def)) for icon_def in desktop_def["icons"] if icon_def["type"] == "stack"
    )

    top_icons = []
    for icon in icons:
        stack_name = icon.icon_def.get("stack")
        if stack_name is not None and stack_name in stacks:
            stacks[stack_name].add_child(icon)
        else:
            top_icons.append(icon)

    list_icons = top_icons
    list_icons += stacks.values()
    list_icons.sort(key=lambda i: (bool(i.children), i["name"]))
    for icon in list_icons:
        if icon.children:
            icon.children.sort(key=lambda i: i["name"])

    td["list_icons"] = list_icons

    exclude_private = False
    if not request.user.is_staff:
        if user.is_anonymous() or user.username != username:
            exclude_private = True
    desktops = Desktop.get_desktop_links(desktop_owner_user, exclude_private=exclude_private)

    desktop_url = Desktop.get_desktop_url(username, desktop_slug)

    allow_edit = staff or (user.is_authenticated() and user.username == username)
    own_desktop = user.username == username

    # pprint(icons)
    # print icons

    td.update(
        user=request.user,
        desktop_owner_user=desktop.owner,
        desktops=desktops,
        desktop_url=desktop_url,
        desktop_settings=dict(private=desktop.private),
        allow_edit=allow_edit,
        own_desktop=own_desktop,
        test_account=username == "test",
    )

    html = render_to_string("desktop/desktop_view_list.html", td, request)
    cache.set(view_cache_key, html)

    return HttpResponse(html)
Exemplo n.º 3
0
def desktop_view(request, username="", desktop_slug=""):
    """ Generates the desktop for viewing. """

    user = request.user
    anon = request.user.is_anonymous()
    staff = "staff" in request.GET and user.is_staff

    rev_id = Desktop.get_revision_id(username, desktop_slug)
    desktop_cache_key = get_desktop_cache_key("getdesktop", username, desktop_slug, rev_id=rev_id)
    desktop = cache.get(desktop_cache_key, None)

    if desktop is None:
        try:
            desktop = Desktop.objects.get(owner__username=username, slug=desktop_slug)
        except Desktop.DoesNotExist:
            raise Http404

        cache.set(desktop_cache_key, desktop)

    desktop_owner_user = desktop.owner

    if not request.user.is_staff:
        if desktop.private:
            if anon or request.user != desktop.owner:
                raise Http404

    if request.user.is_anonymous():
        action = "view_anon"
    elif request.user.username == username:
        action = "view_own"
    else:
        action = "view_another"

    if staff:
        action += "_staff"

    view_cache_key = get_desktop_cache_key(action, username, desktop_slug, rev_id=rev_id)

    cached_html = cache.get(view_cache_key, None)
    if cached_html is not None:
        return HttpResponse(cached_html)

    td = {}
    td["desktop"] = desktop
    td["username"] = username
    td["desktop_slug"] = desktop_slug

    td["desktop_name"] = desktop.name if desktop_slug != u"default" else possessive(username) + u" desktop"
    td["theme"] = desktop.theme

    td["definition_json"] = desktop.definition_json

    exclude_private = False
    if not request.user.is_staff:
        if user.is_anonymous() or user.username != username:
            exclude_private = True
    desktops = Desktop.get_desktop_links(desktop_owner_user, exclude_private=exclude_private)

    desktop_url = Desktop.get_desktop_url(username, desktop_slug)

    allow_edit = staff or (user.is_authenticated() and user.username == username)
    own_desktop = user.username == username

    td.update(
        user=request.user,
        desktop_owner_user=desktop.owner,
        desktops=desktops,
        desktop_url=desktop_url,
        desktop_settings=dict(private=desktop.private),
        allow_edit=allow_edit,
        own_desktop=own_desktop,
        test_account=username == "test",
    )

    html = render_to_string("desktop/desktop_view.html", td, request)
    cache.set(view_cache_key, html)

    return HttpResponse(html)