Exemple #1
0
    def post(self, request):
        """
        Upload a new document.
        
        A document should be JSON formatted.
        
        If a list is provided, it's assumed to be a list of objects who are 
        documents.  Each document in the list is processed, validated and saved.
        
        For each document provided, the database ID is returned for that 
        document.  If a list is provided, the IDs will be returned in the same
        order as the documents were received.
        
        Basic sanity checking is done, and a JSON object with a list of errors 
        is returned if the response is not properly formed.
        
        The error will raise a HTTP 400 "Bad Request".
        
        Example error output:
        
        {
            'error' : "The document POSTed was not well formed",
            'fields' : [list of fields that were missing or contained errors],
        }
        """

        try:
            data = json.loads(request.raw_post_data)
        except ValueError, e:
            return json_response(json.dumps({
                'error' : e
            }), code="500")
Exemple #2
0
    def get(self, request):

        results = reports.find()
        # results = reports.find({"experiment_type": "Stone Measurement"})
        # results = reports.find({"data.measurement.width": {'$gt' : 80}})

        results = json.dumps([result for result in results], default=json_util.default)

        return json_response(results)
Exemple #3
0
def ajax_delete(request):
    data = {'valid': False, 'errors': ugettext('some errors...')}
    attachment_id = request.POST['id']
    attachment = Attachment.objects.get(pk=attachment_id)
    if (attachment.user != request.user):
        data['errors'] = ugettext('no right')
    else:
        attachment.delete()
        data['valid'] = True
        data.pop('errors')
    return json_response(data)
Exemple #4
0
def ajax_change_descn(request):
    #TODO AJAX POST ONLY
    #TODO HANDEL AJAX ERROR
    data = {'valid': False, 'errors': ugettext('some errors...')}
    attachment_id = request.POST['id']
    attachment = Attachment.objects.get(pk=attachment_id)
    if (attachment.user != request.user):
        data['errors'] = ugettext('no right')
    elif request.method == "POST":
        attachment.description = request.POST['descn']
        data['valid'] = True
        data.pop('errors')
        attachment.save()
    return json_response(data)
Exemple #5
0
def ajax_upload(request):
    data = {'valid': False, 'errors': ugettext('no file')}
    attachment_form = AttachmentForm(user=request.user)
    if request.method == "POST":
        attachment_form = AttachmentForm(request.POST, request.FILES, user=request.user, \
                actived=False)
        #TODO improve validate
        if  attachment_form.is_valid():
            attachment = attachment_form.save()
            data['valid'] = True
            data.pop('errors')
            data['attachment'] = {'id': attachment.id, \
                    'fn': attachment.org_filename, 'url': attachment.file.url, 'descn': ''}
        else:
            print attachment_form.errors
    return json_response(data)
Exemple #6
0
    def get(self, request):
        
        import river_maths
        base_query = self.buildQuery(request)

        width_query = base_query
        width_query['experiment_type'] = "Wet Width"
        width_results = reports.find(width_query).sort('location.name')

        depth_query = self.buildQuery(request)
        depth_query['experiment_type'] = "Wet (Water) Depth"
        
        
        depth_results = reports.find(depth_query).sort('location.name')



        depths = [float(sum(r['data']['measurement'])) / len(r['data']['measurement']) for r in depth_results]
        # widths = list(width_results)
        widths = [r['data']['measurement'] for r in width_results]
        
        
        distance = [
            0.1,0.411,0.822,1.233,1.644,2.055,2.466,2.877,3.288,3.699,4.12
        ]
        
        wd = zip(widths, depths)
        
        csas = [river_maths.calCSA(w,d) for w,d in wd]
        
        plot_data = zip(distance,csas[:len(distance)])
        

        # results = json.dumps(list(depth_results), default=json_util.default)
        # results = json.dumps([result['experiment_type'] for result in results], default=json_util.default)

        return json_response(json.dumps(plot_data, default=json_util.default))
Exemple #7
0
 def _wrapped_view(request, *args, **kwargs):
     if not request.user.is_authenticated():
         return json_response({'valid': False, 'msg': msg})
     return view_func(request, *args, **kwargs)
Exemple #8
0
 def get(self, request):
     
     res = json.dumps({
         'error' : """Use POST to add or update a report."""
     })
     return json_response(res, code=500)