示例#1
0
def export_pentabarf_task(self, event_id, temp=True):
    event = safe_query(db, Event, 'id', event_id, 'event_id')

    try:
        if temp:
            filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/' + event_id + '/')
        else:
            filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/' + event_id + '/')

        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = "pentabarf.xml"
        file_path = os.path.join(filedir, filename)
        with open(file_path, "w") as temp_file:
            temp_file.write(str(PentabarfExporter.export(event_id), 'utf-8'))
        pentabarf_file = UploadedFile(file_path=file_path, filename=filename)
        if temp:
            pentabarf_url = upload(pentabarf_file, UPLOAD_PATHS['exports-temp']['pentabarf'].format(event_id=event_id))
        else:
            pentabarf_url = upload(pentabarf_file, UPLOAD_PATHS['exports']['pentabarf'].format(event_id=event_id))
        result = {
            'download_url': pentabarf_url
        }
        if not temp:
            event.pentabarf_url = pentabarf_url
            save_to_db(event)

    except Exception as e:
        print(traceback.format_exc())
        result = {'__error': True, 'result': str(e)}

    return result
示例#2
0
def export_xcal_task(self, event_id, temp=True):
    event = safe_query(db, Event, 'id', event_id, 'event_id')

    try:
        if temp:
            filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/' + event_id + '/')
        else:
            filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/' + event_id + '/')

        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = "xcal.xcs"
        file_path = os.path.join(filedir, filename)
        with open(file_path, "w") as temp_file:
            temp_file.write(str(XCalExporter.export(event_id), 'utf-8'))
        xcal_file = UploadedFile(file_path=file_path, filename=filename)
        if temp:
            xcal_url = upload(xcal_file, UPLOAD_PATHS['exports-temp']['xcal'].format(event_id=event_id))
        else:
            xcal_url = upload(xcal_file, UPLOAD_PATHS['exports']['xcal'].format(event_id=event_id))
        result = {
            'download_url': xcal_url
        }
        if not temp:
            event.xcal_url = xcal_url
            save_to_db(event)

    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logging.error('Error in xcal download')

    return result
示例#3
0
def export_ical_task(self, event_id, temp=True):
    event = safe_query(Event, 'id', event_id, 'event_id')

    try:
        file_path = generate_ics_file(event_id, temp)

        filename = os.path.basename(file_path)
        ical_file = UploadedFile(file_path=file_path, filename=filename)
        if temp:
            ical_url = upload(
                ical_file, UPLOAD_PATHS['exports-temp']['ical'].format(event_id=event_id)
            )
        else:
            ical_url = upload(
                ical_file, UPLOAD_PATHS['exports']['ical'].format(event_id=event_id)
            )
        result = {'download_url': ical_url}
        if not temp:
            event.ical_url = ical_url
            save_to_db(event)

    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logger.exception('Error in ical download')

    return result
示例#4
0
def upload_file():
    force_local = request.args.get('force_local', 'false')
    if 'file' in request.files:
        files = request.files['file']
        file_uploaded = uploaded_file(files=files)
        if force_local == 'true':
            files_url = upload_local(
                file_uploaded,
                UPLOAD_PATHS['temp']['event'].format(uuid=uuid.uuid4()))
        else:
            files_url = upload(
                file_uploaded,
                UPLOAD_PATHS['temp']['event'].format(uuid=uuid.uuid4()))
    elif 'files[]' in request.files:
        files = request.files.getlist('files[]')
        files_uploaded = uploaded_file(files=files, multiple=True)
        files_url = []
        for file_uploaded in files_uploaded:
            if force_local == 'true':
                files_url.append(
                    upload_local(
                        file_uploaded, UPLOAD_PATHS['temp']['event'].format(
                            uuid=uuid.uuid4())))
            else:
                files_url.append(
                    upload(
                        file_uploaded, UPLOAD_PATHS['temp']['event'].format(
                            uuid=uuid.uuid4())))
    else:
        abort(make_response(jsonify(error="Bad Request"), 400))

    return jsonify({"url": files_url})
