Beispiel #1
0
def export_single(request):
    key = request.META.get('HTTP_AUTHORIZATION', None)
    id = request.GET.get('id', None)
    if key is None:
        raise AuthenticationError('No API key specified!')
    try:
        api_key = ApiKey.objects.get(key=key)
    except ApiKey.DoesNotExist:
        raise AuthenticationError('Invalid API key specified!')
    try:
        api_key.scopes.get(id=SCOPE_EXPORT)
    except Scope.DoesNotExist:
        raise AuthenticationError('Invalid API key scope!')
    if id is None:
        raise ApiException('Missing id!', status=400)
    try:
        badge = Badge.objects.get(id=id)
    except Badge.DoesNotExist:
        raise ApiException('Invalid id!', status=400)
    return ApiResponse(dict(
        id=badge.id,
        name=badge.name,
        mac=badge.mac,
        image=badge.render().decode('ascii'),
    ))
Beispiel #2
0
def post_send(request):
    content = request.JSON.get('content', None)
    if content is None:
        raise ApiException('No message set!', 400)
    try:
        content = content[:255]
    except ValueError:
        raise ApiException('Invalid message set!', 400)
    sender = utils.get_badge(request)
    Post.objects.create(sender=sender, content=content)
    return ApiResponse(status=204)
Beispiel #3
0
def settings_get(request):
    key = request.POST.get('key', None)
    if key is None:
        raise ApiException('No key set!', 404)
    badge = utils.get_badge(request)
    try:
        setting = Setting.objects.get(badge=badge, key=key)
    except Setting.DoesNotExist:
        raise ApiException('Invalid key set!', 400)
    return ApiResponse({
        key: setting.value
    })
Beispiel #4
0
def token_submit(request):
    token = request.JSON.get('token', None)
    if token is None:
        raise ApiException('No token sent!', 400)
    logger.debug(token)
    badge = utils.get_badge(request)
    errors = []
    try:
        result = requests.post('https://con.troopers.de/token/submit', data={
            'identifier': badge.id,
            'token': token,
        })
        logger.debug(result.status_code)
        result.raise_for_status()
        data = result.json()
        logger.debug(json.dumps(data))
        errors.extend(data.get('errors', []))
    except Exception as e:
        logger.exception(e)
        errors.extend([
            'Internal error',
        ])
    logger.debug(json.dumps(errors))
    return ApiResponse({
        'success': len(errors) is 0,
        'errors': errors
    })
Beispiel #5
0
def name(request):
    new_name = request.JSON.get('name', None)
    if new_name is None:
        raise ApiException('No name sent!', 400)
    badge = utils.get_badge(request)
    badge.name = new_name
    badge.save()
    return ApiResponse(status=204)
Beispiel #6
0
def message_send(request):
    receiver_id = request.JSON.get('receiver', None)
    if receiver_id is None:
        raise ApiException('No receiver set!', 400)
    try:
        receiver = Badge.objects.get(id=receiver_id)
    except Badge.DoesNotExist:
        raise ApiException('Invalid receiver set!', 404)
    message = request.JSON.get('message', None)
    if message is None:
        raise ApiException('No message set!', 400)
    try:
        message = message[:255]
    except ValueError:
        raise ApiException('Invalid message set!', 400)
    sender = utils.get_badge(request)
    Message.objects.create(sender=sender, receiver=receiver, message=message)
    return ApiResponse(status=204)
Beispiel #7
0
 def __call__(self, request):
     request.JSON = {}
     if request.content_type in ['application/json', 'text/json']\
             and request.body is not b''\
             and request.method in ['POST']:
         try:
             request.JSON = json.loads(request.body)
         except ValueError as ve:
             return ApiException(ve.msg, 400).response
     return self.get_response(request)
Beispiel #8
0
def vote_send(request):
    talk_id = request.JSON.get('talk', None)
    if talk_id is None:
        raise ApiException('No talk set!', 400)
    try:
        talk = Talk.objects.get(id=talk_id)
    except Talk.DoesNotExist:
        raise ApiException('Invalid talk set!', 404)
    rating = request.JSON.get('rating', None)
    if rating is None:
        raise ApiException('No rating set!', 400)
    try:
        rating = max(0, min(5, int(rating)))
    except ValueError:
        raise ApiException('Invalid rating set!', 404)
    badge = utils.get_badge(request)
    Vote.objects.update_or_create(badge=badge, talk=talk, defaults=dict(
        rating=rating,
    ))
    return ApiResponse(status=204)
Beispiel #9
0
def image(request):
    badge = utils.get_badge(request)
    data = request.JSON.get('image', None)
    if data is None:
        return ApiResponse(dict(
            image=list(badge.image),
        ))
    try:
        badge.image = data
    except ValueError:
        raise ApiException('Invalid image sent!', 400)
    badge.save()
    return ApiResponse(status=204)