Example #1
0
    def test_get_avatar_url(self, mock_url_for):
        """Test get_avatar_url works."""
        util.get_avatar_url('rackspace', '1.png', '1')
        mock_url_for.assert_called_with('rackspace', container='1', filename='1.png')

        util.get_avatar_url('local', '1.png', '1')
        mock_url_for.assert_called_with('uploads.uploaded_file', filename='1/1.png')
Example #2
0
    def test_get_avatar_url(self, mock_url_for):
        """Test get_avatar_url works."""
        util.get_avatar_url('rackspace', '1.png', '1')
        mock_url_for.assert_called_with('rackspace',
                                        container='1',
                                        filename='1.png')

        util.get_avatar_url('local', '1.png', '1')
        mock_url_for.assert_called_with('uploads.uploaded_file',
                                        filename='1/1.png')
Example #3
0
    def _file_upload(self, data):
        """Method that must be overriden by the class to allow file uploads for
        only a few classes."""
        cls_name = self.__class__.__name__.lower()
        content_type = 'multipart/form-data'
        if (content_type in request.headers.get('Content-Type')
                and cls_name in self.allowed_classes_upload):
            tmp = dict()
            for key in request.form.keys():
                tmp[key] = request.form[key]

            ensure_authorized_to('create',
                                 self.__class__,
                                 project_id=tmp['project_id'])
            upload_method = current_app.config.get('UPLOAD_METHOD')
            if request.files.get('file') is None:
                raise AttributeError
            _file = request.files['file']
            container = "user_%s" % current_user.id
            uploader.upload_file(_file, container=container)
            file_url = get_avatar_url(upload_method, _file.filename, container)
            tmp['media_url'] = file_url
            if tmp.get('info') is None:
                tmp['info'] = dict()
            tmp['info']['container'] = container
            tmp['info']['file_name'] = _file.filename
            return tmp
        else:
            return None
Example #4
0
def _handle_avatar_update(user, avatar_form):
    if avatar_form.validate_on_submit():
        _file = request.files['avatar']
        coordinates = (avatar_form.x1.data, avatar_form.y1.data,
                       avatar_form.x2.data, avatar_form.y2.data)
        prefix = time.time()
        _file.filename = "%s_avatar.png" % prefix
        container = "user_%s" % user.id
        uploader.upload_file(_file,
                             container=container,
                             coordinates=coordinates)
        # Delete previous avatar from storage
        if user.info.get('avatar'):
            uploader.delete_file(user.info['avatar'], container)
        upload_method = current_app.config.get('UPLOAD_METHOD')
        avatar_url = get_avatar_url(upload_method,
                                    _file.filename, container)
        user.info['avatar'] = _file.filename
        user.info['container'] = container
        user.info['avatar_url'] = avatar_url
        user_repo.update(user)
        cached_users.delete_user_summary(user.name)
        flash(gettext('Your avatar has been updated! It may \
                      take some minutes to refresh...'), 'success')
        return True
    else:
        flash("You have to provide an image file to update your avatar", "error")
        return False
Example #5
0
    def _file_upload(self, request):
        content_type = 'multipart/form-data'
        if content_type in request.headers.get('Content-Type'):
            tmp = dict()
            for key in request.form.keys():
                tmp[key] = request.form[key]

            ensure_authorized_to('create', HelpingMaterial,
                                 project_id=tmp['project_id'])
            upload_method = current_app.config.get('UPLOAD_METHOD')
            if request.files.get('file') is None:
                raise AttributeError
            _file = request.files['file']
            container = "user_%s" % current_user.id
            uploader.upload_file(_file,
                                 container=container)
            file_url = get_avatar_url(upload_method,
                                      _file.filename, container)
            tmp['media_url'] = file_url
            if tmp.get('info') is None:
                tmp['info'] = dict()
            tmp['info']['container'] = container
            tmp['info']['file_name'] = _file.filename
            return tmp
        else:
            return None
Example #6
0
def _handle_avatar_update(user, avatar_form):
    if avatar_form.validate_on_submit():
        _file = request.files['avatar']
        coordinates = (avatar_form.x1.data, avatar_form.y1.data,
                       avatar_form.x2.data, avatar_form.y2.data)
        prefix = time.time()
        _file.filename = "%s_avatar.png" % prefix
        container = "user_%s" % user.id
        uploader.upload_file(_file,
                             container=container,
                             coordinates=coordinates)
        # Delete previous avatar from storage
        if user.info.get('avatar'):
            uploader.delete_file(user.info['avatar'], container)
        upload_method = current_app.config.get('UPLOAD_METHOD')
        avatar_url = get_avatar_url(upload_method, _file.filename, container)
        user.info['avatar'] = _file.filename
        user.info['container'] = container
        user.info['avatar_url'] = avatar_url
        user_repo.update(user)
        cached_users.delete_user_summary(user.name)
        flash(
            gettext('Your avatar has been updated! It may \
                      take some minutes to refresh...'), 'success')
        return True
    else:
        flash("You have to provide an image file to update your avatar",
              "error")
        return False