示例#5
0
def create_save_pdf(pdf_data, key, dir_path='/static/uploads/pdf/temp/'):
    """
    Create and Saves PDFs from html
    :param pdf_data:
    :return:
    """
    filedir = current_app.config.get('BASE_DIR') + dir_path

    if not os.path.isdir(filedir):
        os.makedirs(filedir)

    filename = get_file_name() + '.pdf'
    dest = filedir + filename

    file = open(dest, "wb")
    pisa.CreatePDF(io.BytesIO(pdf_data.encode('utf-8')), file)
    file.close()

    uploaded_file = UploadedFile(dest, filename)
    upload_path = key.format(identifier=get_file_name())
    new_file = upload(uploaded_file, upload_path)
    # Removing old file created
    os.remove(dest)

    return new_file
示例#6
0
def create_save_pdf(pdf_data,
                    key,
                    dir_path='/static/uploads/pdf/temp/',
                    identifier=get_file_name(),
                    upload_dir='static/media/'):
    """
    Create and Saves PDFs from html
    :param pdf_data:
    :return:
    """
    filedir = current_app.config.get('BASE_DIR') + dir_path

    if not os.path.isdir(filedir):
        os.makedirs(filedir)

    filename = identifier + '.pdf'
    dest = filedir + filename

    file = open(dest, "wb")
    pisa.CreatePDF(io.BytesIO(pdf_data.encode('utf-8')), file)
    file.close()

    uploaded_file = UploadedFile(dest, filename)
    upload_path = key.format(identifier=identifier)
    new_file = upload(uploaded_file, upload_path, upload_dir=upload_dir)
    # Removing old file created
    os.remove(dest)

    return new_file
示例#7
0
def export_attendees_csv_task(self, event_id):
    attendees = db.session.query(TicketHolder).filter_by(event_id=event_id)
    custom_forms = db.session.query(CustomForms).filter_by(
        event_id=event_id, form=CustomForms.TYPE.ATTENDEE, is_included=True
    )
    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = f"attendees-{uuid.uuid1().hex}.csv"
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_attendees_csv

            content = export_attendees_csv(attendees, custom_forms)
            for row in content:
                writer.writerow(row)
        attendees_csv_file = UploadedFile(file_path=file_path, filename=filename)
        attendees_csv_url = upload(
            attendees_csv_file,
            UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier=''),
        )
        result = {'download_url': attendees_csv_url}
    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logger.exception('Error in exporting attendees list as CSV')

    return result
示例#8
0
def export_speakers_csv_task(self, event_id):
    speakers = db.session.query(Speaker).filter_by(event_id=event_id)
    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = "speakers-{}.csv".format(uuid.uuid1().hex)
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_speakers_csv
            content = export_speakers_csv(speakers)
            for row in content:
                writer.writerow(row)
        speakers_csv_file = UploadedFile(file_path=file_path, filename=filename)
        speakers_csv_url = upload(speakers_csv_file,
                                  UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier=''))
        result = {
            'download_url': speakers_csv_url
        }
    except Exception as e:
        print(traceback.format_exc())
        result = {'__error': True, 'result': str(e)}

    return result
示例#9
0
def create_save_pdf(pdf_data):
    """
    Create and Saves PDFs from html
    :param pdf_data:
    :return:
    """
    filedir = current_app.config.get('BASE_DIR') + '/static/uploads/pdf/tickets/'

    if not os.path.isdir(filedir):
        os.makedirs(filedir)

    filename = get_file_name() + '.pdf'
    dest = filedir + filename

    file = open(dest, "wb")
    pisa.CreatePDF(cStringIO.StringIO(pdf_data.encode('utf-8')), file)
    file.close()

    uploaded_file = UploadedFile(dest, filename)
    upload_path = UPLOAD_PATHS['pdf']['ticket_attendee'].format(identifier=get_file_name())
    new_file = upload(uploaded_file, upload_path)
    # Removing old file created
    os.remove(dest)

    return new_file
