Esempio n. 1
0
class FunfResource(MongoDBResource):

    id = fields.CharField(attribute="_id")
    key = fields.CharField(attribute="key",
                           null=True,
                           help_text='The funf probe name.')
    time = fields.DateTimeField(
        attribute="time",
        null=True,
        help_text=
        'A human readable datetime.  The time represents when funf collected the data.'
    )
    value = fields.CharField(attribute="value",
                             null=True,
                             help_text='A json blob of funf data.')

    class Meta:
        authentication = OAuth2Authentication("funf_write")
        authorization = PDSAuthorization(scope="funf_write",
                                         audit_enabled=True)
        resource_name = "funf"
        list_allowed_methods = ["delete", "get", "post"]
        object_class = Document
        collection = "funf"  # collection name
        filtering = {"key": ["exact"]}
Esempio n. 2
0
class FunfConfigResource(MongoDBResource):

    id = fields.CharField(attribute="_id")
    name = fields.CharField(attribute="name", blank=False, null=False)
    config = fields.DictField(attribute="config", blank=False, null=False)

    class Meta:
        resource_name = "funfconfig"
        list_allowed_methods = ["delete", "get", "post"]
        authentication = OAuth2Authentication("funf_write")
        authorization = PDSAuthorization(scope="funf_write",
                                         audit_enabled=True)
        object_class = Document
        collection = "funfconfig"  # collection name
Esempio n. 3
0
class MyTestResource(Resource):
    name = fields.CharField(attribute='name')

    class Meta:
        resource_name = 'test_resource'
        allow_methods = ['get']
        object_class = BaseJsonModel

    def alter_list_data_to_serialize(self, request, data_dict):
        if isinstance(data_dict, dict):
            if 'meta' in data_dict:
                # Get rid of the "meta".
                del (data_dict['meta'])
                # Rename the objects.
                # data_dict['testobjects'] = copy.copy(data_dict['objects'])
                # del (data_dict['objects'])

        return data_dict['objects'][0]

    def obj_get_list(self, bundle, **kwargs):
        return self.get_object_list(bundle.request)

    def get_object_list(self, request):
        result = {'name': 'lee'}
        new_obj = BaseJsonModel()
        new_obj.name = result['name']
        rs = [new_obj]
        return rs

    def dehydrate(self, bundle):
        bundle.data['age'] = 10
        bundle.data.pop('resource_uri')
        return bundle
