示例#1
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     dataframe = self._respond_csv(ty, project.id)
     if dataframe is not None:
         info_dataframe = self._respond_csv(ty, project.id, info_only=True)
         datafile = tempfile.NamedTemporaryFile()
         info_datafile = tempfile.NamedTemporaryFile()
         try:
             dataframe.to_csv(datafile, index=False,
                              encoding='utf-8')
             info_dataframe.to_csv(
                 info_datafile, index=False, encoding='utf-8')
             datafile.flush()
             info_datafile.flush()
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 _zip = self._zip_factory(zipped_datafile.name)
                 _zip.write(
                     datafile.name, secure_filename('%s_%s.csv' % (name, ty)))
                 _zip.write(
                     info_datafile.name, secure_filename('%s_%s_info_only.csv' % (name, ty)))
                 _zip.close()
                 container = "user_%d" % project.owner_id
                 _file = FileStorage(
                     filename=self.download_name(project, ty), stream=zipped_datafile)
                 uploader.upload_file(_file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
             info_datafile.close()
示例#2
0
    def _make_zip(self, project, ty):
        print '****** entered _make_zip *******'
        # json_buffer = self._respond_cust_exp_json(project.id)
        # if json_buffer is None:
            # logging.debug('buffer is null')
            # logging.debug('****** exit create_zip *******')
            # return

        # detailed csv    
        file_full = tempfile.NamedTemporaryFile()
        wfull = UnicodeWriter(file_full)
        self._write_csv(wfull)
        file_full.flush()
        
        #summarized csv
        file_summary = tempfile.NamedTemporaryFile()
        wfull = UnicodeWriter(file_summary)
        self._write_csv_summary(wfull)
        file_summary.flush()
        
        print '********** file names, file_full: %s, file_summary: %s' % (file_full, file_summary)
        name = self._project_name_latin_encoded(project)			
        #datafile = tempfile.NamedTemporaryFile()
        try:
            # for line in json_buffer:
                # datafile.write(str(line))
            # datafile.flush()
            zipped_datafile = tempfile.NamedTemporaryFile()
            try:
                zip = self._zip_factory(zipped_datafile.name)
                print '******** zip filename: %s' % zip
                #zip.write(datafile.name, secure_filename('%s_%s.json' % (name, ty)))
                zip.write(file_full.name, secure_filename('%s_%s_full.csv' % (name, ty)))
                zip.write(file_summary.name, secure_filename('%s_%s_summary.csv' % (name, ty)))
                zip.close()
                container = "user_%d" % project.owner_id
                file = FileStorage(filename=self.download_name(project, ty), stream=zipped_datafile)
                uploader.upload_file(file, container=container)
                print '****** zip file %s upload complete *******' % file
            finally:
                zipped_datafile.close()
        finally:
            #datafile.close()
            file_full.close()
            file_summary.close()
            
        # create response; copied from Exporter:get_zip()
        filename = self.download_name(project, ty)
        if isinstance(uploader, local.LocalUploader):
            filepath = self._download_path(project)
            res = send_file(filename_or_fp=safe_join(filepath, filename),
                            mimetype='application/octet-stream',
                            as_attachment=True,
                            attachment_filename=filename)
            return res
        else:
            return redirect(url_for('rackspace', filename=filename,
                                    container=self._container(project),
                                    _external=True))
            
示例#3
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     dataframe = self._respond_csv(ty, project.id)
     if dataframe is not None:
         info_dataframe = self._respond_csv(ty, project.id, info_only=True)
         datafile = tempfile.NamedTemporaryFile(mode='w')
         info_datafile = tempfile.NamedTemporaryFile(mode='w')
         try:
             dataframe.to_csv(datafile, index=False, encoding='utf-8')
             info_dataframe.to_csv(info_datafile,
                                   index=False,
                                   encoding='utf-8')
             datafile.flush()
             info_datafile.flush()
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 _zip = self._zip_factory(zipped_datafile.name)
                 _zip.write(datafile.name,
                            secure_filename('%s_%s.csv' % (name, ty)))
                 _zip.write(
                     info_datafile.name,
                     secure_filename('%s_%s_info_only.csv' % (name, ty)))
                 _zip.close()
                 container = "user_%d" % project.owner_id
                 _file = FileStorage(filename=self.download_name(
                     project, ty),
                                     stream=zipped_datafile)
                 uploader.upload_file(_file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
             info_datafile.close()
示例#4
0
    def handle_zip(self, name, data, ty, user_id, project, ext, zipname=None):
        zipped_datafile = tempfile.NamedTemporaryFile()
        _zip = self._zip_factory(zipped_datafile.name)
        try:
            datafile = tempfile.NamedTemporaryFile()
            try:
                datafile.write(json.dumps(data))
                datafile.flush()
                _zip.write(datafile.name,
                           secure_filename('%s_%s.%s' % (name, ty, ext)))
            finally:
                datafile.close()
        finally:
            _zip.close()
            if user_id:
                container = "user_%d" % user_id
                if zipname is None:
                    zipname = "user_%s.zip" % user_id
                else:
                    zipname = "%s_sec_%s" % (uuid.uuid1(), zipname)
                _file = FileStorage(filename=zipname, stream=zipped_datafile)
            else:
                container = "user_%d" % project.owner_id
                fname = self.download_name(project, ty)
                _file = FileStorage(filename=fname, stream=zipped_datafile)
            uploader.upload_file(_file, container=container)

            if zipname and "_sec_" in zipname:
                days = current_app.config.get('TTL_ZIP_SEC_FILES', 3)
                scheduler.enqueue_in(timedelta(days=days),
                                     uploader.delete_file, zipname, container)
            zipped_datafile.close()
            return zipname
示例#5
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     csv_task_generator = self._respond_csv(ty, project.id)
     if csv_task_generator is not None:
         # TODO: use temp file from csv generation directly
         datafile = tempfile.NamedTemporaryFile()
         try:
             for line in csv_task_generator:
                 datafile.write(str(line))
             datafile.flush()
             csv_task_generator.close()  # delete temp csv file
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 zip = self._zip_factory(zipped_datafile.name)
                 zip.write(datafile.name,
                           secure_filename('%s_%s.csv' % (name, ty)))
                 zip.close()
                 container = "user_%d" % project.owner_id
                 file = FileStorage(filename=self.download_name(
                     project, ty),
                                    stream=zipped_datafile)
                 uploader.upload_file(file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
示例#6
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
示例#7
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
示例#8
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
示例#9
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
示例#10
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     csv_task_generator = self._respond_csv(ty, project.id)
     if csv_task_generator is not None:
         # TODO: use temp file from csv generation directly
         datafile = tempfile.NamedTemporaryFile()
         try:
             for line in csv_task_generator:
                 datafile.write(str(line))
             datafile.flush()
             csv_task_generator.close()  # delete temp csv file
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 zip = self._zip_factory(zipped_datafile.name)
                 zip.write(
                     datafile.name, secure_filename('%s_%s.csv' % (name, ty)))
                 zip.close()
                 container = "user_%d" % project.owner_id
                 file = FileStorage(
                     filename=self.download_name(project, ty), stream=zipped_datafile)
                 uploader.upload_file(file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
示例#11
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
示例#12
0
    def _make_cust_exp_zip(self, project, ty):
        logging.debug('****** entered create_zip *******')
        json_buffer = self._respond_cust_exp_json(project.id)
        if json_buffer is None:
            logging.debug('buffer is null')
            logging.debug('****** exit create_zip *******')
            return

        name = self._project_name_latin_encoded(project)
        datafile = tempfile.NamedTemporaryFile()
        try:
            for line in json_buffer:
                datafile.write(str(line))
            datafile.flush()
            zipped_datafile = tempfile.NamedTemporaryFile()
            try:
                zip = self._zip_factory(zipped_datafile.name)
                logging.debug('zip filename: %s', zip)
                zip.write(datafile.name,
                          secure_filename('%s_%s.json' % (name, ty)))
                zip.close()
                container = "user_%d" % project.owner_id
                file = FileStorage(filename=self.download_name(project, ty),
                                   stream=zipped_datafile)
                uploader.upload_file(file, container=container)
                logging.debug('****** zip file %s upload complete *******',
                              file)
            finally:
                zipped_datafile.close()
        finally:
            datafile.close()

        # create response; copied from Exporter:get_zip()
        filename = self.download_name(project, ty)
        if isinstance(uploader, local.LocalUploader):
            filepath = self._download_path(project)
            res = send_file(filename_or_fp=safe_join(filepath, filename),
                            mimetype='application/octet-stream',
                            as_attachment=True,
                            attachment_filename=filename)
            return res
        else:
            return redirect(
                url_for('rackspace',
                        filename=filename,
                        container=self._container(project),
                        _external=True))
示例#13
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
示例#14
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
示例#15
0
    def _make_zipfile(self, project, obj, file_format, obj_generator, expanded=False):
        """Generate a ZIP of a certain type and upload it.

        :param project: A project object
        :param obj: The domain object to be exported
        :param file_format: The file format of the export
        :param obj_generator: A generator object containing
            the data to be written to file
        :param expanded: Boolean indicating whether or not
            relevant object metadata should be included
            in the export

        :return: The path where the .zip file is saved
        """
        name = self._project_name_latin_encoded(project)
        if obj_generator is not None:
            with tempfile.NamedTemporaryFile() as datafile:
                for line in obj_generator:
                    datafile.write(str(line))
                datafile.flush()
                obj_generator.close()

                with tempfile.NamedTemporaryFile() as zipped_datafile:
                    with self._zip_factory(zipped_datafile.name) as _zip:
                        _zip.write(datafile.name,
                                   secure_filename('{0}_{1}.{2}'
                                                   .format(name, obj, file_format)))
                        _zip.content_type = 'application/zip'

                    filename = self.download_name(project, obj)
                    zip_file = FileStorage(filename=filename,
                                           stream=zipped_datafile)

                    container = self._container(project)
                    if uploader.file_exists(filename, container):
                        assert uploader.delete_file(filename, container)
                    uploader.upload_file(zip_file, container=container)

                    if isinstance(uploader, local.LocalUploader):
                        path = uploader.get_file_path(container, filename)
                    else:
                        path = None

                    return path
示例#16
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
示例#17
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     json_task_generator = self._respond_json(ty, project.id)
     if json_task_generator is not None:
         datafile = tempfile.NamedTemporaryFile()
         try:
             datafile.write(json.dumps(json_task_generator))
             datafile.flush()
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 _zip = self._zip_factory(zipped_datafile.name)
                 _zip.write(datafile.name, secure_filename('%s_%s.json' % (name, ty)))
                 _zip.close()
                 container = "user_%d" % project.owner_id
                 _file = FileStorage(filename=self.download_name(project, ty), stream=zipped_datafile)
                 uploader.upload_file(_file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
示例#18
0
 def _make_zip(self, app, ty):
     name = self._app_name_latin_encoded(app)
     json_task_generator = self._respond_json(ty, app.id)
     if json_task_generator is not None:
         datafile = tempfile.NamedTemporaryFile()
         try:
             for line in json_task_generator:
                 datafile.write(str(line))
             datafile.flush()
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 zip = self._zip_factory(zipped_datafile.name)
                 zip.write(datafile.name, secure_filename('%s_%s.json' % (name, ty)))
                 zip.close()
                 container = "user_%d" % app.owner_id
                 file = FileStorage(filename=self.download_name(app, ty), stream=zipped_datafile)
                 uploader.upload_file(file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
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)
        user.info = {'avatar': _file.filename,
                             'container': container}
        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')
    else:
        flash("You have to provide an image file to update your avatar", "error")
示例#20
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)
        user.info = {'avatar': _file.filename,
                             'container': container}
        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')
    else:
        flash("You have to provide an image file to update your avatar", "error")
示例#21
0
    def handle_zip(self, name, data, ty, user_id, project, ext, zipname=None):
        zipped_datafile = tempfile.NamedTemporaryFile()
        _zip = self._zip_factory(zipped_datafile.name)
        try:
            datafile = tempfile.NamedTemporaryFile()
            try:
                datafile.write(json.dumps(data))
                datafile.flush()
                _zip.write(datafile.name,
                           secure_filename('%s_%s.%s' % (name, ty, ext)))
            finally:
                datafile.close()
        finally:
            _zip.close()
            if user_id:
                container = "user_%d" % user_id
                if zipname is None:
                    zipname = "user_%s.zip" % user_id
                else:
                    zipname = "%s_sec_%s" % (uuid.uuid1(), zipname)
                _file = FileStorage(filename=zipname,
                                    stream=zipped_datafile)
            else:
                container = "user_%d" % project.owner_id
                fname = self.download_name(project, ty)
                _file = FileStorage(filename=fname,
                                    stream=zipped_datafile)
            uploader.upload_file(_file, container=container)

            if zipname and "_sec_" in zipname:
                days = current_app.config.get('TTL_ZIP_SEC_FILES', 3)
                scheduler.enqueue_in(timedelta(days=days),
                                     uploader.delete_file,
                                     zipname,
                                     container)
            zipped_datafile.close()
            return zipname
示例#22
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)
示例#23
0
def update_profile(name):
    """
    Update user's profile.

    Returns Jinja2 template.

    """
    user = User.query.filter_by(name=name).first()
    if not user:
        return abort(404)
    require.user.update(user)
    show_passwd_form = True
    if user.twitter_user_id or user.google_user_id or user.facebook_user_id:
        show_passwd_form = False
    usr = cached_users.get_user_summary(name)
    # Extend the values
    current_user.rank = usr.get('rank')
    current_user.score = usr.get('score')
    # Title page
    title_msg = "Update your profile: %s" % current_user.fullname
    # Creation of forms
    update_form = UpdateProfileForm(obj=user)
    update_form.set_locales(current_app.config['LOCALES'])
    avatar_form = AvatarUploadForm()
    password_form = ChangePasswordForm()
    external_form = update_form


    if request.method == 'GET':
        return render_template('account/update.html',
                               title=title_msg,
                               user=usr,
                               form=update_form,
                               upload_form=avatar_form,
                               password_form=password_form,
                               external_form=external_form,
                               show_passwd_form=show_passwd_form)
    else:
        # Update user avatar
        if request.form.get('btn') == 'Upload':
            avatar_form = AvatarUploadForm()
            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" % current_user.id
                uploader.upload_file(file,
                                     container=container,
                                     coordinates=coordinates)
                # Delete previous avatar from storage
                if current_user.info.get('avatar'):
                    uploader.delete_file(current_user.info['avatar'], container)
                current_user.info = {'avatar': file.filename,
                                     'container': container}
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your avatar has been updated! It may \
                              take some minutes to refresh...'), 'success')
                return redirect(url_for('.update_profile', name=current_user.name))
            else:
                flash("You have to provide an image file to update your avatar",
                      "error")
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user profile
        elif request.form.get('btn') == 'Profile':
            update_form = UpdateProfileForm()
            update_form.set_locales(current_app.config['LOCALES'])
            if update_form.validate():
                current_user.id = update_form.id.data
                current_user.fullname = update_form.fullname.data
                current_user.name = update_form.name.data
                current_user.email_addr = update_form.email_addr.data
                current_user.privacy_mode = update_form.privacy_mode.data
                current_user.locale = update_form.locale.data
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(url_for('.update_profile', name=current_user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % current_user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)

        # Update user password
        elif request.form.get('btn') == 'Password':
            # Update the data because passing it in the constructor does not work
            update_form.name.data = user.name
            update_form.fullname.data = user.fullname
            update_form.email_addr.data = user.email_addr
            update_form.ckan_api.data = user.ckan_api
            external_form = update_form
            if password_form.validate_on_submit():
                user = db.session.query(model.user.User).get(current_user.id)
                if user.check_password(password_form.current_password.data):
                    user.set_password(password_form.new_password.data)
                    db.session.add(user)
                    db.session.commit()
                    flash(gettext('Yay, you changed your password succesfully!'),
                          'success')
                    return redirect(url_for('.update_profile', name=name))
                else:
                    msg = gettext("Your current password doesn't match the "
                                  "one in our records")
                    flash(msg, 'error')
                    return render_template('/account/update.html',
                                           form=update_form,
                                           upload_form=avatar_form,
                                           password_form=password_form,
                                           external_form=external_form,
                                           title=title_msg,
                                           show_passwd_form=show_passwd_form)
            else:
                flash(gettext('Please correct the errors'), 'error')
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user external services
        elif request.form.get('btn') == 'External':
            del external_form.locale
            del external_form.email_addr
            del external_form.fullname
            del external_form.name
            if external_form.validate():
                current_user.ckan_api = external_form.ckan_api.data or None
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(url_for('.update_profile', name=current_user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % current_user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Otherwise return 415
        else:
            return abort(415)
示例#24
0
def update_profile(name):
    """
    Update user's profile.

    Returns Jinja2 template.

    """
    user = User.query.filter_by(name=name).first()
    if not user:
        return abort(404)
    require.user.update(user)
    show_passwd_form = True
    if user.twitter_user_id or user.google_user_id or user.facebook_user_id:
        show_passwd_form = False
    usr = cached_users.get_user_summary(name)
    # Extend the values
    current_user.rank = usr.get('rank')
    current_user.score = usr.get('score')
    # Title page
    title_msg = "Update your profile: %s" % current_user.fullname
    # Creation of forms
    update_form = UpdateProfileForm(obj=user)
    update_form.set_locales(current_app.config['LOCALES'])
    avatar_form = AvatarUploadForm()
    password_form = ChangePasswordForm()
    external_form = update_form

    if request.method == 'GET':
        return render_template('account/update.html',
                               title=title_msg,
                               user=usr,
                               form=update_form,
                               upload_form=avatar_form,
                               password_form=password_form,
                               external_form=external_form,
                               show_passwd_form=show_passwd_form)
    else:
        # Update user avatar
        if request.form.get('btn') == 'Upload':
            avatar_form = AvatarUploadForm()
            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" % current_user.id
                uploader.upload_file(file,
                                     container=container,
                                     coordinates=coordinates)
                # Delete previous avatar from storage
                if current_user.info.get('avatar'):
                    uploader.delete_file(current_user.info['avatar'],
                                         container)
                current_user.info = {
                    'avatar': file.filename,
                    'container': container
                }
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(
                    gettext('Your avatar has been updated! It may \
                              take some minutes to refresh...'), 'success')
                return redirect(
                    url_for('.update_profile', name=current_user.name))
            else:
                flash(
                    "You have to provide an image file to update your avatar",
                    "error")
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user profile
        elif request.form.get('btn') == 'Profile':
            update_form = UpdateProfileForm()
            update_form.set_locales(current_app.config['LOCALES'])
            if update_form.validate():
                current_user.id = update_form.id.data
                current_user.fullname = update_form.fullname.data
                current_user.name = update_form.name.data
                current_user.email_addr = update_form.email_addr.data
                current_user.privacy_mode = update_form.privacy_mode.data
                current_user.locale = update_form.locale.data
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(
                    url_for('.update_profile', name=current_user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % current_user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)

        # Update user password
        elif request.form.get('btn') == 'Password':
            # Update the data because passing it in the constructor does not work
            update_form.name.data = user.name
            update_form.fullname.data = user.fullname
            update_form.email_addr.data = user.email_addr
            update_form.ckan_api.data = user.ckan_api
            external_form = update_form
            if password_form.validate_on_submit():
                user = db.session.query(model.user.User).get(current_user.id)
                if user.check_password(password_form.current_password.data):
                    user.set_password(password_form.new_password.data)
                    db.session.add(user)
                    db.session.commit()
                    flash(
                        gettext('Yay, you changed your password succesfully!'),
                        'success')
                    return redirect(url_for('.update_profile', name=name))
                else:
                    msg = gettext("Your current password doesn't match the "
                                  "one in our records")
                    flash(msg, 'error')
                    return render_template('/account/update.html',
                                           form=update_form,
                                           upload_form=avatar_form,
                                           password_form=password_form,
                                           external_form=external_form,
                                           title=title_msg,
                                           show_passwd_form=show_passwd_form)
            else:
                flash(gettext('Please correct the errors'), 'error')
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user external services
        elif request.form.get('btn') == 'External':
            del external_form.locale
            del external_form.email_addr
            del external_form.fullname
            del external_form.name
            if external_form.validate():
                current_user.ckan_api = external_form.ckan_api.data or None
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(
                    url_for('.update_profile', name=current_user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % current_user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Otherwise return 415
        else:
            return abort(415)
示例#25
0
def update_profile(name):
    """
    Update user's profile.

    Returns Jinja2 template.

    """
    user = user_repo.get_by_name(name)
    if not user:
        return abort(404)
    require.user.update(user)
    show_passwd_form = True
    if user.twitter_user_id or user.google_user_id or user.facebook_user_id:
        show_passwd_form = False
    usr = cached_users.get_user_summary(name)
    # Extend the values
    user.rank = usr.get('rank')
    user.score = usr.get('score')
    # Title page
    title_msg = "Update your profile: %s" % user.fullname
    # Creation of forms
    update_form = UpdateProfileForm(obj=user)
    update_form.set_locales(current_app.config['LOCALES'])
    avatar_form = AvatarUploadForm()
    password_form = ChangePasswordForm()
    external_form = update_form

    if request.method == 'GET':
        return render_template('account/update.html',
                               title=title_msg,
                               user=usr,
                               form=update_form,
                               upload_form=avatar_form,
                               password_form=password_form,
                               external_form=external_form,
                               show_passwd_form=show_passwd_form)
    else:
        acc_conf_dis = current_app.config.get('ACCOUNT_CONFIRMATION_DISABLED')
        # Update user avatar
        if request.form.get('btn') == 'Upload':
            avatar_form = AvatarUploadForm()
            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)
                user.info = {'avatar': file.filename, 'container': container}
                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 redirect(url_for('.update_profile', name=user.name))
            else:
                flash(
                    "You have to provide an image file to update your avatar",
                    "error")
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user profile
        elif request.form.get('btn') == 'Profile':
            update_form = UpdateProfileForm()
            update_form.set_locales(current_app.config['LOCALES'])
            if update_form.validate():
                user.id = update_form.id.data
                user.fullname = update_form.fullname.data
                user.name = update_form.name.data
                if (user.email_addr != update_form.email_addr.data
                        and acc_conf_dis is False):
                    user.valid_email = False
                    user.newsletter_prompted = False
                    account = dict(fullname=update_form.fullname.data,
                                   name=update_form.name.data,
                                   email_addr=update_form.email_addr.data)
                    confirm_url = get_email_confirmation_url(account)
                    subject = ('You have updated your email in %s! Verify it' \
                               % current_app.config.get('BRAND'))
                    msg = dict(subject=subject,
                               recipients=[update_form.email_addr.data],
                               body=render_template(
                                   '/account/email/validate_email.md',
                                   user=account,
                                   confirm_url=confirm_url))
                    msg['html'] = markdown(msg['body'])
                    mail_queue.enqueue(send_mail, msg)
                    user.confirmation_email_sent = True
                    fls = gettext('An email has been sent to verify your \
                                  new email: %s. Once you verify it, it will \
                                  be updated.' % account['email_addr'])
                    flash(fls, 'info')
                if acc_conf_dis:
                    user.email_addr = update_form.email_addr.data
                user.privacy_mode = update_form.privacy_mode.data
                user.locale = update_form.locale.data
                user_repo.update(user)
                cached_users.delete_user_summary(user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(url_for('.update_profile', name=user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)

        # Update user password
        elif request.form.get('btn') == 'Password':
            # Update the data because passing it in the constructor does not work
            update_form.name.data = user.name
            update_form.fullname.data = user.fullname
            update_form.email_addr.data = user.email_addr
            update_form.ckan_api.data = user.ckan_api
            external_form = update_form
            if password_form.validate_on_submit():
                user = user_repo.get(user.id)
                if user.check_password(password_form.current_password.data):
                    user.set_password(password_form.new_password.data)
                    user_repo.update(user)
                    flash(
                        gettext('Yay, you changed your password succesfully!'),
                        'success')
                    return redirect(url_for('.update_profile', name=name))
                else:
                    msg = gettext("Your current password doesn't match the "
                                  "one in our records")
                    flash(msg, 'error')
                    return render_template('/account/update.html',
                                           form=update_form,
                                           upload_form=avatar_form,
                                           password_form=password_form,
                                           external_form=external_form,
                                           title=title_msg,
                                           show_passwd_form=show_passwd_form)
            else:
                flash(gettext('Please correct the errors'), 'error')
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Update user external services
        elif request.form.get('btn') == 'External':
            del external_form.locale
            del external_form.email_addr
            del external_form.fullname
            del external_form.name
            if external_form.validate():
                user.ckan_api = external_form.ckan_api.data or None
                user_repo.update(user)
                cached_users.delete_user_summary(user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(url_for('.update_profile', name=user.name))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % user.fullname
                return render_template('/account/update.html',
                                       form=update_form,
                                       upload_form=avatar_form,
                                       password_form=password_form,
                                       external_form=external_form,
                                       title=title_msg,
                                       show_passwd_form=show_passwd_form)
        # Otherwise return 415
        else:
            return abort(415)
示例#26
0
def update_profile():
    """
    Update user's profile.

    Returns Jinja2 template.

    """
    form = UpdateProfileForm()
    upload_form = AvatarUploadForm()
    if request.method == 'GET':
        form = UpdateProfileForm(obj=current_user)
        form.set_locales(current_app.config['LOCALES'])
        form.populate_obj(current_user)

        title_msg = "Update your profile: %s" % current_user.fullname
        return render_template('account/update.html',
                               title=title_msg,
                               form=form,
                               upload_form=upload_form)
    else:
        form = UpdateProfileForm(request.form)
        upload_form = AvatarUploadForm(request.form)
        form.set_locales(current_app.config['LOCALES'])
        if request.form['btn'] == 'Upload':
            avatar = request.files['avatar']
            extension = avatar.filename.rsplit(".")[1]
            coordinates = (upload_form.x1.data, upload_form.y1.data,
                           upload_form.x2.data, upload_form.y2.data)
            prefix = time.time()
            avatar.filename = "%s_avatar.%s" % (prefix, extension)
            container = "user_%s" % current_user.id
            uploader.upload_file(avatar,
                                 container=container,
                                 coordinates=coordinates)
            # Delete previous avatar from storage
            if 'avatar' in current_user.info:
              uploader.delete_file(current_user.info['avatar'], container)
            current_user.info = {'avatar': avatar.filename,
                                 'container': container}
            db.session.commit()
            cached_users.delete_user_summary(current_user.name)
            flash(gettext('Your avatar has been updated! It may \
                          take some minutes to refresh...'), 'success')
            return redirect(url_for('.profile'))
        else:
            if form.validate():
                current_user.id = form.id.data
                current_user.fullname = form.fullname.data
                current_user.name = form.name.data
                current_user.email_addr = form.email_addr.data
                current_user.ckan_api = form.ckan_api.data
                current_user.privacy_mode = form.privacy_mode.data
                db.session.commit()
                cached_users.delete_user_summary(current_user.name)
                flash(gettext('Your profile has been updated!'), 'success')
                return redirect(url_for('.profile'))
            else:
                flash(gettext('Please correct the errors'), 'error')
                title_msg = 'Update your profile: %s' % current_user.fullname
                return render_template('/account/update.html', form=form,
                                       upload_form=upload_form,
                                       title=title_msg)
示例#27
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