示例#10
0
def export_speakers_csv_task(self, event_id):
    speakers = db.session.query(Speaker).filter_by(event_id=event_id)
    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'),
                               'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = "speakers-{}.csv".format(uuid.uuid1().hex)
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_speakers_csv
            content = export_speakers_csv(speakers)
            for row in content:
                writer.writerow(row)
        speakers_csv_file = UploadedFile(file_path=file_path,
                                         filename=filename)
        speakers_csv_url = upload(
            speakers_csv_file,
            UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id,
                                                       identifier=''))
        result = {'download_url': speakers_csv_url}
    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logging.error('Error in exporting speakers list as CSV')

    return result
示例#11
0
def export_group_followers_csv_task(self, group_id):

    followers = UserFollowGroup.query.filter_by(group_id=group_id).all()

    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = f"group-followers-{uuid.uuid1().hex}.csv"
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_group_followers_csv

            content = export_group_followers_csv(followers)
            for row in content:
                writer.writerow(row)
        group_followers_csv_file = UploadedFile(file_path=file_path, filename=filename)
        group_followers_csv_url = upload(
            group_followers_csv_file,
            UPLOAD_PATHS['exports-temp']['csv'].format(event_id='group', identifier=''),
        )
        result = {'download_url': group_followers_csv_url}
    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logging.exception('Error in exporting group followers as CSV')

    return result
示例#12
0
def export_attendees_csv_task(self, event_id):
    attendees = db.session.query(TicketHolder).filter_by(event_id=event_id)
    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = "attendees-{}.csv".format(uuid.uuid1().hex)
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_attendees_csv
            content = export_attendees_csv(attendees)
            for row in content:
                writer.writerow(row)
        attendees_csv_file = UploadedFile(file_path=file_path, filename=filename)
        attendees_csv_url = upload(attendees_csv_file,
                                   UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier=''))
        result = {
            'download_url': attendees_csv_url
        }
    except Exception as e:
        print(traceback.format_exc())
        result = {'__error': True, 'result': str(e)}

    return result
示例#13
0
def create_save_resized_image(image_file, basewidth=None, maintain_aspect=None, height_size=None, upload_path=None,
                              ext='jpg', remove_after_upload=False, resize=True):
    """
    Create and Save the resized version of the background image
    :param resize:
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param height_size:
    :param maintain_aspect:
    :param basewidth:
    :param image_file:
    :return:
    """
    if not image_file:
        return None
    filename = '{filename}.{ext}'.format(filename=get_file_name(), ext=ext)
    data = urllib.request.urlopen(image_file).read()
    image_file = io.BytesIO(data)
    try:
        im = Image.open(image_file)
    except IOError:
        raise IOError("Corrupt/Invalid Image")

    # Convert to jpeg for lower file size.
    if im.format is not 'JPEG':
        img = im.convert('RGB')
    else:
        img = im

    if resize:
        if maintain_aspect:
            width_percent = (basewidth / float(img.size[0]))
            height_size = int((float(img.size[1]) * float(width_percent)))

        img = img.resize((basewidth, height_size), PIL.Image.ANTIALIAS)

    temp_file_relative_path = 'static/media/temp/' + generate_hash(str(image_file)) + get_file_name() + '.jpg'
    temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path
    dir_path = temp_file_path.rsplit('/', 1)[0]

    # create dirs if not present
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)

    img.save(temp_file_path)
    upfile = UploadedFile(file_path=temp_file_path, filename=filename)

    if remove_after_upload:
        # os.remove(image_file) No point in removing in memory file
        pass

    uploaded_url = upload(upfile, upload_path)
    os.remove(temp_file_path)

    return uploaded_url
