Exemple #1
0
class Channel(flask_restful.Resource):
    @validate_client()
    @validate_id()
    def get(self, client, id=None):
        if id:
            response = {'channel': models.Channel.get(id, client.id)}
            return marshal(response, one_channel_fields)
        else:
            response = {'channels': models.Channel.all(client.id), 'meta': {}}
            return marshal(response, channels_fields)

    @validate_client(True)
    def post(self, client):
        json = request.get_json(silent=True)
        logging.getLogger(__name__).debug('Post channel: %s', json)
        try:
            validate(json, channel_input_format)
        except ValidationError, e:
            logging.debug(str(e))
            # TODO: generate good error messages
            return marshal({'error': {
                'message': utils.parse_error(e)
            }}, error_fields), 400

        channel = models.Channel()
        mapper.fill_from_json(channel, json, mapper.channel_mapping)
        channel.client = client
        try:
            db_helper.manage_channel_types(channel, json["types"])
        except exceptions.InvalidJson, e:
            return marshal({'error': {
                'message': utils.parse_error(e)
            }}, error_fields), 400
Exemple #2
0
def create_channel_from_json(json):
    channel = models.Channel()
    channel.id = json['id']
    mapper.fill_from_json(channel, json, mapper.channel_mapping)
    channel.created_at = get_datetime_from_json_attr(json, 'created_at')
    channel.updated_at = get_datetime_from_json_attr(json, 'updated_at')
    channel.channel_types = create_channel_types_from_json(json)

    return channel
Exemple #3
0
def test_name_channel():
    channel = models.Channel()
    eq_(str(channel), "<Channel '{}'>".format(channel.id))