librelist archives

« back to archive

Flask upload image

Flask upload image

From:
d4n1
Date:
2012-06-26 @ 11:30
I am developing a website that has a gallery of images. I have some fields
and a field to store the image. I'm trying to use the flask and the
flask-admin-peewee, but without success.
I use an image with no field peewee following example but not in
combination with other fields. The flask-admin has the option of managing
uploads without any integration with other fields.

Help me, please.

Thanks.

Re: [flask] Flask upload image

From:
Serge S. Koval
Date:
2012-06-26 @ 11:35
Um,

 Are you using Flask-Admin or Flask-Peewee?

Serge.

On Tue, Jun 26, 2012 at 2:30 PM, d4n1 <d4n1h4ck@gmail.com> wrote:

> I am developing a website that has a gallery of images. I have some fields
> and a field to store the image. I'm trying to use the flask and the
> flask-admin-peewee, but without success.
> I use an image with no field peewee following example but not in
> combination with other fields. The flask-admin has the option of managing
> uploads without any integration with other fields.
>
> Help me, please.
>
> Thanks.
>

Re: [flask] Flask upload image

From:
pronoyc@gmail.com
Date:
2012-06-26 @ 11:42
>
> I am developing a website that has a gallery of images. I have some fields
>> and a field to store the image. I'm trying to use the flask and the
>> flask-admin-peewee, but without success.
>> I use an image with no field peewee following example but not in
>> combination with other fields. The flask-admin has the option of managing
>> uploads without any integration with other fields.
>>
>
Keep it simple and straight :)

Use HTML5 uploader and store all the images in a folder and their path in
the db.
Here's an 
example<https://github.com/DarkSector/profilebuilder/blob/master/profilebuilder/views.py#L557>

In the template use the simple HTML 5 forms
Here's the form

code<https://github.com/DarkSector/profilebuilder/blob/master/profilebuilder/templates/upload_image_test.html#L41>


We should have a snippet for this or a tutorial in the flask docs, people
(including me) ask a lot of questions about serving static content.
-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc

Re: [flask] Flask upload image

From:
d4n1
Date:
2012-06-26 @ 12:01
I'm trying flask-peewee and flask-admin too.

I'm going to try html5 pronoy too.

But I need of the admin, and flask-admin or flask-peewee are good solutions.

Thanks. I'm going to try this.
On Jun 26, 2012 8:45 AM, "pronoyc@gmail.com" <pronoyc@gmail.com> wrote:

> I am developing a website that has a gallery of images. I have some fields
>>> and a field to store the image. I'm trying to use the flask and the
>>> flask-admin-peewee, but without success.
>>> I use an image with no field peewee following example but not in
>>> combination with other fields. The flask-admin has the option of managing
>>> uploads without any integration with other fields.
>>>
>>
> Keep it simple and straight :)
>
> Use HTML5 uploader and store all the images in a folder and their path in
> the db.
> Here's an 
example<https://github.com/DarkSector/profilebuilder/blob/master/profilebuilder/views.py#L557>
>
> In the template use the simple HTML 5 forms
> Here's the form 
code<https://github.com/DarkSector/profilebuilder/blob/master/profilebuilder/templates/upload_image_test.html#L41>
>
>
> We should have a snippet for this or a tutorial in the flask docs, people
> (including me) ask a lot of questions about serving static content.
> --
> Regards,
>
> Pronoy Chopra
> http://blog.pronoy.in <http://www.pronoy.in/about>/
> http://www.twitter.com/pronoyc
>
>

Re: [flask] Flask upload image

From:
Serge S. Koval
Date:
2012-06-26 @ 12:10
In flask-admin, you can override create_model/update_model logic to save
form field as a file and store path to the file in the model.

Serge.

On Tue, Jun 26, 2012 at 3:01 PM, d4n1 <d4n1h4ck@gmail.com> wrote:

> I'm trying flask-peewee and flask-admin too.
>
> I'm going to try html5 pronoy too.
>
> But I need of the admin, and flask-admin or flask-peewee are good
> solutions.
>
> Thanks. I'm going to try this.
>

Re: [flask] Flask upload image

