def is_valid(self, bundle, request=None): from client.api import EnvironmentResource, AreaResource from coresql.models import CATEGORY_CHOICES, Annotation ## check that we have a user if not bundle.request.user or bundle.request.user.is_anonymous(): return {"__all__": "No user found in request."} if not bundle.data: return {"__all__": "No data submitted."} errors = {} if bundle.request.method.upper() == "POST": env_obj = None area_obj = None if "environment" in bundle.data: try: env_obj = EnvironmentResource().get_via_uri(bundle.data["environment"]) except: env_obj = None if "area" in bundle.data: try: area_obj = AreaResource().get_via_uri(bundle.data["area"]) except: area_obj = None if env_obj is None and area_obj is None: errors["environment"] = ["No or wrong environment uri"] errors["area"] = ["No or wrong area uri"] if not env_obj is None and not area_obj is None and area_obj.env != env_obj: errors["environment"] = ["Environment resource mismatches parent environment of area resource."] if not "data" in bundle.data or not bundle.data["data"]: errors["data"] = ["No or empty data field."] if not "category" in bundle.data or not (bundle.data["category"], bundle.data["category"]) in CATEGORY_CHOICES: errors["category"] = ["No category specified or wrong category."] ## some additional validation of the data field might also be possible if no errors up to now if not errors: for _, cls in Annotation.get_subclasses(): category = bundle.data["category"] data = bundle.data["data"] if cls.is_annotation_for(category, data): data_errors = cls.validate_data(category, data) if data_errors: errors["data"] = data_errors import sys print >>sys.stderr, data_errors break return errors
def hydrate(self, bundle): #from coresql.models import DescriptionAnnotation, EntryAnnotation, Entry """ switch after the annotation category and construct the appropriate annotation type object with the given data """ instantiated = False ## get all data from bundle.data user = bundle.request.user.get_profile() category = bundle.data['category'] data = bundle.data['data'] environment = None area = None try: environment = EnvironmentResource().get_via_uri(bundle.data['environment'], request=bundle.request) except: environment = None try: area = AreaResource().get_via_uri(bundle.data['area'], request=bundle.request) except: area = None for _, cls in Annotation.get_subclasses(): if cls.is_annotation_for(category, data): try: bundle.obj = cls(user = user, environment = environment, area = area, category = category, data = data) instantiated = True break except DuplicateAnnotationException, e: raise ImmediateHttpResponse(response=http.HttpForbidden(content=e.get_message())) except AnnotationException, ex: raise ImmediateHttpResponse(response=http.HttpBadRequest(content=ex.get_message()))
def build_filters(self, filters = None): if filters is None: filters = {} if 'environment' in filters and 'all' in filters and filters['all'] == 'true': """ if environment and all are in the filters, don't apply them any more because it will have already been handled in get_object_list """ del filters['environment'] del filters['all'] orm_filters = super(AnnotationResource, self).build_filters(filters) ## we now that there has to be a category in filters because of the validation category = filters['category'] for _, cls in Annotation.get_subclasses(): if cls.is_annotation_for(category, None): categ_specific_filters = cls.get_extra_filters(filters) orm_filters.update(categ_specific_filters) break return orm_filters
def is_valid(self, bundle, request=None): from client.api import EnvironmentResource, AreaResource from coresql.models import CATEGORY_CHOICES, Annotation ## check that we have a user if not bundle.request.user or bundle.request.user.is_anonymous(): return {'__all__': 'No user found in request.'} if not bundle.data: return {'__all__': 'No data submitted.'} errors = {} if bundle.request.method.upper() == "POST": env_obj = None area_obj = None if 'environment' in bundle.data: try: env_obj = EnvironmentResource().get_via_uri( bundle.data['environment']) except: env_obj = None if 'area' in bundle.data: try: area_obj = AreaResource().get_via_uri(bundle.data['area']) except: area_obj = None if env_obj is None and area_obj is None: errors['environment'] = ['No or wrong environment uri'] errors['area'] = ['No or wrong area uri'] if not env_obj is None and not area_obj is None and area_obj.env != env_obj: errors['environment'] = [ "Environment resource mismatches parent environment of area resource." ] if not 'data' in bundle.data or not bundle.data['data']: errors['data'] = ["No or empty data field."] if not 'category' in bundle.data or not ( bundle.data['category'], bundle.data['category']) in CATEGORY_CHOICES: errors['category'] = ["No category specified or wrong category."] ## some additional validation of the data field might also be possible if no errors up to now if not errors: for _, cls in Annotation.get_subclasses(): category = bundle.data['category'] data = bundle.data['data'] if cls.is_annotation_for(category, data): data_errors = cls.validate_data(category, data) if data_errors: errors['data'] = data_errors import sys print >> sys.stderr, data_errors break return errors