Esempio n. 4
0
class ProductResource(ModelResource):
    product_id = fields.CharField(attribute='product_id')
    product_name = fields.CharField(attribute='product_name')
    product_url = fields.CharField(attribute='product_url')
    advertiser = fields.CharField(attribute='advertiser')
    designer = fields.CharField(attribute='designer')
    image_url = fields.CharField(attribute='image_url')
    price = fields.CharField(attribute='price')
    commission = fields.CharField(attribute='commission')

    class Meta:
        queryset = Product.objects.all().distinct()
        queryset = Product.objects.all()
        resource_name = 'product'
        filtering = {
            'id': ALL_WITH_RELATIONS,
            'product_id': ALL_WITH_RELATIONS, 
            'product_name': ALL_WITH_RELATIONS,
            'product_url': ALL_WITH_RELATIONS,
            'advertiser': ALL_WITH_RELATIONS,
            'designer': ALL_WITH_RELATIONS,
            'image_url': ALL_WITH_RELATIONS,
            'price': ALL_WITH_RELATIONS,
            'commission': ALL_WITH_RELATIONS
        }

    def prepend_urls(self):
        return [
        url(r"^(?P<resource_name>%s)/search%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_search'), name="api_get_search"),
        ]

    def get_search(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)
        self.throttle_check(request)

        sqs = SearchQuerySet().models(Product).load_all().auto_query(request.GET.get('keywords', ''))
        paginator = Paginator(sqs, 20)

        try:
            page = paginator.page(int(request.GET.get('page', 1)))
        except InvalidPage:
            raise Http404("Sorry, no results on that page.")

        objects = []

        for result in page.object_list:
            bundle = self.build_bundle(obj=result.object, request=request)
            bundle = self.full_dehydrate(bundle)
            objects.append(bundle)

        object_list = {
            'objects': objects,
        }

        self.log_throttled_access(request)
        return self.create_response(request, object_list)
Esempio n. 5
0
class IncidentResource(MongoDBResource):
    id = fields.CharField(attribute="_id")
    type = fields.CharField(attribute="type", null=False)
    date = fields.DateTimeField(attribute="date", null=False)
    description = fields.CharField(attribute="description", null=False)
    location = fields.DictField(attribute="location", null=False)
    user_reported = fields.BooleanField(attribute="user_reported", null=False)
    source = fields.CharField(attribute="source", null=False)

    class Meta:
        authentication = OAuth2Authentication("crowdsos_write")
        authorization = PDSAuthorization(scope="crowdsos_write",
                                         audit_enabled=False)
        resource_name = "incident"
        list_allowed_methods = ["delete", "get", "post"]
        object_class = Document
        collection = "incident"
        filtering = {"type": ["exact"]}
Esempio n. 6
0
class ShowResource(ModelResource):
    # artist = fields.ToOneField(ArtistResource, 'Artist', null=False, blank=True)  # was ForeignKey # ApiField works shouldn't use
    # venue = fields.ToOneField(VenueResource, 'Venue', null=False, blank=True)

    # this works, most likely way to go   shows id in uri:
    # artist = fields.ToOneField(attribute="artist", to=ArtistResource)  # CharField worked # This works using ToOneField
    # venue = fields.ToOneField(attribute="venue", to=VenueResource)

    # this works, shows translated foreign key value, not id
    # For updating the main App DB, getting the actual foreign key value makes the most sense in terms of work
    # to update the DB.
    artist = fields.CharField(attribute="artist")
    venue = fields.CharField(attribute="venue")

    class Meta:
        queryset = Show.objects.all()
        resource_name = 'show'
        fields = ['show_date', 'artist', 'venue']
        limit = 0
        include_resource_uri = False
Esempio n. 7
0
class AnswerResource(MongoDBResource):
    id = fields.CharField(attribute="_id",
                          help_text='A guid identifier for an answer entry.')
    key = fields.CharField(
        attribute="key",
        help_text='A unique string to identify each answer.',
        null=False,
        unique=True)
    value = fields.DictField(
        attribute="value",
        help_text='A json blob of answer data.',
        null=True,
    )

    class Meta:
        resource_name = "answer"
        list_allowed_methods = ["delete", "get", "post"]
        help_text = 'resource help text...'
        authentication = OAuth2Authentication("funf_write")
        authorization = PDSAuthorization(scope="funf_write",
                                         audit_enabled=True)
        object_class = Document
        collection = "answer"  # collection name
Esempio n. 8
0
class SeriesResource(Resource):
    id = fields.CharField(attribute='id')
    poles = fields.ListField(attribute='poles')
    available = fields.ListField(attribute='available')

    class Meta:
        object_class = StationSeries
        resource_name = 'series'
        serializer = Serializer(formats=['json'])
        limit = 200
        allowed_methods = ['get']
        filtering = {
            'id': ('exact', ),
        }

    def _client(self):
        return TempoDbClient()

    def _get_series(self, station_id=None, **kwargs):
        return self._client().get_series(station_id, **kwargs)

    def get_object_list(self, request):
        series_list = self._get_series(hours=2).items()
        res = []
        for sta_id, series in series_list:
            obj = StationSeries(initial=series)
            obj.id = sta_id
            res.append(obj)
        return res

    def obj_get_list(self, request=None, **kwargs):
        return self.get_object_list(request)

    def obj_get(self, request=None, **kwargs):
        station_id = kwargs['pk']
        series = self._get_series(station_id=station_id, hours=2)
        station_series = StationSeries(initial=series[station_id])
        station_series.id = station_id
        return station_series
Esempio n. 9
0
class EventResource(ModelResource): 
  activity = fields.ToOneField(ActivityResource, 'activity')
  task_id = fields.CharField(attribute='task_id')
   
  class Meta:
    queryset = Event.objects.all()
    resource_name = 'events'
    list_allowed_methods = ['get','post']
    detail_allowed_methods = ['get','put','post','delete']
    authentication = ApiKeyTokenAuthentication()
    authorization = Authorization()
    serializer = CustomJSONSerializer(formats=['json'])

  def apply_authorization_limits(self, request, object_list):
    '''
    Only permit access to the current user's subusers.
    '''
    return object_list.filter(activity__subuser__user=request.user)

  def obj_create(self, bundle, request=None, **kwargs):
        return super(EventResource, self).obj_create(bundle, request)
        
  def get_object_list(self, request):
        return super(EventResource, self).get_object_list(request)
Esempio n. 10
0
class SearchResource(Resource):
    KeyW = fields.CharField(attribute='KeyW')
    fileno = fields.IntegerField(attribute='fileno')
    Lu = fields.ListField(attribute='Lu')
    Cfw = fields.ListField(attribute="Cfw")
    
    class Meta:
        resource_name = 'search'
        object_class = Search
        authorization = Authorization()
        always_return_data=True

    # adapted this from ModelResource
    def get_resource_uri(self, bundle_or_obj):
        kwargs = {
            'resource_name': self._meta.resource_name,
        }

        if isinstance(bundle_or_obj, Bundle):
            kwargs['pk'] = bundle_or_obj.obj.KeyW  # pk is referenced in ModelResource
        else:
            kwargs['pk'] = bundle_or_obj.KeyW
        
        if self._meta.api_name is not None:
            kwargs['api_name'] = self._meta.api_name
        
        return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

    def get_object_list(self, request):
        # inner get of object list... this is where you'll need to
        # fetch the data from what ever data source
        return 0

    def obj_get_list(self, request=None, **kwargs):
        # outer get of object list... this calls get_object_list and
        # could be a point at which additional filtering may be applied
        return self.get_object_list(request)

    def obj_get(self, request=None, **kwargs):
        # get one object from data source
        data = {"KeyW":self.KeyW, "fileno":self.fileno, "Lu":self.Lu, "Cfw":self.Cfw}
        return data
    
    def obj_create(self, bundle, request=None, **kwargs):
        logger.info("Search in SSE Server")
        logger.debug("TA url:",URL_TA)
        
        # create a new object
        bundle.obj = Search()
         
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
         
        logger.debug("Received data from user:"******" - KeyW: ",bundle.obj.KeyW, " - file number: ", bundle.obj.fileno, " - Lu:", bundle.obj.Lu)
        
        # invoke API of TA
        fileno = bundle.obj.fileno
        KeyW = json.dumps(bundle.obj.KeyW)
        
        logger.debug("KeyW:", KeyW)
        
        data = {}
        data["KeyW"] = bundle.obj.KeyW
        data["fileno"] = bundle.obj.fileno
        
        logger.debug("json data:", data)
        logger.debug("URL_TA:",URL_TA)
        
        # Send request to TA
        logger.debug("Send request to TA")
        response = requests.post(URL_TA, json=data)  
        
        logger.debug("Response from TA: Lta = ", response.text)
        
        # compare the list received from TA with the list received from the user
        Lu = bundle.obj.Lu
        logger.debug("List from user:"******"Lta"]
        logger.debug("List from TA:", Lta)
        
        if Lu == Lta:
            logger.debug("Lu = Lta")
            # Identify list of json identifiers
            #Cfw = []
            
            # List of encrypted data
            CipherL = []

            logger.debug("fileno:",fileno)  
            for i in range(1, int(fileno) + 1): # fileno starts at 1
                logger.debug("i:", i)
                KeyW_ciphertext = json.loads(KeyW)['ct'] # get value of json
                input =  (KeyW_ciphertext + str(i) + "0").encode('utf-8')
                addr = hash(input)
                logger.debug("hash input to compute address:", input)
                logger.debug("the hash output (computed from KeyW):", addr)
                logger.debug("type of addr:",type(addr))

                try:
                    logger.debug("finding address")
                    
                    # Retrieve value which corresponds to the address 'addr'
                    cf = Map.objects.get(address=addr).value
                    logger.debug("File identifier:",cf)
                    
                    # Create list of values, which will be used to identify json-id
                    #Cfw.append(cf)
                    
                    # Retrieve ciphertexts
                    ct = CipherText.objects.filter(jsonId=cf).values()
                    logger.debug("Ciphertext of the same file:",ct)
                    CipherL.append(list(ct))
                    
                    # Delete the current (address, value) and update with the new (address, value)
                    Map.objects.get(address=addr).delete()
                    logger.debug("New address:",Lu[i-1])
                    Map.objects.create(address=Lu[i-1],value=cf) # fileno == length(Lu)
                except:
                    logger.debug("Not found:",addr)
                    cf = None
                        
#             bundle.obj.Cfw = Cfw
            bundle.obj.Cfw = CipherL
            logger.debug("The list of ciphertext:",CipherL)
           # bundle.obj.Cfw = CipherL
            bundle.obj.KeyW = '' # hide KeyW in the response
            bundle.obj.fileno = 0 # hide fileNo in the response  
            bundle.obj.Lu=[]     # hide Lu in the response  
#             logger.debug("Send list of addresses (Cfw) back to the user:"******"Send list of ciphertext (Cfw) back to the user:"******"Lu!=Lta")
        
 
        return bundle
Esempio n. 11
0
class SplitTransactionResource(ModelResource):
    total_value = fields.DecimalField(attribute='total_value',
                                      default=Decimal(0))
    installments = fields.IntegerField(attribute='installments')
    first_installment_date = fields.DateField()
    category = fields.ForeignKey(CategoryResource,
                                 'category',
                                 full=True,
                                 null=True)
    description = fields.CharField(attribute='description',
                                   null=True,
                                   blank=True)
    transactions = fields.ToManyField(
        TransactionResource,
        attribute=lambda bundle: Transaction.objects.filter(
            installment_of=bundle.obj).order_by('installment_number'),
        full=True,
        null=True)

    class Meta:
        resource_name = "split_transaction"
        queryset = SplitTransaction.objects.all()\
            .annotate(total_value=Sum('transactions__value'))\
            .annotate(installments=Count('transactions'))
        always_return_data = True
        authentication = MultiAuthentication(SessionAuthentication(),
                                             BasicAuthentication())
        authorization = UserObjectsOnlyAuthorization()
        validation = CleanedDataFormValidation(
            form_class=SplitTransactionApiForm)
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get']

    def _create_installments(self, bundle):
        """
        Creates the installments for this split transaction.
        """
        data = bundle.data

        transaction_data = {
            'value': data.get('total_value') / data.get('installments'),
            'date': data.get('first_installment_date'),
            'user': bundle.obj.user,
            'description': data.get('description'),
            'created': timezone.now(),
            'category': bundle.obj.category
        }

        transactions = []
        for i in range(0, data['installments']):
            transactions.append(
                Transaction.objects.create(installment_number=(i + 1),
                                           **transaction_data))

            transaction_data['date'] += relativedelta(months=1)

        return transactions

    def hydrate_total_value(self, bundle):
        value = bundle.data.get('total_value', None)

        if value:
            bundle.data['total_value'] = parse_decimal(
                str(value), locale=bundle.request.locale)

        return bundle

    def full_hydrate(self, bundle):
        bundle = super(SplitTransactionResource, self).full_hydrate(bundle)

        # this must happen after all hydrations because order isn't garanteed
        value = bundle.data.get('total_value')
        if value:
            # casting value to str to avoid repeating decimals
            value = Decimal(str(value)).copy_abs()

            if bundle.obj.category.is_negative:
                value = value.copy_negate()

            bundle.data['total_value'] = value

        return bundle

    def alter_detail_data_to_serialize(self, request, bundle):
        data = bundle.data
        del data['category']
        del data['first_installment_date']
        return bundle

    def alter_list_data_to_serialize(self, request, data):
        for bundle in data['objects']:
            del bundle.data['category']
            del bundle.data['first_installment_date']
        return data

    def obj_create(self, bundle, **kwargs):
        bundle = super(SplitTransactionResource,
                       self).obj_create(bundle,
                                        user=bundle.request.user,
                                        **kwargs)

        bundle.obj.transactions = self._create_installments(bundle)

        return bundle
Esempio n. 12
0
class LongLineReqResource(Resource):
    requestType = fields.CharField(attribute='requestType')
    requestLine = fields.CharField(attribute='requestLine')

    #Lno = fields.ListField(attribute='Lno',default=[]) # List of addresses, computed by TA

    class Meta:
        resource_name = 'longrequest'
        object_class = LongLineReq
        authorization = Authorization()
        always_return_data = True  # This is enabled, permitting return results for post request
        #fields = ['Lno']

    # adapted this from ModelResource
    def get_resource_uri(self, bundle_or_obj):
        kwargs = {
            'resource_name': self._meta.resource_name,
        }

        if isinstance(bundle_or_obj, Bundle):
            kwargs[
                'pk'] = bundle_or_obj.obj.requestType  # pk is referenced in ModelResource
        else:
            kwargs['pk'] = bundle_or_obj.requestType

        if self._meta.api_name is not None:
            kwargs['api_name'] = self._meta.api_name

        return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

    def get_object_list(self, request):
        # inner get of object list... this is where you'll need to
        # fetch the data from what ever data source
        return 0

    def obj_get_list(self, request=None, **kwargs):
        # outer get of object list... this calls get_object_list and
        # could be a point at which additional filtering may be applied
        return self.get_object_list(request)

    def obj_get(self, bundle, request=None, **kwargs):
        #         get one object from data source
        requestType = kwargs['pk']

        bundle_obj = LongLineReq()
        bundle_obj.requestType = requestType

        try:
            return bundle_obj
        except KeyError:
            raise NotFound("Object not found")

    def obj_create(self, bundle, request=None, **kwargs):
        logger.info("Long line request in TA Server")

        # create a new object
        bundle.obj = LongLineReq()

        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)

        requestType = bundle.obj.requestType
        requestLine = bundle.obj.requestLine
        logger.debug("Type of request:", requestType)
        logger.debug("Request content:", requestLine)

        if requestType == "fileno":
            #response = FileNo.objects.filter(Q(w="patient[age]20") | Q(w="patient[age]23"))
            logger.debug("Send internal request")
            response = requests.get("http://127.0.0.1:8080/api/v1/fileno/?w=" +
                                    requestLine)
            logger.debug("List of file no:", response)
        else:
            #response = SearchNo.objects.get(w=requestLine)
            response = requests.get("http://127.0.0.1:8080/api/v1/search/?w=" +
                                    requestLine)
            logger.debug("List of search no:", response)
        bundle.obj.requestLine = ''  # hide requestLine in the response
        bundle.obj.requestType = ''  # hide requestType in the response
        #bundle.data["result"]=response.text
        bundle.data["result"] = response
        return bundle