示例#14
0
def create_save_resized_image(image_file, basewidth=None, maintain_aspect=None, height_size=None, upload_path=None,
                              ext='jpg', remove_after_upload=False, resize=True):
    """
    Create and Save the resized version of the background image
    :param resize:
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param height_size:
    :param maintain_aspect:
    :param basewidth:
    :param image_file:
    :return:
    """
    if not image_file:
        return None
    filename = '{filename}.{ext}'.format(filename=get_file_name(), ext=ext)
    data = urllib.request.urlopen(image_file).read()
    image_file = io.BytesIO(data)
    try:
        im = Image.open(image_file)
    except IOError:
        raise IOError("Corrupt/Invalid Image")

    # Convert to jpeg for lower file size.
    if im.format != 'JPEG':
        img = im.convert('RGB')
    else:
        img = im

    if resize:
        if maintain_aspect:
            width_percent = (basewidth / float(img.size[0]))
            height_size = int((float(img.size[1]) * float(width_percent)))

        img = img.resize((basewidth, height_size), PIL.Image.ANTIALIAS)

    temp_file_relative_path = 'static/media/temp/' + generate_hash(str(image_file)) + get_file_name() + '.jpg'
    temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path
    dir_path = temp_file_path.rsplit('/', 1)[0]

    # create dirs if not present
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)

    img.save(temp_file_path)
    upfile = UploadedFile(file_path=temp_file_path, filename=filename)

    if remove_after_upload:
        # os.remove(image_file) No point in removing in memory file
        pass

    uploaded_url = upload(upfile, upload_path)
    os.remove(temp_file_path)

    return uploaded_url
示例#15
0
def export_speakers_csv_task(self, event_id, status='all'):

    if status not in [
        'all',
        'pending',
        'accepted',
        'confirmed',
        'rejected',
        'withdrawn',
        'canceled',
        'without_session',
    ]:
        status = 'all'

    if status == 'without_session':
        speakers = Speaker.query.filter(
            Speaker.sessions.is_(None),
            Speaker.event_id == event_id,
            Speaker.deleted_at.is_(None),
        ).all()
    elif status == 'all':
        speakers = Speaker.query.filter(
            Speaker.event_id == event_id, Speaker.deleted_at.is_(None)
        ).all()
    else:
        speakers = Speaker.query.filter(
            Speaker.sessions.any(state=status),
            Speaker.event_id == event_id,
            Speaker.deleted_at.is_(None),
        ).all()

    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = f"speakers-{uuid.uuid1().hex}.csv"
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_speakers_csv

            content = export_speakers_csv(speakers)
            for row in content:
                writer.writerow(row)
        speakers_csv_file = UploadedFile(file_path=file_path, filename=filename)
        speakers_csv_url = upload(
            speakers_csv_file,
            UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier=''),
        )
        result = {'download_url': speakers_csv_url}
    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logger.exception('Error in exporting speakers list as CSV')

    return result
示例#16
0
def export_event_json(event_id, settings):
    """
    Exports the event as a zip on the server and return its path
    """
    # make directory
    exports_dir = app.config['BASE_DIR'] + '/static/uploads/exports/'
    if not os.path.isdir(exports_dir):
        os.makedirs(exports_dir)
    dir_path = exports_dir + 'event%d' % int(event_id)
    if os.path.isdir(dir_path):
        shutil.rmtree(dir_path, ignore_errors=True)
    os.makedirs(dir_path)
    # save to directory
    for e in EXPORTS:
        if e[0] == 'event':
            query_obj = db.session.query(
                e[1]).filter(e[1].id == event_id).first()
            data = _order_json(dict(query_obj.__dict__), e)
            _download_media(data, 'event', dir_path, settings)
        else:
            query_objs = db.session.query(
                e[1]).filter(e[1].event_id == event_id).all()
            data = [
                _order_json(dict(query_obj.__dict__), e)
                for query_obj in query_objs
            ]
            for count in range(len(data)):
                data[count] = _order_json(data[count], e)
                _download_media(data[count], e[0], dir_path, settings)
        data_str = json.dumps(
            data,
            indent=4,
            ensure_ascii=False,
            default=handle_unserializable_data).encode('utf-8')
        fp = open(dir_path + '/' + e[0], 'w')
        fp.write(str(data_str, 'utf-8'))
        fp.close()
    # add meta
    data_str = json.dumps(_generate_meta(),
                          sort_keys=True,
                          indent=4,
                          ensure_ascii=False).encode('utf-8')
    fp = open(dir_path + '/meta', 'w')
    fp.write(str(data_str, 'utf-8'))
    fp.close()
    # make zip
    shutil.make_archive(dir_path, 'zip', dir_path)
    dir_path = dir_path + ".zip"

    storage_path = UPLOAD_PATHS['exports']['zip'].format(event_id=event_id)
    uploaded_file = UploadedFile(dir_path, dir_path.rsplit('/', 1)[1])
    storage_url = upload(uploaded_file, storage_path)

    return storage_url
