Commits

Richard Jones committed b4fd024

break all the stuffs

Comments (0)

Files changed (2)

     params = client.access_resource(RESOURCE_URL, params)
     print 'non-oauth parameters: %s' % params
 
+def example_upload(access_token, **params):
+    # test config
+    CONSUMER_KEY = 'sekrit'
+    CONSUMER_SECRET = '123'
+    RESOURCE_URL = 'https://testpypi.python.org/oauth/add_release'
+    # access some protected resources
+    print '* Access protected resources ...'
+    client = SimpleOAuthClient(CONSUMER_KEY, CONSUMER_SECRET, access_token)
+    params = client.access_resource(RESOURCE_URL, params, 'POST')
+    print 'non-oauth parameters: %s' % params
 
 if __name__ == '__main__':
     # For testing you will want to run example_init() and work through that
     access_token = oauth.OAuthToken(ACCESS_KEY, ACCESS_SECRET)
     example_test(access_token)
     example_release(access_token, name='spam', version='3.0', summary='Spam via OAuth',
-        description='This package was registered **VIA OAUTH**!')
+        description='This package was registered via OAuth.')
 
+    upload = FileUpload(content, filename...)
+    m = hashlib.md5()
+    m.update(content)
+    digest = m.hexdigest()
+    example_upload(access_token, name='spam', version='3.0', summary='Spam via OAuth',
+        description='This package was registered via OAuth.', content=upload,
+        filetype='sdist', md5_digest=digest, comment='via oauth')
+
         self.form['version'] = data['version']
         self.display(ok_message=message)
 
-    def submit(self):
+    def submit(self, parameters=None, response=True):
         ''' Handle the submission of distro metadata.
         '''
         # make sure the user is identified
             raise Unauthorised, \
                 "You must be identified to store package information"
 
+        if parameters is None:
+            parameters = self.form
+
         # pull the package information out of the form submission
-        data = self.form_metadata()
+        data = self.form_metadata(parameters)
 
         # validate the data
         try:
         message = self.store.store_package(name, version, data)
         self.store.changed()
 
-        # return a display of the package
-        self.display(ok_message=message)
+        if response:
+            # return a display of the package
+            self.display(ok_message=message)
 
     def form_metadata(self, submitted_data=None):
         ''' Extract metadata from the form.
         self.wfile.write(digest)
 
     CURRENT_UPLOAD_PROTOCOL = "1"
-    def file_upload(self, response=True):
+    def file_upload(self, response=True, parameters=None):
         # make sure the user is identified
         if not self.authenticated:
             raise Unauthorised, \
                 "You must be identified to edit package information"
 
+        if parameters is None:
+            parameters = self.form
+
         # can't perform CSRF check as this is invoked by tools
         #self.csrf_check()
 
         # Verify protocol version
-        if self.form.has_key('protocol_version'):
-            protocol_version = self.form['protocol_version']
+        if parameters.has_key('protocol_version'):
+            protocol_version = parameters['protocol_version']
         else:
             protocol_version = self.CURRENT_UPLOAD_PROTOCOL
 
 
         # figure the package name and version
         name = version = None
-        if self.form.has_key('name'):
-            name = self.form['name']
-        if self.form.has_key('version'):
-            version = self.form['version']
+        if parameters.has_key('name'):
+            name = parameters['name']
+        if parameters.has_key('version'):
+            version = parameters['version']
         if not name or not version:
             raise FormError, 'Name and version are required'
 
             description = release_metadata['description']
         else:
             # auto-register the release...
-            release_metadata = self.form_metadata()
+            release_metadata = self.form_metadata(parameters)
             description = release_metadata.get('description')
             try:
                 self.validate_metadata(release_metadata)
         # verify we have enough information
         pyversion = 'source'
         content = filetype = md5_digest = comment = None
-        if self.form.has_key('content'):
-            content = self.form['content']
-        if self.form.has_key('filetype'):
-            filetype = self.form['filetype']
+        if parameters.has_key('content'):
+            content = parameters['content']
+        if parameters.has_key('filetype'):
+            filetype = parameters['filetype']
             if filetype == 'sdist':
-                self.form['pyversion'] = 'source'
+                parameters['pyversion'] = 'source'
         if not content or not filetype:
             raise FormError, 'Both content and filetype are required'
 
-        md5_digest = self.form['md5_digest']
-
-        comment = self.form['comment']
+        md5_digest = parameters['md5_digest']
+
+        comment = parameters['comment']
 
         # python version?
-        if self.form['pyversion']:
-            pyversion = self.form['pyversion']
+        if parameters.get('pyversion', ''):
+            pyversion = parameters['pyversion']
         elif filetype not in (None, 'sdist'):
             raise FormError, 'Python version is required for binary distribution uploads'
 
         # grab content
         content = content.value
 
-        if self.form.has_key('gpg_signature'):
-            signature = self.form['gpg_signature']
+        if parameters.has_key('gpg_signature'):
+            signature = parameters['gpg_signature']
             try:
                 # If the signature is present, it may come
                 # as an empty string, or as a file upload
         be clarified and cleaned up).
         '''
         consumer, token, params, user = self._parse_request()
-
-        # pull the package information out of the form submission
-        data = self.form_metadata(params)
-
-        # validate the data
-        self.validate_metadata(data)
-
-        name = data['name']
-        version = data['version']
-
-        # make sure the user has permission to do stuff
-        has_package = self.store.has_package(name)
-        if has_package and not (
-                self.store.has_role('Owner', name) or
-                self.store.has_role('Admin', name) or
-                self.store.has_role('Maintainer', name)):
-            raise Forbidden, \
-                "You are not allowed to store '%s' package information"%name
-
-        if not has_package:
-            names = self.store.find_package(name)
-            if names:
-                raise Forbidden, "Package name conflicts with existing package '%s'" % names[0]
-
-        # make sure the _pypi_hidden flag is set
-        if not data.has_key('_pypi_hidden'):
-            data['_pypi_hidden'] = False
-
-        # save off the data
-        message = self.store.store_package(name, version, data)
-        self.store.changed()
-
+        self.submit(params, False)
         self.write_plain('OK')
 
     def oauth_upload(self):
         '''Upload a file for a package release.
         '''
         consumer, token, params, user = self._parse_request()
-        message = 'Access allowed for %s (ps. I got params=%r)'%(user, params)
+        self.file_upload(False, params)
         self.write_plain(message)
 
     def oauth_docupload(self):