Esempio n. 1
0
File: views.py Progetto: filipp/mwa2
def detail(request, manifest_path):
    '''Returns data on a given manifest'''
    if request.method == 'GET':
        LOGGER.debug("Got read request for %s", manifest_path)
        manifest = Manifest.read(manifest_path)
        if manifest is None:
            raise Http404("%s does not exist" % manifest_path)
        context = {'plist_text': manifest,
                   'pathname': manifest_path}
        return render(request, 'manifests/detail.html', context=context)
    if request.method == 'POST':
        # could be PUT, POST, or DELETE
        if request.META.has_key('HTTP_X_METHODOVERRIDE'):
            http_method = request.META['HTTP_X_METHODOVERRIDE']
            if http_method.lower() == 'delete':
                # DELETE
                LOGGER.debug("Got delete request for %s", manifest_path)
                if not request.user.has_perm('manifest.delete_manifestfile'):
                    raise PermissionDenied
                try:
                    Manifest.delete(manifest_path, request.user)
                except ManifestError, err:
                    return HttpResponse(
                        json.dumps({'result': 'failed',
                                    'exception_type': str(type(err)),
                                    'detail': str(err)}),
                        content_type='application/json')
                else:
                    return HttpResponse(
                        json.dumps({'result': 'success'}),
                        content_type='application/json')
            elif http_method.lower() == 'put':
                # regular POST (update/change)
                LOGGER.debug("Got write request for %s", manifest_path)
                if not request.user.has_perm('manifest.change_manifestfile'):
                    raise PermissionDenied
                if request.is_ajax():
                    json_data = json.loads(request.body)
                    if json_data and 'plist_data' in json_data:
                        plist_data = json_data['plist_data'].encode('utf-8')
                        try:
                            Manifest.write(
                                json_data['plist_data'], manifest_path,
                                request.user)
                        except ManifestError, err:
                            return HttpResponse(
                                json.dumps({'result': 'failed',
                                            'exception_type': str(type(err)),
                                            'detail': str(err)}),
                                content_type='application/json')
                        else:
                            return HttpResponse(
                                json.dumps({'result': 'success'}),
                                content_type='application/json')
            else:
                LOGGER.warning(
                    "Got unknown HTTP_X_METHODOVERRIDE for %s: %s",
                    manifest_path, http_method)
Esempio n. 2
0
File: views.py Progetto: filipp/mwa2
     else:
         LOGGER.warning(
             "Got unknown HTTP_X_METHODOVERRIDE for %s: %s",
             manifest_path, http_method)
 else:
     # true POST request; create new resource
     LOGGER.debug("Got create request for %s", manifest_path)
     try:
         json_data = json.loads(request.body)
     except ValueError:
         json_data = None
     if json_data and 'plist_data' in json_data:
         plist_data = json_data['plist_data'].encode('utf-8')
         try:
             Manifest.write(
                 json_data['plist_data'], manifest_path,
                 request.user)
         except ManifestError, err:
             return HttpResponse(
                 json.dumps({'result': 'failed',
                             'exception_type': str(type(err)),
                             'detail': str(err)}),
                 content_type='application/json')
     else:
         try:
             plist_data = Manifest.new(manifest_path, request.user)
         except ManifestError, err:
             return HttpResponse(
                 json.dumps({'result': 'failed',
                             'exception_type': str(type(err)),
                             'detail': str(err)}),