Exemplo n.º 1
0
def gen_default_client():
    """
    Generates the default client.
    :return: The default client.
    """
    default_client = Clients()
    default_client.id = 0
    default_client.name = "default"
    return default_client
Exemplo n.º 2
0
def save_or_update_station(sp, request, errors, station_instance=None, client=None, parent_station=None):
    """
    Saves or updates a station based on the request form values.

    :param sp: The service provider.
    :param request: The fask request.
    :param errors: The error list instance.
    :param station_instance: The station instance if any. If this is a new station leave this paramter to None.
    :param client: The client instance if this is an override for a client. If this is a new station leave this
        parameter to None.
    :param parent_station: The parent station of this station if this is a
    :return: the save/updated/same station instance and the errors list.
    """
    if not client:
        client = Clients()
        client.id = 0
        client.name = "default"

    is_override = "True" == request.form.get("is_client_override_" + str(client.id))
    if not is_override and client.id != 0:
        return station_instance, errors

    if not station_instance:
        station_instance = Station(int(request.form.get('ebuio_orgapk')))
    station_instance.service_provider_id = sp.id

    for field in station_fields:
        field_value = request.form.get(field + '_' + str(client.id))

        if parent_station and field_value == parent_station[field]:
            field_value = None
        setattr(station_instance, field, field_value)

    if not is_override and station_instance.name == "":
        errors.append("Please set a name")
        return station_instance, errors

    # handle special fields
    station_instance.radiovis_enabled = 'radiovis_enabled_' + str(client.id) in request.form
    station_instance.radioepg_enabled = station_instance.radiospi_enabled = 'radioepg_enabled_' + str(
        client.id) in request.form
    station_instance.radiotag_enabled = 'radiotag_enabled_' + str(client.id) in request.form
    station_instance.radioepgpi_enabled = 'radioepgpi_enabled_' + str(client.id) in request.form

    # handle genres
    clientid = str(client.id) if is_override else '0'
    genre_href = map(lambda key: request.form[key],
                     filter(lambda key: key.startswith('genrehref[]_' + clientid + "_") and request.form[key] != '',
                            request.form.keys()))
    genre_href = sorted(genre_href)
    genres = []

    for h in genre_href:
        genre_id = h.split(':')[-1]
        if genre_id != '':
            genres.append({'href': h, 'name': genres_map[genre_id]})

    station_instance.genres = json.dumps(genres)

    if is_override:
        station_instance.parent = parent_station.id
        station_instance.fk_client = client.id

    # save
    if (is_override or client.id == 0) and not station_instance.id:
        station_instance.gen_random_password()
        db.session.add(station_instance)

    return station_instance, errors