changeset 132:09c808e39a98

Re-added support for hosted static personas in the backend, although they're not very humanely supported by the UI at present.
author Atul Varma <varmaa@toolness.com>
date Thu, 03 Apr 2008 12:30:39 -0700
parents 596ea5315b3a
children 8993609e7004
files personasbackend/personas/models.py personasbackend/personas/urls.py personasbackend/personas/views.py
diffstat 3 files changed, 92 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/personasbackend/personas/models.py	Wed Apr 02 17:14:54 2008 -0700
+++ b/personasbackend/personas/models.py	Thu Apr 03 12:30:39 2008 -0700
@@ -215,14 +215,22 @@
         )
 
     url = models.URLField(
-        "Persona URL",
+        "Dynamic Persona URL",
         help_text=("URL for the content that will be placed behind "
-                   "the browser's chrome."),
+                   "the browser's chrome. This is an advanced option, "
+                   "and only needs to be specified if a header and "
+                   "footer image are not uploaded."),
         verify_exists=False,
-        blank=False,
+        blank=True,
         )
 
     def __get_resolved_url(self):
+        if not self.url:
+            return make_absolute_url( reverse(
+                    'hosted-static-persona',
+                    kwargs = {'persona_id' : str(self.id)}
+                    ) )
+
         viewinfo = parse_personas_view_url( self.url )
         if not viewinfo:
             return self.url
@@ -247,6 +255,38 @@
 
     thumbnail_url = property( __get_thumbnail_url )
 
+    MAX_FILE_NAME_LENGTH = 255
+
+    header_img = models.ImageField(
+        "Header image file",
+        help_text=("File for the image that will be placed behind "
+                   "the browser's top chrome.  Only needed if "
+                   "a URL is not specified."),
+        upload_to="hosted-content/headers",
+        blank=True,
+        max_length=MAX_FILE_NAME_LENGTH,
+        )
+
+    def get_absolute_header_img_url( self ):
+        return make_absolute_url(
+            self.get_header_img_url()
+            )
+
+    footer_img = models.ImageField(
+        "Footer image file",
+        help_text=("File for the image that will be placed behind "
+                   "the browser's bottom chrome.  Only needed if "
+                   "a URL is not specified."),
+        upload_to="hosted-content/footers",
+        blank=True,
+        max_length=MAX_FILE_NAME_LENGTH,
+        )
+
+    def get_absolute_footer_img_url( self ):
+        return make_absolute_url(
+            self.get_footer_img_url()
+            )
+
     text_color = models.CharField(
         help_text=("The RGB color, as a #RRGGBB color hash, "
                    "of the color of text that will be displayed "
@@ -326,6 +366,8 @@
         "category",
         "description",
         "url",
+        "header_img",
+        "footer_img",
         "text_color",
         "accent_color",
         "status",
@@ -534,6 +576,16 @@
         null=True,
         )
 
+    header_img = models.CharField(
+        max_length=Persona.MAX_FILE_NAME_LENGTH,
+        null=True
+        )
+
+    footer_img = models.CharField(
+        max_length=Persona.MAX_FILE_NAME_LENGTH,
+        null=True
+        )
+
     text_color = models.CharField(
         max_length=MAX_COLOR_LENGTH,
         null=True,
--- a/personasbackend/personas/urls.py	Wed Apr 02 17:14:54 2008 -0700
+++ b/personasbackend/personas/urls.py	Thu Apr 03 12:30:39 2008 -0700
@@ -16,6 +16,9 @@
     url(r'^legacy/(?P<name>.+)$',
         'personasbackend.personas.views.legacy_cbeard_persona',
         name='legacy-cbeard-persona'),
+    url(r'^static/(?P<persona_id>\d+)$',
+        'personasbackend.personas.views.hosted_static_persona',
+        name='hosted-static-persona'),
 
     # Auth
     url(r'^login/$', 'django.contrib.auth.views.login',
--- a/personasbackend/personas/views.py	Wed Apr 02 17:14:54 2008 -0700
+++ b/personasbackend/personas/views.py	Thu Apr 03 12:30:39 2008 -0700
@@ -1,4 +1,5 @@
 from django.http import HttpResponseRedirect, HttpResponseForbidden
+from django.http import Http404
 from django.http import HttpResponse, HttpResponseBadRequest
 from django.template import RequestContext
 from django.contrib.auth.decorators import login_required
@@ -8,26 +9,51 @@
 from personasbackend.personas import models
 from personasbackend.personas import forms
 
+def _render_static_persona( img_src, action ):
+    if action == "footer":
+        position = "bottom left"
+    else:
+        position = "top right"
+
+    return render_to_response(
+        # TODO: Rename the file to static_persona.xul.
+        "personas/legacy_persona.xul",
+        { "image" : img_src, "position" : position },
+        mimetype = "application/vnd.mozilla.xul+xml"
+        )
+
+def hosted_static_persona( request, persona_id ):
+    persona = get_object_or_404( models.Persona, id=persona_id )
+
+    if not (persona.header_img and persona.footer_img):
+        raise Http404( "Persona is not statically hosted." )
+
+    action = request.GET.get( "action", "" )
+
+    if action == "footer":
+        img_src = persona.get_absolute_footer_img_url()
+    elif action == "header":
+        img_src = persona.get_absolute_header_img_url()
+    else:
+        return HttpResponseBadRequest( "Invalid or unsupported action: %s" %
+                                       action )
+
+    return _render_static_persona( img_src, action )
+
 def legacy_cbeard_persona( request, name ):
     urldict = {"name" : name}
 
     action = request.GET.get( "action", "" )
 
     if action == "footer":
-        position = "bottom left"
         img_src = models.LEGACY_FOOTER_URL % urldict
     elif action == "header":
-        position = "top right"
         img_src = models.LEGACY_HEADER_URL % urldict
     else:
         return HttpResponseBadRequest( "Invalid or unsupported action: %s" %
                                        action )
 
-    return render_to_response(
-        "personas/legacy_persona.xul",
-        { "image" : img_src, "position" : position },
-        mimetype = "application/vnd.mozilla.xul+xml"
-        )
+    return _render_static_persona( img_src, action )
 
 def list_view( request ):
     personas = models.Persona.objects.filter( status="published" )
@@ -70,7 +96,7 @@
         pageTitle = "Edit Persona"
 
     if request.method == "POST":
-        form = forms.PersonaForm( request.POST,
+        form = forms.PersonaForm( request.POST, request.FILES,
                                   instance=persona )
         if form.is_valid():
             newPersona = form.save( commit=False )