From:
d4n1
Date:
2012-06-26 @ 14:36
In my models.py I has:
path = op.join(op.dirname(__file__), 'uploads')


class Album(db.Model):
    """ Class Album of photos """
    id = db.Column(db.Integer, primary_key=True)
    album = db.Column(db.String(50), unique=True, nullable=False)
    cover = db.Column(db.String(50), unique=True, nullable=False)
    belongs = db.Column(db.String(50), nullable=False)

    def __unicode__(self):
        return self.album

    # Model handlers
    def create_model(self, form):
        """
            Create model from form.

            `form`
                Form instance
        """
        try:
            model = self.model()
            form.populate_obj(model)
            self.session.add(model)
            self.session.commit()
            return True
        except Exception, ex:
            flash(gettext('Failed to create model. %(error)s',
error=str(ex)), 'error')
            return False

    def update_model(self, form, model):
        """
            Update model from form.

            `form`
                Form instance
        """
        try:
            form.populate_obj(model)
            self.session.commit()
            return True
        except Exception, ex:
            flash(gettext('Failed to update model. %(error)s',
error=str(ex)), 'error')
            return False


class Photo(db.Model):
    """ Photos belongs Album """
    id = db.Column(db.Integer, primary_key=True)
    photo = db.Column(db.String(50), nullable=False)

    album_id = db.Column(db.Integer(), db.ForeignKey(Album.id),
nullable=False)
    album = db.relationship(Album, backref='photo')

    def __unicode__(self):
        return self.photo


admin.py:
class AlbumAdmin(sqlamodel.ModelView):
    list_columns = ('album', 'cover', 'belongs')

    sortable_columns = ('album', 'belongs')

    column_filters = ('album', 'cover', 'belongs')

    def __init__(self, session):
        super(AlbumAdmin, self).__init__(Album, session)


class PhotoAdmin(sqlamodel.ModelView):
    list_columns = ('photo', 'album')

    sortable_columns = ('photo', 'album')

    column_filters = ('photo', 'album')

    def __init__(self, session):
        super(PhotoAdmin, self).__init__(Photo, session)


How to overwrite create/update and show upload field in my templates. I
copy the directory admin of the flask_admin.

Help me. And sorry, I'm new in Flask.

2012/6/26 Serge S. Koval <serge.koval@gmail.com>

> In flask-admin, you can override create_model/update_model logic to save
> form field as a file and store path to the file in the model.
>
> Serge.
>
>
> On Tue, Jun 26, 2012 at 3:01 PM, d4n1 <d4n1h4ck@gmail.com> wrote:
>
>> I'm trying flask-peewee and flask-admin too.
>>
>> I'm going to try html5 pronoy too.
>>
>> But I need of the admin, and flask-admin or flask-peewee are good
>> solutions.
>>
>> Thanks. I'm going to try this.
>>
>


-- 
#!/usr/bin/python
class Myself(Developer):
    @myself
    def about:
        '''
        Developer Python,
        SysAdmin GNU/Linux,
        Archlinux and Gnome user and developer,
        Degree in Systems Analysis and Development,
        Graduate Student in Software Engineering,
        Technician Analysis and System Design,
        Technical Project and Network Administration,
        Linux Certification (LPI-1),
        Designer, Bassist, ...
        '''
        return ['Daniel Melo<
http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4499888T5>
', 'blog <http://d4n1h4ck.blogspot.com.br/>', 'github<
https://github.com/d4n1>
']

Re: [flask] Flask upload image

From:
Serge S. Koval
Date:
2012-06-26 @ 18:38
In code below, you just declared some models and created admin views.

1. You need to have custom form for the admin view. See here:

http://flask-admin.readthedocs.org/en/latest/mod_model/#flask.ext.admin.model.BaseModelView.form

2. You need to add create_model/update_model in the admin view, not in the
model. In there, just access your form FileField and save it to the file.

That's it.

Serge.

On Tue, Jun 26, 2012 at 5:36 PM, d4n1 <d4n1h4ck@gmail.com> wrote:

> In my models.py I has:
> path = op.join(op.dirname(__file__), 'uploads')
>
...