<commit_before>'''
Created on Feb 29, 2012

:Authors: <NAME>
:Version: 2.0
'''
import logging
import cjson

from django.contrib.auth import authenticate

from piston.handler import BaseHandler
from piston.resource import Resource

from mds.api import do_authenticate, LOGGER
from mds.api.handlers import DispatchingHandler
from mds.api.decorators import logged, validate
from mds.api.docs.utils import handler_uri_templates
from mds.api.responses import succeed, fail, error
from mds.api.signals import EventSignal, EventSignalHandler

from .forms import *
from .models import *

__all__ = ['ConceptHandler', 
           'RelationshipHandler',
           'RelationshipCategoryHandler',
           'DeviceHandler', 
           'EncounterHandler',
           'EventHandler',
           'LocationHandler',
           'NotificationHandler', 
           'ObservationHandler', 
           'ObserverHandler',
           'ProcedureHandler',
           'DocHandler' ,
           'SessionHandler',
           'SubjectHandler',
           'LocationHandler',]

   
@logged     
class SessionHandler(DispatchingHandler):
    """ Handles session auth requests. """
    allowed_methods = ('GET','POST',)
    signals = { LOGGER:( EventSignal(), EventSignalHandler(Event))}
    form = SessionForm
    #model = None
    
    def create(self,request):
        #data = self.flatten_dict(request.POST)
        '''
        is_json = request.META.get('CONTENT_TYPE', False)
        if is_json and is_json == 'application/json':
            #data = cjson.decode(request.body)
            username = data.get('username', None)
            password = data.get('password', None)
        else:
            username = request.REQUEST.get('username', 'empty')
            password = request.REQUEST.get('password','empty')
        '''
        #data = self.flatten_dict(request.POST)
        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:
                logging.debug("is_json = True")
                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','<PASSWORD>')
            user = authenticate(username=username, password=password)
            if user is not None:
                observer = Observer.objects.get(user=user)
                return succeed(observer.uuid)
            else:
                logging.warn(msg)
                return fail(msg)
        except Exception as e:
            msg = "Internal Server Error"
            logging.error(unicode(e))
            return error(msg)
        
    def read(self,request):
        success,msg = do_authenticate(request)
        if success:
            return succeed(msg)
        else:
            return fail(msg)
    
@logged
class ConceptHandler(DispatchingHandler):
    """ Handles concept requests. """
    allowed_methods = ('GET', 'POST','PUT')
    model = Concept
    form = ConceptForm
    #fields = ("uuid", "name")
    signals = { LOGGER:( EventSignal(), EventSignalHandler(Event))}

class RelationshipHandler(DispatchingHandler):
    """ Handles concept relationship requests. """
    allowed_methods = ('GET', 'POST','PUT')
    model = Relationship
    form = RelationshipForm<commit_msg>Bug fix<commit_after>'''
Created on Feb 29, 2012

:Authors: <NAME>
:Version: 2.0
'''
import logging
import cjson

from django.contrib.auth import authenticate

from piston.handler import BaseHandler
from piston.resource import Resource

from mds.api import do_authenticate, LOGGER
from mds.api.handlers import DispatchingHandler
from mds.api.decorators import logged, validate
from mds.api.docs.utils import handler_uri_templates
from mds.api.responses import succeed, fail, error
from mds.api.signals import EventSignal, EventSignalHandler

from .forms import *
from .models import *

__all__ = ['ConceptHandler', 
           'RelationshipHandler',
           'RelationshipCategoryHandler',
           'DeviceHandler', 
           'EncounterHandler',
           'EventHandler',
           'LocationHandler',
           'NotificationHandler', 
           'ObservationHandler', 
           'ObserverHandler',
           'ProcedureHandler',
           'DocHandler' ,
           'SessionHandler',
           'SubjectHandler',
           'LocationHandler',]

   
@logged     
class SessionHandler(DispatchingHandler):
    """ Handles session auth requests. """
    allowed_methods = ('GET','POST',)
    signals = { LOGGER:( EventSignal(), EventSignalHandler(Event))}
    form = SessionForm
    #model = None
    
    def create(self,request):
        #data = self.flatten_dict(request.POST)
        '''
        is_json = request.META.get('CONTENT_TYPE', False)
        if is_json and is_json == 'application/json':
            #data = cjson.decode(request.body)
            username = data.get('username', None)
            password = data.get('password', None)
        else:
            username = request.REQUEST.get('username', 'empty')
            password = <PASSWORD>.REQUEST.get('password','<PASSWORD>')
        '''
        #data = self.flatten_dict(request.POST)
        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:
                logging.debug("is_json = True")
                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','<PASSWORD>')
            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))
            return error(msg)
        
    def read(self,request):
        success,msg = do_authenticate(request)
        if success:
            return succeed(msg)
        else:
            return fail(msg)
    
@logged
class ConceptHandler(DispatchingHandler):
    """ Handles concept requests. """
    allowed_methods = ('GET', 'POST','PUT')
    model = Concept
    form = ConceptForm
    #fields = ("uuid", "name")
    signals = { LOGGER:( EventSignal(), EventSignalHandler(Event))}

class RelationshipHandler(DispatchingHandler):
    """ Handles concept relationship requests. """
    allowed_methods = ('GET', 'POST','PUT')
    model = Relationship
    form = RelationshipForm
{'fraction_non_alphanumeric': 0.10826493065383527, 'fraction_numerical': 0.0022643645626945937, 'mean_word_length': 2.299253034547152, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253057', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1866, 'n_words': 445}