# Path: smart/models/ontology_url_patterns.py
# class CallMapper(object):
#     __mapper_registry = set()
# 
#     @classmethod
#     def map_call(cls, call):
#         potential_maps = {}
#         for m_class in cls.__mapper_registry:
#             m = m_class(call)  # instantiate the mapper
#             precedence = m.map_score
#             if precedence > 0:
#                 potential_maps[m] = precedence
# 
#         in_order = sorted(
#             potential_maps.keys(),
#             key=lambda x: potential_maps[x]
#         )
#         #print call.client_method_name, in_order
#         return in_order[-1]
# 
#     @classmethod
#     def register(cls, *new_registrant, **options):
#         if new_registrant:
#             cls.__mapper_registry.add(new_registrant[0])
#             return new_registrant
# 
#         http_method = options.pop('method', None)
#         category = options.pop('category', None)
#         cardinality = options.pop('cardinality', None)
#         client_method_name = options.pop('client_method_name', None)
#         target = options.pop('target', None)
#         filter_func = options.pop('filter_func', None)
#         path = options.pop('path', None)
# 
#         assert http_method or category or cardinality or client_method_name or target or path, "Can't define a call mapping with no parameters"
# 
#         def ret(single_func):
#             class SingleMethodMatcher(BasicCallMapper):
#                 @property
#                 def maps_p(self):
#                     return  ((not http_method or str(self.call.htt_method) == http_method) and
#                              (not category or str(self.call.category) == category) and
#                              (not target or str(self.call.target) == target) and
#                              (not path or str(self.call.path) == path) and
#                              (not client_method_name or str(self.call.client_method_name) == client_method_name) and
#                              (not filter_func or filter_func(self.call)))
#                 maps_to = staticmethod(single_func)
#             cls.__mapper_registry.add(SingleMethodMatcher)
#             return single_func
#         return ret


from smart.models import *
from base import utils
from django.http import HttpResponse
from smart.models.ontology_url_patterns import CallMapper
@CallMapper.register(client_method_name="get_user_preferences")
def preferences_get (request, user_id, pha_email):   
    p = fetch_preferences (user_id, pha_email)
    return HttpResponse(p.data, mimetype=p.mime)

@CallMapper.register(client_method_name="delete_user_preferences")
def preferences_delete(request, user_id, pha_email): 
    p = fetch_preferences (user_id, pha_email)
    p.delete()
    return HttpResponse("ok")
    
@CallMapper.register(client_method_name="put_scratchpad_data")
def scratchpad_put (request, record_id, pha_email):
    try:
        ct = utils.get_content_type(request).lower().split(';')[0]
        if (not ct) or len(ct) == 0 or ct == "none": ct = "text/plain"
    except:
        ct = "text/plain"

    p = fetch_scratchpad (record_id, pha_email)
    p.data = request.raw_post_data
    p.mime = ct
    p.save()
    return HttpResponse(p.data, mimetype=p.mime)

@CallMapper.register(client_method_name="get_scratchpad_data")
def scratchpad_get (request, record_id, pha_email):   
    p = fetch_scratchpad (record_id, pha_email)
    return HttpResponse(p.data, mimetype=p.mime)


# Path: smart/models/ontology_url_patterns.py
# class CallMapper(object):
#     __mapper_registry = set()
# 
#     @classmethod
#     def map_call(cls, call):
#         potential_maps = {}
#         for m_class in cls.__mapper_registry:
#             m = m_class(call)  # instantiate the mapper
#             precedence = m.map_score
#             if precedence > 0:
#                 potential_maps[m] = precedence
# 
#         in_order = sorted(
#             potential_maps.keys(),
#             key=lambda x: potential_maps[x]
#         )
#         #print call.client_method_name, in_order
#         return in_order[-1]
# 
#     @classmethod
#     def register(cls, *new_registrant, **options):
#         if new_registrant:
#             cls.__mapper_registry.add(new_registrant[0])
#             return new_registrant
# 
#         http_method = options.pop('method', None)
#         category = options.pop('category', None)
#         cardinality = options.pop('cardinality', None)
#         client_method_name = options.pop('client_method_name', None)
#         target = options.pop('target', None)
#         filter_func = options.pop('filter_func', None)
#         path = options.pop('path', None)
# 
#         assert http_method or category or cardinality or client_method_name or target or path, "Can't define a call mapping with no parameters"
# 
#         def ret(single_func):
#             class SingleMethodMatcher(BasicCallMapper):
#                 @property
#                 def maps_p(self):
#                     return  ((not http_method or str(self.call.htt_method) == http_method) and
#                              (not category or str(self.call.category) == category) and
#                              (not target or str(self.call.target) == target) and
#                              (not path or str(self.call.path) == path) and
#                              (not client_method_name or str(self.call.client_method_name) == client_method_name) and
#                              (not filter_func or filter_func(self.call)))
#                 maps_to = staticmethod(single_func)
#             cls.__mapper_registry.add(SingleMethodMatcher)
#             return single_func
#         return ret


# Path: smart/views/data_store.py
from smart.models import *
from base import utils
from django.http import HttpResponse
from smart.models.ontology_url_patterns import CallMapper

@CallMapper.register(client_method_name="get_user_preferences")
def preferences_get (request, user_id, pha_email):   
    p = fetch_preferences (user_id, pha_email)
    return HttpResponse(p.data, mimetype=p.mime)

@CallMapper.register(client_method_name="delete_user_preferences")
def preferences_delete(request, user_id, pha_email): 
    p = fetch_preferences (user_id, pha_email)
    p.delete()
    return HttpResponse("ok")
    
@CallMapper.register(client_method_name="put_scratchpad_data")
def scratchpad_put (request, record_id, pha_email):
    try:
        ct = utils.get_content_type(request).lower().split(';')[0]
        if (not ct) or len(ct) == 0 or ct == "none": ct = "text/plain"
    except:
        ct = "text/plain"

    p = fetch_scratchpad (record_id, pha_email)
    p.data = request.raw_post_data
    p.mime = ct
    p.save()
    return HttpResponse(p.data, mimetype=p.mime)

@CallMapper.register(client_method_name="get_scratchpad_data")
def scratchpad_get (request, record_id, pha_email):   
    p = fetch_scratchpad (record_id, pha_email)
    return HttpResponse(p.data, mimetype=p.mime)


@CallMapper.register(client_method_name="delete_scratchpad_data")