Esempio n. 1
0
    def post_doc_data(self, bundle):

        # get the basic information needed from the request
        # and add the epoch time to the title of the document
        user_id, GET_data = bundle.request.user.id, bundle.data
        post_data, doc_title, file_type = \
            GET_data['docfile'], GET_data['doc_title'] \
            + '-' + str(int(time.time())), GET_data['file_type']

        # this handles the fact that test posts are different from
        # application posts. Need to investigate.
        if post_data == 'data:' or len(post_data) == 0:
            raise RhizomeApiException(
                message='file is empty please check the upload and try again')

        # when posting from ODK cronjob, we dont add the file_meta. but we do
        # from the webapp.  Look into changing so the requests are consistent
        try:
            file_meta, base64data = post_data.split(',')
        except ValueError:
            base64data = post_data

        file_header = None
        file_content = None

        if '.csv' in doc_title:
            new_file_path = None
            file_content = ContentFile(base64.b64decode(base64data))
            file_header = file_content.readline()
        elif '.xlsx' in doc_title or '.xls' in doc_title:
            # workaround - need to create the xls file in order to read from it
            new_file_path = settings.MEDIA_ROOT + doc_title
            new_file = open(new_file_path, 'w')
            new_file.write(base64.b64decode(base64data))
            new_file.close()
            the_file = open(new_file_path)
            try:
                file_df = read_excel(the_file)
            except Exception:
                os.remove(new_file_path)
                raise RhizomeApiException(
                    message='There was an error with your file. Please check \\\
                        the upload and try again')
            file_content = ContentFile(file_df.to_csv())
            file_header = file_content.readline()
        else:
            RhizomeApiException(
                message='Please upload either xls, xlsx or csv file formats')

        sd = Document.objects.create(
            **{
                'doc_title': doc_title,
                'file_type': file_type,
                'created_by_id': user_id,
                'file_header': file_header
            })
        sd.docfile.save(sd.guid, file_content)

        return sd
Esempio n. 2
0
    def get_time_group_series(self, dp_df):

        if dp_df['data_date'][0] == None:
            raise RhizomeApiException(
                'This is a campaign (not date) indicator')

        if self.time_gb == 'year':
            dp_df['time_grouping'] = dp_df['data_date'].map(
                lambda x: int(x.year))
        elif self.time_gb == 'quarter':
            dp_df['time_grouping'] = dp_df['data_date']\
                .map(lambda x: str(x.year) + str((x.month - 1) // 3 + 1))
        elif self.time_gb == 'all_time':
            dp_df['time_grouping'] = 1
        else:
            dp_df = DataFrame()

        ## find the unique possible groupings for this time range and gb param
        ## sketchy -- this wont work for quarter groupingings, only years.
        self.distinct_time_groupings = list(dp_df.time_grouping.unique())
        if not self.distinct_time_groupings:
            start_yr, end_yr = self.start_date[0:4],\
                self.end_date[0:4]
            self.distinct_time_groupings = range(int(start_yr), int(end_yr))

        return dp_df
Esempio n. 3
0
 def pre_process_data(self, request):
     '''
     Run the aggrefresh for the requested Campaign
     '''
     campaign_id = request.GET.get('campaign_id', None)
     try:
         campaign_object = Campaign.objects.get(id=campaign_id)
     except Campaign.DoesNotExist as err:
         raise RhizomeApiException(err)
     campaign_object.aggregate_and_calculate()
Esempio n. 4
0
    def validate_filters(self, request):
        '''
        Make sure that all required filters have been passed in
        '''

        filters = {}

        # Grab a mutable copy of the request#
        if hasattr(request, 'GET'):
            filters = request.GET.copy()

        # check required parameters and raise exception if one is missing #
        if hasattr(self._meta, 'GET_params_required'):
            keys_req = self._meta.GET_params_required
            missing_keys = list(set(keys_req).difference(set(filters.keys())))
            if len(missing_keys) > 0:
                msg = 'Missing required parameter %s' % missing_keys[0]
                raise RhizomeApiException(msg)

        return filters
Esempio n. 5
0
    def obj_get(self, bundle, **kwargs):
        """
        Takes optional ``kwargs``, which are used to narrow the query to find
        the instance.

        Currently used to find one object from the url api/v1/resource/<pk>/

        Try to find an object using the pk
        """

        try:
            obj = self._meta.object_class.objects.get(id=kwargs['pk'])
        except ObjectDoesNotExist:
            msg = 'No {0} object found for id : {1}'\
                .format(self._meta.resource_name, kwargs['pk'])
            raise RhizomeApiException(message=msg, code=500)
        except MultipleObjectsReturned:
            raise http.HttpMultipleChoices("More than one resource found\
                at this URI.")

        return obj
Esempio n. 6
0
    def validate_obj_create(self, bundle, **kwargs):
        '''
        Custom module that is meant to clean and check the POST request, making
        sure that we catch any errors we can befoer submitting to the database.

        For instance, here, we check that the necessary Keys are there, so that
        we can save a trip to the DB by using the `required_fields_for_post`
        attribute on the Meta class of the base_model resoruce.
        '''

        keys_passed = [unicode(x) for x in bundle.data.keys()]
        keys_req = [unicode(x) for x in self._meta.required_fields_for_post]
        missing_keys = set(keys_req).difference(set(keys_passed))

        if len(missing_keys) > 0:
            raise RhizomeApiException(message='missing params %s' %
                                      missing_keys)

        bundle = self.clean_json_fields(bundle)

        return bundle
Esempio n. 7
0
    def obj_create(self, bundle, **kwargs):
        """
        A ORM-specific implementation of ``obj_create``.

        This also handles updates ( PUT ) requests by looking if the
        request has the ID in there, and if so, updating the resorce with the
        relevant data items.
        """

        # add any additional data needed for post, for instance datapoitns
        # that are inserted via a POST request should have a document
        bundle = self.add_default_post_params(bundle, **kwargs)

        ## Try to validate / clean the POST before submitting the INSERT ##
        bundle = self.validate_obj_create(bundle, **kwargs)

        #### FIXME REMOVE BELOW CODE AND USE PROPER PATCH FORMAT ####
        id_from_post = bundle.data.get('id', None)
        if id_from_post == '-1':
            del bundle.data['id']
            id_from_post = None

        if id_from_post:  # this is a PUT or update of an existing resource #
            obj = self._meta.object_class.objects.get(id=id_from_post)
            self.update_object(obj, **bundle.data)
        #### REMOVE ABOVE CODE ####

        else:  # create the object with the data from the request #
            try:
                obj = self._meta.object_class.objects.create(**bundle.data)
            except IntegrityError as err:
                raise RhizomeApiException(message=err.message, code=497)

        bundle.obj = obj
        bundle.data['id'] = bundle.obj.id

        return bundle