Esempio n. 13
0
class SearchResource(Resource):
    KeyW = fields.CharField(attribute='KeyW')
    #fileno = fields.IntegerField(attribute='fileno')
    Lta = fields.ListField(attribute='Lta',
                           default=[])  # List of addresses, computed by TA

    class Meta:
        resource_name = 'search'
        object_class = Search
        authorization = Authorization()
        always_return_data = True  # This is enabled, permitting return results for post request
        fields = ['Lta']

    # adapted this from ModelResource
    def get_resource_uri(self, bundle_or_obj):
        kwargs = {
            'resource_name': self._meta.resource_name,
        }

        if isinstance(bundle_or_obj, Bundle):
            kwargs[
                'pk'] = bundle_or_obj.obj.KeyW  # pk is referenced in ModelResource
        else:
            kwargs['pk'] = bundle_or_obj.KeyW

        if self._meta.api_name is not None:
            kwargs['api_name'] = self._meta.api_name

        return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs)

    def get_object_list(self, request):
        # inner get of object list... this is where you'll need to
        # fetch the data from what ever data source
        return 0

    def obj_get_list(self, request=None, **kwargs):
        # outer get of object list... this calls get_object_list and
        # could be a point at which additional filtering may be applied
        return self.get_object_list(request)

    def obj_get(self, bundle, request=None, **kwargs):
        #         get one object from data source
        KeyW = kwargs['pk']

        bundle_obj = Search()
        bundle_obj.KeyW = KeyW

        try:
            return bundle_obj
        except KeyError:
            raise NotFound("Object not found")

    def obj_create(self, bundle, request=None, **kwargs):
        logger.info("Search in TA Server")

        # create a new object
        bundle.obj = Search()

        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)

        KeyW = bundle.obj.KeyW
        logger.debug("Type of ciphertext:", type(KeyW))

        # Recover hash(w) and searchNo[w] from KeyW
        logger.debug("key:", KeyG)
        logger.debug("KeyW", KeyW)
        plaintext = SJCL().decrypt(KeyW, KeyG)
        logger.debug("plaintext:", plaintext)

        hashChars = int(
            hash_length /
            4)  # hash_length/4 = number of characters in hash value = 64

        plaintext_str = str(
            plaintext, 'utf-8'
        )  # convert type from byte (plaintext) to string (plaintext_str)
        hashW = plaintext_str[0:hashChars]
        logger.debug("hashW:", hashW)
        logger.debug("search no:", plaintext_str[hashChars:])
        searchNo = plaintext_str[hashChars:]

        # increase search number
        searchNo = str(int(searchNo) + 1)

        logger.debug("hashW:", hashW, "searchNo:", searchNo)

        plaintext_byte = str.encode(hashW + searchNo)
        logger.debug("new plaintext:", plaintext_byte)
        newKeyW = SJCL().encrypt(plaintext_byte, KeyG, SALT,
                                 IV)  # Compute new KeyW
        logger.debug("new ciphertext:", newKeyW)
        logger.debug("decrypted value:", SJCL().decrypt(newKeyW, KeyG))
        newKeyW_ciphertext = newKeyW[
            'ct']  # convert type from dict (newKeyW) to byte (newKeyW_byte)
        logger.debug("newKeyW_ciphertext:", newKeyW_ciphertext)

        # fileno = bundle.obj.fileno # INCORRECT:  fileno should be retrieved locally instead of from the request
        logger.debug("Retrieve fileno")
        Lta = []
        try:
            fileno = FileNo.objects.get(w=hashW).fileno
            logger.debug("fileno from the internal request:", fileno)
            # Compute all addresses with the new key
            for i in range(1,
                           int(fileno) + 1):  # file number is counted from 1
                logger.debug("i:", i)
                logger.debug("newKeyW_ciphertext:",
                             str(newKeyW_ciphertext, 'utf-8'))
                input = (str(newKeyW_ciphertext, 'utf-8') + str(i) +
                         "0").encode('utf-8')
                addr = hash(input)
                logger.debug("hash input:", input)
                logger.debug("hash output (computed from newKeyW):", addr)
                Lta.append(addr)
        except:  # not found
            fileno = 0
            logger.debug("Not found fileno")
        finally:
            bundle.obj.Lta = Lta
            bundle.obj.KeyW = ''  # hide KeyW in the response
            # bundle.obj.fileno = 0 # hide fileNo in the response
            return bundle  # return the list of computed addresses to CSP, which sends the request