Example #7
0
    def _file_upload(self, data):
        """Method that must be overriden by the class to allow file uploads for
        only a few classes."""
        cls_name = self.__class__.__name__.lower()
        content_type = 'multipart/form-data'
        if (content_type in request.headers.get('Content-Type') and
                cls_name in self.allowed_classes_upload):
            tmp = dict()
            for key in request.form.keys():
                tmp[key] = request.form[key]

            if isinstance(self, announcement.Announcement):
                # don't check project id for announcements
                ensure_authorized_to('create', self)
                if tmp.get('info') is not None:
                    try:
                        tmp['info'] = json.loads(tmp['info'])
                    except ValueError:
                        raise BadRequest
                upload_method = current_app.config.get('UPLOAD_METHOD')
                if request.files.get('file') is None:
                    raise AttributeError
                _file = request.files['file']
                container = "user_%s" % current_user.id
            else:
                ensure_authorized_to('create', self.__class__,
                                     project_id=tmp['project_id'])
                project = project_repo.get(tmp['project_id'])
                upload_method = current_app.config.get('UPLOAD_METHOD')
                if request.files.get('file') is None:
                    raise AttributeError
                _file = request.files['file']
                if current_user.is_authenticated:
                    if current_user.admin:
                        container = "user_%s" % project.owner.id
                    else:
                        container = "user_%s" % current_user.id
                else:
                    container = "anonymous"
            uploader.upload_file(_file,
                                 container=container)
            avatar_absolute = current_app.config.get('AVATAR_ABSOLUTE')
            file_url = get_avatar_url(upload_method,
                                      _file.filename,
                                      container,
                                      avatar_absolute)
            tmp['media_url'] = file_url
            if tmp.get('info') is None:
                tmp['info'] = dict()
            tmp['info']['container'] = container
            tmp['info']['file_name'] = _file.filename
            return tmp
        else:
            return None
Example #8
0
 def _file_upload(self, data):
     """Method that must be overriden by the class to allow file uploads for
     only a few classes."""
     cls_name = self.__class__.__name__.lower()
     content_type = 'multipart/form-data'
     if (content_type in request.headers.get('Content-Type') and
             cls_name in self.allowed_classes_upload):
         data = dict()
         for key in request.form.keys():
             if key in ['project_id', 'task_id']:
                 data[key] = int(request.form[key])
             elif key == 'info':
                 data[key] = json.loads(request.form[key])
             else:
                 data[key] = request.form[key]
         # inst = self._create_instance_from_request(data)
         data = self.hateoas.remove_links(data)
         inst = self.__class__(**data)
         self._add_user_info(inst)
         is_authorized(current_user, 'create', inst)
         upload_method = current_app.config.get('UPLOAD_METHOD')
         if request.files.get('file') is None:
             raise AttributeError
         _file = request.files['file']
         if current_user.is_authenticated():
             container = "user_%s" % current_user.id
         else:
             container = "anonymous"
         if _file.filename == 'blob' or _file.filename is None:
             _file.filename = "%s.png" % time.time()
         uploader.upload_file(_file,
                              container=container)
         avatar_absolute = current_app.config.get('AVATAR_ABSOLUTE')
         file_url = get_avatar_url(upload_method,
                                   _file.filename,
                                   container,
                                   avatar_absolute)
         data['media_url'] = file_url
         if data.get('info') is None:
             data['info'] = dict()
         data['info']['container'] = container
         data['info']['file_name'] = _file.filename
         return data
     else:
         return None