示例#17
0
def create_system_image(image_file=None,
                        upload_path=None,
                        unique_identifier=None,
                        ext='jpg'):
    """
    Create System Images for Event Topics
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param image_file:
    :return:
    """
    # Get an unique identifier from uuid if not provided
    filename = '{filename}.{ext}'.format(filename=get_file_name(), ext=ext)
    if image_file:
        with urllib.request.urlopen(image_file) as img_data:
            image_file = io.BytesIO(img_data.read())
    else:
        file_relative_path = 'static/default_system_image.png'
        image_file = current_app.config['BASE_DIR'] + '/' + file_relative_path
    try:
        im = Image.open(image_file)
    except IOError:
        raise IOError("Corrupt/Invalid Image")

    # Convert to jpeg for lower file size.
    if im.format != 'JPEG':
        img = im.convert('RGB')
    else:
        img = im

    temp_file_relative_path = ('static/media/temp/' +
                               generate_hash(str(image_file)) +
                               get_file_name() + '.jpg')
    temp_file_path = current_app.config[
        'BASE_DIR'] + '/' + temp_file_relative_path
    dir_path = temp_file_path.rsplit('/', 1)[0]

    # create dirs if not present
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)

    img.save(temp_file_path)
    upfile = UploadedFile(file_path=temp_file_path, filename=filename)

    if not upload_path:
        upload_path = UPLOAD_PATHS['event_topic']['system_image'].format(
            event_topic_id=unique_identifier)

    uploaded_url = upload(upfile, upload_path)
    os.remove(temp_file_path)

    image = {'system_image_url': uploaded_url}
    return image
示例#18
0
def upload_image():
    image = request.json['data']
    extension = '.{}'.format(image.split(";")[0].split("/")[1])
    image_file = uploaded_image(extension=extension, file_content=image)
    force_local = request.args.get('force_local', 'false')
    if force_local == 'true':
        image_url = upload_local(
            image_file,
            UPLOAD_PATHS['temp']['image'].format(uuid=uuid.uuid4()))
    else:
        image_url = upload(
            image_file,
            UPLOAD_PATHS['temp']['image'].format(uuid=uuid.uuid4()))
    return jsonify({"url": image_url})
def export_event_json(event_id, settings):
    """
    Exports the event as a zip on the server and return its path
    """
    # make directory
    exports_dir = app.config['BASE_DIR'] + '/static/uploads/exports/'
    if not os.path.isdir(exports_dir):
        os.makedirs(exports_dir)
    dir_path = exports_dir + 'event%d' % int(event_id)
    if os.path.isdir(dir_path):
        shutil.rmtree(dir_path, ignore_errors=True)
    os.makedirs(dir_path)
    # save to directory
    for e in EXPORTS:
        if e[0] == 'event':
            query_obj = db.session.query(e[1]).filter(
                e[1].id == event_id).first()
            data = _order_json(dict(query_obj.__dict__), e)
            _download_media(data, 'event', dir_path, settings)
        else:
            query_objs = db.session.query(e[1]).filter(
                e[1].event_id == event_id).all()
            data = [_order_json(dict(query_obj.__dict__), e) for query_obj in query_objs]
            for count in range(len(data)):
                data[count] = _order_json(data[count], e)
                _download_media(data[count], e[0], dir_path, settings)
        data_str = json.dumps(data, indent=4, ensure_ascii=False, default=handle_unserializable_data).encode('utf-8')
        fp = open(dir_path + '/' + e[0], 'w')
        fp.write(str(data_str, 'utf-8'))
        fp.close()
    # add meta
    data_str = json.dumps(
        _generate_meta(), sort_keys=True,
        indent=4, ensure_ascii=False
    ).encode('utf-8')
    fp = open(dir_path + '/meta', 'w')
    fp.write(str(data_str, 'utf-8'))
    fp.close()
    # make zip
    shutil.make_archive(dir_path, 'zip', dir_path)
    dir_path = dir_path + ".zip"

    storage_path = UPLOAD_PATHS['exports']['zip'].format(
        event_id=event_id
    )
    uploaded_file = UploadedFile(dir_path, dir_path.rsplit('/', 1)[1])
    storage_url = upload(uploaded_file, storage_path)

    return storage_url
