Пример #1
0
class NotificationHandler(DispatchingHandler):
    """ Handles notification requests. """
    allowed_methods = ('GET', 'POST', 'PUT')
    model = Notification
    form = NotificationForm
    signals = {LOGGER: (EventSignal(), EventSignalHandler(Event))}
Пример #2
0
class SessionHandler(DispatchingHandler):
    """ Handles session auth requests. """
    allowed_methods = (
        'GET',
        'POST',
    )
    signals = {LOGGER: (EventSignal(), EventSignalHandler(Event))}
    form = SessionForm

    #model = None

    def create(self, request):
        try:
            content_type = request.META.get('CONTENT_TYPE', None)
            logging.debug(content_type)
            is_json = 'json' in content_type
            logging.debug("is_json: %s" % is_json)
            if is_json:
                raw_data = request.read()
                data = cjson.decode(raw_data)
            else:
                data = self.flatten_dict(request.POST)

            username = data.get('username', 'empty')
            password = data.get('password', 'empty')
            if not settings.TARGET == 'SELF':
                instance = User(username=username)
                auth = {'username': username, 'password': password}
                result = backends.create('Session', auth, instance)
                if not result:
                    return fail([],
                                errors=[
                                    "Observer does not exist",
                                ],
                                code=404)
                # Create a user or fetch existin and update password
                user, created = User.objects.get_or_create(
                    username=result.user.username)
                user.set_password(password)
                user.save()

                # should have returned an Observer instance here
                observers = Observer.objects.filter(
                    user__username=user.username)
                # If none were returned we need to create the Observer
                if observers.count() == 0:
                    observer = Observer(user=user, uuid=result.uuid)
                    observer.save()
                else:
                    # Observer already exists so we don't have to do
                    # anything since password cache is updated
                    observer = observers[0]
                return succeed(observer.uuid)
            else:
                user = authenticate(username=username, password=password)
                if user is not None:
                    observer = Observer.objects.get(user=user)
                    return succeed(observer.uuid)
                else:
                    msg = "Invalid credentials"
                    logging.warn(msg)
                    return fail(msg)
        except Exception as e:
            msg = "Internal Server Error"
            logging.error(unicode(e))
            logtb()
            return error(msg)

    def read(self, request):
        success, msg = do_authenticate(request)
        if success:
            return succeed(msg)
        else:
            return fail(msg)
Пример #3
0
class RelationshipCategoryHandler(DispatchingHandler):
    """ Handles concept relationship category requests. """
    allowed_methods = ('GET', 'POST', 'PUT')
    model = RelationshipCategory
    form = RelationshipCategoryForm
    signals = {LOGGER: (EventSignal(), EventSignalHandler(Event))}
Пример #4
0
class ConceptHandler(DispatchingHandler):
    """ Handles concept requests. """
    allowed_methods = ('GET', 'POST', 'PUT')
    model = Concept
    form = ConceptForm
    signals = {LOGGER: (EventSignal(), EventSignalHandler(Event))}
Пример #5
0
''' Dispatch signals to send messages with.

:author: Sana Development Team
:version: 2.0
'''
from mds.api.signals import EventSignal
from mds.core.models import Event


def event_signalhandler(sender, **kwargs):
    data = sender.get('event')
    obj = Event(**data)
    obj.save()


done_logging = EventSignal()
class SavedProcedureHandler(BaseHandler):
    """ Handles encounter requests. """
    allowed_methods = ('POST', )
    signals = {LOGGER: (EventSignal(), EventSignalHandler(RequestLog))}

    def create(self, request, *args, **kwargs):
        logging.info("Received saved procedure submission.")
        response = ''
        form = ProcedureSubmitForm(self.flatten_dict(request.POST))
        logging.debug("Data: %s" % form.data)
        try:
            form.full_clean()
            if not form.is_valid():
                raise ValidationError(form._get_errors())

            savedproc_guid = form.cleaned_data['savedproc_guid']
            procedure_guid = form.cleaned_data['procedure_guid']
            responses = form.cleaned_data['responses']
            phone = form.cleaned_data['phone']
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            result, message = register_saved_procedure(savedproc_guid,
                                                       procedure_guid,
                                                       responses, phone,
                                                       username, password)

            encounter, data, created = spform_to_encounter(form.cleaned_data)
            encounter.save()
            logging.debug("Saved encounter: " + encounter.uuid)
            observations = responses_to_observations(encounter,
                                                     data,
                                                     sort=True)

            for obs in observations:
                obs.save()

                if obs.is_complex:
                    obs.create_file()

#result, message = True, encounter

            if result:
                response = succeed("Successfully saved the procedure: %s" %
                                   message)
                logging.info("Saved procedure successfully registered.")
            else:
                response = fail(message)
                logging.error("Failed to register procedure: %s" % message)
        except ValidationError, e:
            for k, v in form._get_errors().items():
                logging.error("SavedProcedure argument %s:%s" % (k, v))
            response = fail("Invalid ProcedureSubmitForm data")
            raise Exception('Saved procedure submission was invalid')

        except Exception, e:
            et, val, tb = sys.exc_info()
            trace = traceback.format_tb(tb)
            error = "Exception : %s %s %s" % (et, val, trace[0])
            for tbm in trace:
                logging.error(tbm)
            response = fail(error)
class Base64PacketHandler(BaseHandler):
    allowed_methods = ('POST', )
    signals = {LOGGER: (EventSignal(), EventSignalHandler(RequestLog))}

    def create(self, request, *args, **kwargs):
        return binarychunk_hack_submit(request)