~ubuntu-branches/ubuntu/saucy/maas/saucy-updates

« back to all changes in this revision

Viewing changes to src/maasserver/api.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez, Andres Rodriguez, Steve Langasek
  • Date: 2013-03-19 15:38:22 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20130319153822-17izmcpy6wjlyfqi
Tags: 1.3+bzr1455+dfsg-0ubuntu1
* New upstream bugfix release.
  - Fixes and returns the 'resource_url' with the 'canonical' url for
    a file that is fetched using the API (LP: #1154142)

[ Andres Rodriguez ]
* debian/control:
  - Change Conflicts/Replaces for Breaks/Replaces.
  - Conflicts on tftpd-hpa and dnsmasq.
  - Do not pre-depends, but Depends on ${misc:Depends} for 'maas'.

[ Steve Langasek ]
* postinst scripts are never called with 'reconfigure' as the script
  argument.  Remove references to this (mythical) invocation.
* always call 'set -e' from maintainer scripts instead of passing 'sh -e'
  as the interpreter, so that scripts will behave correctly when run via
  'sh -x'.
* invoke-rc.d is never allowed to not exist - simplify scripts (and make
  them better policy-compliant) by invoking unconditionally.  (The only
  possible exception is in the postrm, where it's *theoretically* possible
  for invoke-rc.d to be missing if the user has completely stripped
  down their system; that's a fairly unreasonable corner case, but we
  might as well be correct if it ever happens.)
* db_get+db_set is a no-op; don't call db_set to push back a value we just
  got from db_get.
* Omit superfluous calls to 'exit 0' at the end of each script.
* Remove maas-cluster-controller prerm script, which called debconf for no
  reason.
* Don't invoke debconf in the postrm script either, debhelper already does
  this for us.
* Other miscellaneous maintainer script fixes
* debian/maas-common.postinst: call adduser and addgroup unconditionally;
  the tools are already designed to DTRT, we don't need to check for the
  user/group existence before calling them nor should we worry about
  calling them only once on first install.
* debian/maas-common.postrm: delete the maas group, not just the user,
  as the comment in the code implies we should do.

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
    PermissionDenied,
105
105
    ValidationError,
106
106
    )
 
107
from django.core.urlresolvers import reverse
107
108
from django.db.utils import DatabaseError
108
109
from django.forms.models import model_to_dict
109
110
from django.http import (
115
116
    render_to_response,
116
117
    )
117
118
from django.template import RequestContext
118
 
from django.utils.http import urlquote_plus
119
119
from docutils import core
120
120
from formencode import validators
121
121
from maasserver.api_support import (
846
846
    # Encode the content as base64.
847
847
    dict_representation['content'] = b64encode(
848
848
        getattr(stored_file, 'content'))
 
849
    dict_representation['resource_uri'] = reverse(
 
850
        'file_handler', args=[stored_file.filename])
849
851
    # Emit the json for this object manually because, no matter what the
850
852
    # piston documentation says, once a type is associated with a list
851
853
    # of fields by piston's typemapper mechanism, there is no way to
855
857
    return stream
856
858
 
857
859
 
858
 
def get_owned_file_or_404(filename, user):
859
 
    """Return the file named 'filename' owned by 'user'.
860
 
 
861
 
    If there is no such file, try getting the corresponding unowned file
862
 
    to be compatible with old versions of MAAS where all the files where
863
 
    unowned.
864
 
    """
865
 
    stored_files = FileStorage.objects.filter(
866
 
        filename=filename, owner=user)
867
 
    # filename and owner are 'unique together', we can have either 0 or 1
868
 
    # objects in 'stored_files'.
869
 
    if len(stored_files) == 0:
870
 
        # In order to support upgrading from installations where the notion
871
 
        # of a file owner was not yet implemented, return non-owned files
872
 
        # with this filename at a last resort option.
873
 
        stored_files = FileStorage.objects.filter(
874
 
            filename=filename, owner=None)
875
 
        if len(stored_files) == 0:
876
 
            raise Http404
877
 
    return stored_files[0]
878
 
 
879
 
 
880
860
class FileHandler(OperationsHandler):
881
861
    """Manage a FileStorage object.
882
862
 
891
871
 
892
872
        The 'content' of the file is base64-encoded."""
893
873
        try:
894
 
            stored_file = get_owned_file_or_404(
895
 
                filename=filename, user=request.user)
 
874
            stored_file = get_object_or_404(FileStorage,
 
875
                filename=filename, owner=request.user)
896
876
        except Http404:
897
877
            # In order to fix bug 1123986 we need to distinguish between
898
878
            # a 404 returned when the file is not present and a 404 returned
909
889
    @operation(idempotent=False)
910
890
    def delete(self, request, filename):
911
891
        """Delete a FileStorage object."""
912
 
        stored_file = get_owned_file_or_404(
913
 
            filename=filename, user=request.user)
 
892
        stored_file = get_object_or_404(FileStorage,
 
893
            filename=filename, owner=request.user)
914
894
        stored_file.delete()
915
895
        return rc.DELETED
916
896
 
918
898
    def resource_uri(cls, stored_file=None):
919
899
        filename = "filename"
920
900
        if stored_file is not None:
921
 
            filename = urlquote_plus(stored_file.filename)
 
901
            filename = stored_file.filename
922
902
        return ('file_handler', (filename, ))
923
903
 
924
904