示例#20
0
def create_system_image(image_file=None, upload_path=None, unique_identifier=None,
                        ext='jpg'):
    """
    Create System Images for Event Topics
    :param upload_path:
    :param ext:
    :param remove_after_upload:
    :param image_file:
    :return:
    """
    # Get an unique identifier from uuid if not provided
    filename = '{filename}.{ext}'.format(filename=get_file_name(), ext=ext)
    if image_file:
        with urllib.request.urlopen(image_file) as img_data:
            image_file = io.BytesIO(img_data.read())
    else:
        file_relative_path = 'static/default_system_image.png'
        image_file = app.config['BASE_DIR'] + '/' + file_relative_path
    try:
        im = Image.open(image_file)
    except IOError:
        raise IOError("Corrupt/Invalid Image")

    # Convert to jpeg for lower file size.
    if im.format is not 'JPEG':
        img = im.convert('RGB')
    else:
        img = im

    temp_file_relative_path = 'static/media/temp/' + generate_hash(str(image_file)) + get_file_name() + '.jpg'
    temp_file_path = app.config['BASE_DIR'] + '/' + temp_file_relative_path
    dir_path = temp_file_path.rsplit('/', 1)[0]

    # create dirs if not present
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)

    img.save(temp_file_path)
    upfile = UploadedFile(file_path=temp_file_path, filename=filename)

    if not upload_path:
        upload_path = UPLOAD_PATHS['event_topic']['system_image'].format(event_topic_id=unique_identifier)

    uploaded_url = upload(upfile, upload_path)
    os.remove(temp_file_path)

    image = {'system_image_url': uploaded_url}
    return image
示例#21
0
def export_admin_sales_csv_task(self, status='all'):
    current_time = datetime.now(pytz.utc)
    if status not in [
        'all',
        'live',
        'past',
    ]:
        status = 'all'

    if status == 'all':
        sales = Event.query.all()
    elif status == 'live':
        sales = Event.query.filter(
            Event.starts_at <= current_time,
            Event.ends_at >= current_time,
        ).all()
    elif status == 'past':
        sales = Event.query.filter(
            Event.ends_at <= current_time,
        ).all()

    try:
        filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/')
        if not os.path.isdir(filedir):
            os.makedirs(filedir)
        filename = f"sales-{uuid.uuid1().hex}.csv"
        file_path = os.path.join(filedir, filename)

        with open(file_path, "w") as temp_file:
            writer = csv.writer(temp_file)
            from app.api.helpers.csv_jobs_util import export_sales_csv

            content = export_sales_csv(sales)
            for row in content:
                writer.writerow(row)
        sales_csv_file = UploadedFile(file_path=file_path, filename=filename)
        sales_csv_url = upload(
            sales_csv_file,
            UPLOAD_PATHS['exports-temp']['csv'].format(event_id='admin', identifier=''),
        )
        result = {'download_url': sales_csv_url}
    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logging.exception('Error in exporting sales as CSV')

    return result
示例#22
0
def create_save_pdf(
    pdf_data,
    key,
    dir_path='/static/uploads/pdf/temp/',
    identifier=get_file_name(),
    upload_dir='static/media/',
    new_renderer=False,
    extra_identifiers=None,
):
    """
    Create and Saves PDFs from html
    :param pdf_data:
    :return:
    """
    if extra_identifiers is None:
        extra_identifiers = {}
    filedir = current_app.config.get('BASE_DIR') + dir_path

    if not os.path.isdir(filedir):
        os.makedirs(filedir)

    filename = identifier + '.pdf'
    dest = filedir + filename

    pdf_content = pdf_data.encode('utf-8')
    if not new_renderer:
        file = open(dest, "wb")
        pisa.CreatePDF(io.BytesIO(pdf_content), file)
        file.close()
    else:
        HTML(string=pdf_content).write_pdf(dest)

    uploaded_file = UploadedFile(dest, filename)
    upload_path = key.format(**{'identifier': identifier, **extra_identifiers})
    new_file = upload(uploaded_file, upload_path, upload_dir=upload_dir)
    # Removing old file created
    os.remove(dest)

    return new_file