Example #9
0
 def _file_upload(self, data):
     """Method that must be overriden by the class to allow file uploads for
     only a few classes."""
     cls_name = self.__class__.__name__.lower()
     content_type = 'multipart/form-data'
     request_headers = request.headers.get('Content-Type')
     if request_headers is None:
         request_headers = []
     if (content_type in request_headers
             and cls_name in self.allowed_classes_upload):
         data = dict()
         for key in list(request.form.keys()):
             if key in ['project_id', 'task_id']:
                 data[key] = int(request.form[key])
             elif key == 'info':
                 data[key] = json.loads(request.form[key])
             else:
                 data[key] = request.form[key]
         # inst = self._create_instance_from_request(data)
         data = self.hateoas.remove_links(data)
         inst = self.__class__(**data)
         self._add_user_info(inst)
         is_authorized(current_user, 'create', inst)
         upload_method = current_app.config.get('UPLOAD_METHOD')
         if request.files.get('file') is None:
             raise AttributeError
         _file = request.files['file']
         if current_user.is_authenticated:
             container = "user_%s" % current_user.id
         else:
             container = "anonymous"
         if _file.filename == 'blob' or _file.filename is None:
             _file.filename = "%s.png" % time.time()
         uploader.upload_file(_file, container=container)
         avatar_absolute = current_app.config.get('AVATAR_ABSOLUTE')
         file_url = get_avatar_url(upload_method, _file.filename, container,
                                   avatar_absolute)
         data['media_url'] = file_url
         if data.get('info') is None:
             data['info'] = dict()
         data['info']['container'] = container
         data['info']['file_name'] = _file.filename
         return data
     else:
         return None
Example #10
0
    def _file_upload(self, data):
        """Method that must be overriden by the class to allow file uploads for
        only a few classes."""
        cls_name = self.__class__.__name__.lower()
        content_type = 'multipart/form-data'
        if (content_type in request.headers.get('Content-Type') and
                cls_name in self.allowed_classes_upload):
            tmp = dict()
            for key in request.form.keys():
                tmp[key] = request.form[key]

            if isinstance(self, announcement.Announcement):
                # don't check project id for announcements
                ensure_authorized_to('create', self)
                upload_method = current_app.config.get('UPLOAD_METHOD')
                if request.files.get('file') is None:
                    raise AttributeError
                _file = request.files['file']
                container = "user_%s" % current_user.id
            else:
                ensure_authorized_to('create', self.__class__,
                                    project_id=tmp['project_id'])
                project = project_repo.get(tmp['project_id'])
                upload_method = current_app.config.get('UPLOAD_METHOD')
                if request.files.get('file') is None:
                    raise AttributeError
                _file = request.files['file']
                if current_user.admin:
                    container = "user_%s" % project.owner.id
                else:
                    container = "user_%s" % current_user.id
            uploader.upload_file(_file,
                                 container=container)
            file_url = get_avatar_url(upload_method,
                                      _file.filename, container)
            tmp['media_url'] = file_url
            if tmp.get('info') is None:
                tmp['info'] = dict()
            tmp['info']['container'] = container
            tmp['info']['file_name'] = _file.filename
            return tmp
        else:
            return None
Example #11
0
def get_avatars_url():
    """Update db records with full urls for avatar and thumbnail
    :returns: Nothing

    """
    with app.app_context():
        if app.config.get('SERVER_NAME'):
            users = db.session.query(User).all()
            for user in users:
                upload_method = app.config.get('UPLOAD_METHOD')
                avatar = user.info.get('avatar')
                container = user.info.get('container')
                if (avatar and container):
                    print "Updating user: %s" % user.name
                    avatar_url = get_avatar_url(upload_method, avatar, container)
                    user.info['avatar_url'] = avatar_url
                    db.session.merge(user)
                    db.session.commit()
        else:
            print "Add SERVER_NAME to your config file."
Example #12
0
def get_thumbnail_urls():
    """Update db records with full urls for avatar and thumbnail
    :returns: Nothing

    """
    with app.app_context():
        if app.config.get('SERVER_NAME'):
            projects = db.session.query(Project).all()
            for project in projects:
                upload_method = app.config.get('UPLOAD_METHOD')
                thumbnail = project.info.get('thumbnail')
                container = project.info.get('container')
                if (thumbnail and container):
                    print "Updating project: %s" % project.short_name
                    thumbnail_url = get_avatar_url(upload_method, thumbnail, container)
                    project.info['thumbnail_url'] = thumbnail_url
                    db.session.merge(project)
                    db.session.commit()
        else:
            print "Add SERVER_NAME to your config file."
Example #13
0
def get_thumbnail_urls():
    """Update db records with full urls for avatar and thumbnail
    :returns: Nothing

    """
    with app.app_context():
        if app.config.get('SERVER_NAME'):
            projects = db.session.query(Project).all()
            for project in projects:
                upload_method = app.config.get('UPLOAD_METHOD')
                thumbnail = project.info.get('thumbnail')
                container = project.info.get('container')
                if (thumbnail and container):
                    print "Updating project: %s" % project.short_name
                    thumbnail_url = get_avatar_url(upload_method, thumbnail, container)
                    project.info['thumbnail_url'] = thumbnail_url
                    db.session.merge(project)
                    db.session.commit()
        else:
            print "Add SERVER_NAME to your config file."
