Example #1
0
def update_version(event_id, is_created, column_to_increment):
    """Function responsible for increasing version when some data will be
    created or changed
    :param event_id: Event id
    :param is_created: Object exist True/False
    :param column_to_increment: which column should be increment
    """
    VersionUpdater(event_id=event_id,
                   is_created=is_created,
                   column_to_increment=column_to_increment).update()
Example #2
0
def import_event_json(task_handle, zip_path):
    """
    Imports and creates event from json zip
    """
    global CUR_ID, UPLOAD_QUEUE
    UPLOAD_QUEUE = []
    update_state(task_handle, 'Started')

    with app.app_context():
        path = app.config['BASE_DIR'] + '/static/uploads/import_event'
    # delete existing files
    if os.path.isdir(path):
        shutil.rmtree(path, ignore_errors=True)
    # extract files from zip
    with zipfile.ZipFile(zip_path, "r") as z:
        z.extractall(path)
    # create event
    try:
        update_state(task_handle, 'Importing event core')
        data = json.loads(open(path + '/event', 'r').read())
        _, data = _trim_id(data)
        srv = ('event', EventDAO)
        data = _delete_fields(srv, data)
        new_event = EventDAO.create(data, 'dont')[0]
        version_data = data.get('version', {})
        write_file(path + '/social_links',
                   json.dumps(data.get('social_links',
                                       [])))  # save social_links
        _upload_media_queue(srv, new_event)
    except BaseError as e:
        raise make_error('event', er=e)
    except Exception as e:
        raise make_error('event', er=e)
    # create other services
    try:
        service_ids = {}
        for item in IMPORT_SERIES:
            item[1].is_importing = True
            data = open(path + '/%s' % item[0], 'r').read()
            dic = json.loads(data)
            changed_ids = create_service_from_json(task_handle, dic, item,
                                                   new_event.id, service_ids)
            service_ids[item[0]] = changed_ids.copy()
            CUR_ID = None
            item[1].is_importing = False
    except BaseError as e:
        EventDAO.delete(new_event.id)
        raise make_error(item[0], er=e, id_=CUR_ID)
    except IOError:
        EventDAO.delete(new_event.id)
        raise NotFoundError('File %s missing in event zip' % item[0])
    except ValueError:
        EventDAO.delete(new_event.id)
        raise make_error(item[0], er=ServerError('Invalid json'))
    except Exception:
        print traceback.format_exc()
        EventDAO.delete(new_event.id)
        raise make_error(item[0], id_=CUR_ID)
    # run uploads
    _upload_media(task_handle, new_event.id, path)
    # set version
    VersionUpdater(False, new_event.id, '').set(version_data)
    # return
    return new_event