Beispiel #1
0
    def roll(self, **kwargs):
        '''
        Main router for the app. Passes a randomizer's parameters as JSON
        and triggers a result.
        '''
        if 'session_id' in kwargs:
            roll_session = RollSession.objects.get(
                session_uuid=kwargs['session_id']).id
        query = QueryDict('', mutable=True)
        query.update(kwargs['parameters'])
        json_args = json.dumps(kwargs['parameters'])

        try:
            rand = Randomizer.factory(query)
        except RandomizerError as error:
            self.send(HttpResponseServerError(error))
            return

        result = rand.get_new_result()
        new_roll = RollHistory.objects.create(session_id=roll_session,
                                              roll_input=json_args,
                                              roll_output=json.dumps(result),
                                              roll_date=now())
        response = RollHistorySerializer(instance=new_roll).serialize()
        self.publish(self.get_subscription_channels(),
                     {'message': response,
                      'type': 'single'})
Beispiel #2
0
def get_new_result(request):
    '''
    Generate a new result from a randomizer and return it
    '''
    json_param = request.GET.get('rand_parameters')
    if json_param is None:
        return HttpResponseServerError()

    try:
        dict_param = json.loads(json_param)
    except ValueError as error:
        return HttpResponseServerError(error)

    try:
        content = Randomizer.factory(dict_param).get_new_result()
    except RandomizerError as error:
        return HttpResponseServerError(error)

    response = response_factory(request.GET.get('response_type', ''))(content)

    return response