Example #14
0
def get_avatars_url():
    """Update db records with full urls for avatar and thumbnail
    :returns: Nothing

    """
    with app.app_context():
        if app.config.get('SERVER_NAME'):
            users = db.session.query(User).all()
            for user in users:
                upload_method = app.config.get('UPLOAD_METHOD')
                avatar = user.info.get('avatar')
                container = user.info.get('container')
                if (avatar and container):
                    print "Updating user: %s" % user.name
                    avatar_url = get_avatar_url(upload_method, avatar, container)
                    user.info['avatar_url'] = avatar_url
                    db.session.merge(user)
                    db.session.commit()
        else:
            print "Add SERVER_NAME to your config file."
Example #15
0
    def test_get_avatar_url(self, mock_url_for):
        """Test get_avatar_url works."""
        util.get_avatar_url('rackspace', '1.png', '1', True)
        mock_url_for.assert_called_with('rackspace', container='1', filename='1.png')

        util.get_avatar_url('local', '1.png', '1', True)
        mock_url_for.assert_called_with('uploads.uploaded_file',
                                        _external=True,
                                        _scheme='http',
                                        filename='1/1.png')

        util.get_avatar_url('local', '1.png', '1', False)
        mock_url_for.assert_called_with('uploads.uploaded_file',
                                        _external=False,
                                        _scheme='http',
                                        filename='1/1.png')
Example #16
0
    def test_get_avatar_url(self, mock_url_for):
        """Test get_avatar_url works."""
        util.get_avatar_url('rackspace', '1.png', '1', True)
        mock_url_for.assert_called_with('rackspace', container='1', filename='1.png')

        util.get_avatar_url('local', '1.png', '1', True)
        mock_url_for.assert_called_with('uploads.uploaded_file',
                                        _external=True,
                                        _scheme='https',
                                        filename='1/1.png')

        util.get_avatar_url('local', '1.png', '1', False)
        mock_url_for.assert_called_with('uploads.uploaded_file',
                                        _external=False,
                                        _scheme='https',
                                        filename='1/1.png')
Example #17
0
def update_volume(short_name, volume_id):
    """Update a volume."""
    category = project_repo.get_category_by(short_name=short_name)
    if not category:  # pragma: no cover
        abort(404)

    ensure_authorized_to('update', category)
    volumes = category.info.get('volumes', [])

    try:
        volume = [v for v in volumes if v['id'] == volume_id][0]
    except IndexError:
        abort(404)

    form = VolumeForm(**volume)
    form.category_id.data = category.id
    all_importers = importer.get_all_importer_names()
    form.importer.choices = [(name, name) for name in all_importers]

    upload_form = AvatarUploadForm()
    import_form = GenericBulkTaskImportForm()(volume['importer'],
                                              **volume.get('data', {}))

    def update():
        """Helper function to update the current volume."""
        try:
            idx = [
                i for i, _vol in enumerate(volumes) if _vol['id'] == volume_id
            ][0]
        except IndexError:  # pragma: no cover
            abort(404)
        volumes[idx] = volume
        category.info['volumes'] = volumes
        project_repo.update_category(category)

    cat_projects = project_repo.filter_by(category_id=category.id)
    has_projects = len(
        [p for p in cat_projects if p.info.get('volume_id') == volume_id]) > 0

    if request.method == 'POST':
        # Process task import form
        if (request.form.get('btn') == 'Import'
                or request.body.get('btn') == 'Import'):

            import_form = GenericBulkTaskImportForm()(volume['importer'],
                                                      request.body)
            if import_form.validate():
                if has_projects:
                    flash('Update failed as projects have already been built',
                          'error')
                else:
                    volume['data'] = import_form.get_import_data()
                    import_data = import_form.get_import_data()
                    try:
                        importer.count_tasks_to_import(**import_data)
                        update()
                        flash('Volume updated', 'success')
                    except BulkImportException as err:
                        flash(err.message, 'error')

            else:
                flash('Please correct the errors', 'error')

        # Process volume details form
        elif request.form.get('btn') != 'Upload':
            form = VolumeForm(request.body)
            all_importers = importer.get_all_importer_names()
            form.importer.choices = [(name, name) for name in all_importers]

            if form.validate():
                if has_projects:
                    flash('Update failed as projects have already been built',
                          'error')
                else:
                    volume['name'] = form.name.data
                    volume['short_name'] = form.short_name.data
                    volume['importer'] = form.importer.data
                    update()
                    flash('Volume updated', 'success')
            else:
                flash('Please correct the errors', 'error')

        # Process thumbnail upload form
        else:
            if upload_form.validate_on_submit():
                _file = request.files['avatar']
                coordinates = (upload_form.x1.data, upload_form.y1.data,
                               upload_form.x2.data, upload_form.y2.data)
                suffix = time.time()
                _file.filename = "volume_{0}_{1}.png".format(
                    volume['id'], suffix)
                container = "category_{}".format(category.id)
                uploader.upload_file(_file,
                                     container=container,
                                     coordinates=coordinates)

                # Delete previous thumbnail from storage
                if volume.get('thumbnail'):
                    uploader.delete_file(volume['thumbnail'], container)
                volume['thumbnail'] = _file.filename
                volume['container'] = container
                upload_method = current_app.config.get('UPLOAD_METHOD')
                thumbnail_url = get_avatar_url(upload_method, _file.filename,
                                               container)
                volume['thumbnail_url'] = thumbnail_url
                update()
                project_repo.save_category(category)
                flash('Thumbnail updated', 'success')
                url = url_for('.get_volumes', short_name=category.short_name)
                return redirect_content_type(url)
            else:
                flash('You must provide a file', 'error')

    response = dict(form=form,
                    all_importers=all_importers,
                    upload_form=upload_form,
                    import_form=import_form,
                    volume=volume,
                    has_projects=has_projects)
    return handle_content_type(response)