示例#23
0
def _upload_media(task_handle, event_id, base_path):
    """
    Actually uploads the resources
    """
    global UPLOAD_QUEUE
    total = len(UPLOAD_QUEUE)
    ct = 0

    for i in UPLOAD_QUEUE:
        # update progress
        ct += 1
        update_state(task_handle, 'Uploading media (%d/%d)' % (ct, total))
        # get upload infos
        name, model = i['srv']
        id_ = i['id']
        if name == 'event':
            item = db.session.query(model).filter_by(id=event_id).first()
        else:
            item = db.session.query(model).filter_by(event_id=event_id).filter_by(id=id_).first()
        # get cur file
        if i['field'] in ['original', 'large', 'thumbnail', 'small', 'icon']:
            field = '{}_image_url'.format(i['field'])
        else:
            field = '{}_url'.format(i['field'])
        path = getattr(item, field)
        if path.startswith('/'):
            # relative files
            path = base_path + path
            if os.path.isfile(path):
                filename = path.rsplit('/', 1)[1]
                file = UploadedFile(path, filename)
            else:
                file = ''  # remove current file setting
        else:
            # absolute links
            try:
                filename = UPLOAD_PATHS[name][field].rsplit('/', 1)[1]
                if is_downloadable(path):
                    r = requests.get(path, allow_redirects=True)
                    file = UploadedMemory(r.content, filename)
                else:
                    file = None
            except:
                file = None
        # don't update current file setting
        if file is None:
            continue
        # upload
        try:
            if file == '':
                raise Exception()
            key = UPLOAD_PATHS[name][field]
            if name == 'event':
                key = key.format(event_id=event_id)
            else:
                key = key.format(event_id=event_id, id=id_)
            print(key)
            new_url = upload(file, key)
        except Exception:
            print(traceback.format_exc())
            new_url = None
        setattr(item, field, new_url)
        save_to_db(item, msg='Url updated')
    # clear queue
    UPLOAD_QUEUE = []
    return
def _upload_media(task_handle, event_id, base_path):
    """
    Actually uploads the resources
    """
    global UPLOAD_QUEUE
    total = len(UPLOAD_QUEUE)
    ct = 0

    for i in UPLOAD_QUEUE:
        # update progress
        ct += 1
        update_state(task_handle, 'Uploading media (%d/%d)' % (ct, total))
        # get upload infos
        name, model = i['srv']
        id_ = i['id']
        if name == 'event':
            item = db.session.query(model).filter_by(id=event_id).first()
        else:
            item = db.session.query(model).filter_by(event_id=event_id).filter_by(id=id_).first()
        # get cur file
        if i['field'] in ['original', 'large', 'thumbnail', 'small', 'icon']:
            field = '{}_image_url'.format(i['field'])
        else:
            field = '{}_url'.format(i['field'])
        path = getattr(item, field)
        if path.startswith('/'):
            # relative files
            path = base_path + path
            if os.path.isfile(path):
                filename = path.rsplit('/', 1)[1]
                file = UploadedFile(path, filename)
            else:
                file = ''  # remove current file setting
        else:
            # absolute links
            try:
                filename = UPLOAD_PATHS[name][field].rsplit('/', 1)[1]
                if is_downloadable(path):
                    r = requests.get(path, allow_redirects=True)
                    file = UploadedMemory(r.content, filename)
                else:
                    file = None
            except:
                file = None
        # don't update current file setting
        if file is None:
            continue
        # upload
        try:
            if file == '':
                raise Exception()
            key = UPLOAD_PATHS[name][field]
            if name == 'event':
                key = key.format(event_id=event_id)
            else:
                key = key.format(event_id=event_id, id=id_)
            print(key)
            new_url = upload(file, key)
        except Exception:
            print(traceback.format_exc())
            new_url = None
        setattr(item, field, new_url)
        save_to_db(item, msg='Url updated')
    # clear queue
    UPLOAD_QUEUE = []
    return