Ejemplo n.º 1
0
    def _create_user(self, form):
        if request.files.get('avatar'):
            avatar = resize_and_upload(request.files['avatar'], 'AVATAR')
        elif form.avatar_url.data:
            avatar_file = StringIO(requests.get(form.avatar_url.data).content)
            avatar = resize_and_upload(avatar_file, 'AVATAR')
        else:
            avatar = ''

        user = User(
            username=form.username.data.strip(),
            password_hash='',
            first_name=form.first_name.data,
            last_name=form.last_name.data,
            description=form.description.data,
            site_url=form.site_url.data,
            email='',
            date_of_birth=date(1900, 1, 1),
            avatar=avatar,
            refresh_token='',
            locale=form.locale.data or 'en-us',
            is_active=True)
        self.record_action('created', user)
        db.session.add(user)
        db.session.commit()
        return user.id
Ejemplo n.º 2
0
 def _process_image_data(self, form, model=None):
     for field in form:
         if isinstance(field, wtf.FileField):
             data = None
             cfgkey = self.model.__table__.columns.get(field.name).type.cfgkey
             aoi = form.data.get(field.name + '_aoi')
             if aoi:
                 aoi = get_box_value(aoi)
             if field.data:
                 # New image upload
                 data = field.data
             elif aoi and model and not getattr(model, field.name + '_aoi') == aoi:
                 # Same image, aoi updated - need to fetch the image data
                 data = getattr(model, field.name).original
             else:
                 # Allow form to be edited without replacing existing image
                 form._fields.pop(field.name)
             if data:
                 try:
                     if not isinstance(data, FileStorage):
                         data = get_external_resource(data)
                     field.data = resize_and_upload(data, cfgkey, aoi)
                 except IOError, e:
                     # The form has already been validated at this stage but
                     # if we return False then BaseModelView.create_view will
                     # still drop through and render the errors
                     field.errors = [getattr(e, 'message', str(e))]
                     return False
Ejemplo n.º 3
0
    def coverart(self):
        if not User.query.get(request.form.get('owner')):
            return jsonify({'error': 'invalid owner'}), 400

        c = UserCoverArt(cover=resize_and_upload(request.files['cover'], 'CHANNEL'),
                         owner=request.form.get('owner')).save()
        return jsonify({'id': str(c.cover)})
Ejemplo n.º 4
0
def update_image_thumbnails(fieldname):
    """Re-process all images for the specified Model.field."""
    model, fieldname, cfgkey = _parse_fieldname(fieldname)
    for instance in model.query.filter(getattr(model, fieldname) != ''):
        try:
            data = get_external_resource(getattr(instance, fieldname).original)
        except Exception, e:
            msg = e.response.reason if hasattr(e, 'response') else e.message
            logging.error('Unable to process %s: %s', getattr(instance, fieldname).path, msg)
            continue
        aoi = getattr(instance, '%s_aoi' % fieldname, None)
        image_path = resize_and_upload(data, cfgkey, aoi)
        setattr(instance, fieldname, image_path)
        instance.save()
Ejemplo n.º 5
0
def process_image(field, data=None):
    if not data:
        if request.mimetype.startswith('image/'):
            # PIL needs to seek on the data and request.stream doesn't have that
            data = StringIO(request.data)
        elif request.mimetype.startswith('multipart/form-data'):
            data = request.files['image']
        else:
            abort(400, message=_('No image data'))

    cfgkey = field.class_.__table__.columns.get(field.key).type.cfgkey

    try:
        return resize_and_upload(data, cfgkey)
    except IOError, e:
        abort(400, message=e.message or str(e))
Ejemplo n.º 6
0
    def validate(self):
        if not super(ImportForm, self).validate():
            return

        if request.files.get('cover'):
            cover = request.files['cover']
        elif self.cover_url.data:
            cover = StringIO(requests.get(self.cover_url.data).content)
        else:
            cover = None

        if self.user.data:
            try:
                self.cover.data = resize_and_upload(cover, 'CHANNEL') if cover else ''
            except IOError, e:
                self.cover.errors = [str(e)]
                return
            UserCoverArt(cover=self.cover.data, owner=self.user.data).save()
Ejemplo n.º 7
0
    def create_from_external_system(cls, eu, locale):
        avatar = eu.avatar
        if avatar:
            avatar = resize_and_upload(avatar, 'AVATAR')

        new_username = cls.suggested_username(
            cls.sanitise_username(eu.username or eu.display_name))

        return cls.create_with_channel(
            username=new_username,
            first_name=eu.first_name,
            last_name=eu.last_name,
            email=eu.email,
            gender=eu.gender,
            avatar=avatar,
            date_of_birth=eu.dob,
            description=eu.description,
            locale=locale,
        )
Ejemplo n.º 8
0
def upload_default_image(fieldname, filename, name=None):
    model, fieldname, cfgkey = _parse_fieldname(fieldname)
    with open(filename) as img:
        resize_and_upload(img, cfgkey, name=name)