Example #18
0
    def _file_upload(self, data):
        """Method that must be overriden by the class to allow file uploads for
        only a few classes."""
        cls_name = self.__class__.__name__.lower()
        """Accepting both content types - text or with file"""
        content_type_file = 'multipart/form-data'
        content_type_text =  'application/x-www-form-urlencoded'
        request_headers = request.headers.get('Content-Type')
        if request_headers is None:
            request_headers = []
        """ check for content type - file or text"""
        if ( (content_type_file in request_headers or content_type_text in request_headers)
            and cls_name in self.allowed_classes_upload):
            data = dict()
            for key in list(request.form.keys()):
                #Adding user_id in data
                if key in ['project_id']:
                    data[key] = int(request.form[key])
                elif key == 'info':
                    data[key] = json.loads(request.form[key])
                else:
                    data[key] = request.form[key]

            #Check if task exists
            tasks = task_repo.getTasks(data['info']['uuid'],data['project_id'])
            try:
                #if it exists, add as task id
                task = [row[0] for row in tasks]
                data['task_id'] = task[0]
            except:
                #if does not exist, add new task
                info = data['info']
                task = Task(project_id=data['project_id'], info=info,n_answers=10)
                task_repo.save(task)
                data['task_id'] = task.id
            
            """Try to get user by uuid, if not present, add a new user"""
            user = user_repo.get_by(mykaarma_user_id=data['useruuid'])
            if(user is None):
                name = get_mykaarma_username_from_full_name(data["fullname"]) 
                user = user_repo.get_by_name(name)
                while(user is not None):
                    name = get_mykaarma_username_from_full_name(data["fullname"])
                    user = user_repo.get_by_name(name)
                user = User(fullname=data['fullname'],
                    name=name,
                    email_addr=data['email'],
                    mykaarma_user_id=data['useruuid'])
                user_repo.save(user)

            """ add user id extracted from user repo"""
            data['user_id'] = user.id
            """ delete extra keys to suit Taskrun class format"""
            del data['useruuid']
            del data['fullname']
            del data['email']
            data = self.hateoas.remove_links(data)
            inst = self.__class__(**data)
            self._add_user_info(inst)
            is_authorized(current_user, 'create', inst)
            upload_method = current_app.config.get('UPLOAD_METHOD')
            """Add user id to container"""
            container = "user_%s" % data['user_id']
            if data.get('info') is None:
                data['info'] = dict()
            data['info']['container'] = container
            if(request.files.get('file') is not None):
                _file = request.files['file']
                if _file.filename == 'blob' or _file.filename is None:
                   _file.filename = "%s.png" % time.time()
                uploader.upload_file(_file,
                                    container=container)
                avatar_absolute = current_app.config.get('AVATAR_ABSOLUTE')
                file_url = get_avatar_url(upload_method,
                                         _file.filename,
                                         container,
                                         avatar_absolute)
                data['media_url'] = file_url
                data['info']['file_name'] = _file.filename
            return data
        else:
            return None