PiRSquared17 / app-engine-patch

Automatically exported from code.google.com/p/app-engine-patch
0 stars 0 forks source link

cant render image #237

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using appengine patch with django 1.1 (came with appengine patch).
I have a page with multiple columns and multiple files.
I want to be able to upload an image and write text in a column like so:

---
This is the sory of bugs bunny ...
<img src="/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/" alt="didnt
work" />
some more text about bugs bunny ...

---
(where get_absolute_url shows me
/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/)

My trouble is getting the image to be rendered on the page, the above shows
'didnt work' instead of rendering the image.
I can go to the detail pages of page, column and file though.
Have I not done the models correctly, does the app.yaml need something,
should i not be passing a template for the file's generic url?

My urls.py:
-----------

urlpatterns += patterns('',
    url(r'^index/$', direct_to_template, 
        {'template': 'base.html'}, name="main-view"),

    url(r'^pages/page/(?P<object_id>.+)/$', object_detail, 
        {'queryset': Page.all(), 
         'template_name': 'pages_page_detail.html'},
name="pages_page_detail_view"),

    url(r'^pages/column/(?P<object_id>.+)/$', object_detail, 
        {'queryset': Column.all(), 
         'template_name': 'pages_column_detail.html'},
name="pages_column_detail_view"),    

    url(r'^pages/file/(?P<object_id>.+)/$', object_detail, 
        {'queryset': File.all(), 
         'template_name': 'pages_file_detail.html'},
name="pages_file_detail_view"),    

    url(r'^pages/$', object_list, 
        {'queryset': Page.all(), 'paginate_by': 3, 'template_name':
'pages_page_index.html'}, name="page_index_view"), 
)

My models.py:
-------------

class Page(db.Model):
    """Page model."""

    title           = db.StringProperty(_('title'), required=True)
    author          = db.ReferenceProperty(User)
    status          = db.StringProperty(_('status'), choices=[_('draft'), 
                                                              _('public'),
                                                              _('hidden'),
                                                     ])                   

    created         = db.DateTimeProperty(_('created'))
    modified        = db.DateTimeProperty(_('modified'))
    non_indexable   = db.BooleanProperty(_('hide from index'))

    def __unicode__(self):
        return '%s' % self.title

    @permalink    
    def get_absolute_url(self):
        return ('pages_page_detail_view', [self.key()])

class File(db.Model):
    """File model."""
    title           = db.StringProperty(_('title'))
    data            = db.BlobProperty()
    added           = db.DateTimeProperty(_('added'))
    page            = db.ReferenceProperty(Page)
    #content_type    = FakeModelProperty(ContentType, required=True)

    def __unicode__(self):
        return '%s' % self.get_absolute_url()

    @permalink
    def get_absolute_url(self):
        return ('pages_file_detail_view', [self.key()])

class Column(db.Model):
    """Column model."""
    title           = db.StringProperty(_('title'))
    data            = db.TextProperty(_('data'))
    page            = db.ReferenceProperty(Page)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('pages_column_detail_view', [self.key()])

my app.yaml:
------------

application: aep-sample
version: 1
runtime: python
api_version: 1

default_expiration: '3650d'

handlers:
- url: /remote_api
  script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
  secure: optional
  login: admin

- url: /media
  static_dir: _generated_media
  secure: optional

- url: /.*
  script: common/appenginepatch/main.py
  secure: optional

Original issue reported on code.google.com by dstrijdh...@gmail.com on 3 Nov 2009 at 1:17

GoogleCodeExporter commented 9 years ago
Hopefully I haven't got the wrong end of the stick but ...

Calling the following:

<img src="/pages/file/agphZXAtc2FtcGxlchALEgpwYWdlc19maWxlGAUM/" alt="didnt
work" />

will use this 

    url(r'^pages/file/(?P<object_id>.+)/$', object_detail, 
        {'queryset': File.all(), 
         'template_name': 'pages_file_detail.html'},
name="pages_file_detail_view"),    

which renders the response with the pages_file_detail template, which appears 
to be an html file.

File upload/download is a little more involved in AppEngine as you have to save 
the file into a db.BlobProperty - and 
then provide a view specifically to download the file from the blob (setting 
the content-type etc at the same time).

For the upload you can use a form with a forms.ImageField in it (say called 
file_data) and save the uploaded file (from 
request.FILES) into a model containing a BlobProperty (and maybe the 
form.cleaned_data['file_data'].content_type as 
well).

Something like:

if request.method == 'POST':
    form = MyImageUploadForm( request.POST, request.FILES)
    if form.is_valid():
        themodel = MyImageModel()
        themodel.data = form.cleaned_data['file'].read()
        themodel.content_type = form.cleaned_data['file'].content_type
        themodel.put()
else:
    form = MyImageUploadForm

where MyImageModel looks something like the following (you could even add your 
text description to this model).

class MyImageModel(db.Model):
    data = db.BlobProperty()
    content_type = db.StringProperty()

For the download you need to fetch the model by its key, and write the 
blobproperty to the response after setting the 
content-type, but only for requests for the actual image (not the image and the 
text). Maybe you could map your 
download to something like ..

pages/download/file/(?P<object_id>.+)/$

Hope this helps a little bit.

Original comment by rossdjo...@gmail.com on 3 Nov 2009 at 4:30

GoogleCodeExporter commented 9 years ago
Yes, this completely solves the problem!
I'm uploading via the admin now so I only needed the following changes:

urls.py:
---------

    url(r'^pages/file/(?P<object_id>.+)/$', 'pages.views.download_file', 
        name="pages_file_download_view"),

views.py:
---------

    def download_file(request, object_id):
        file = get_object_or_404(File, object_id)
        return HttpResponse(file.data, 
            content_type=guess_type(file.title)[0] or 'application/octet-stream')

and in models.py:
-----------------

    @permalink
    def get_absolute_url(self):
        return ('pages_file_download_view', [self.key()])

Original comment by dstrijdh...@gmail.com on 3 Nov 2009 at 11:49