Example #1
0
def testbackend():
    '''
    backend
    '''
    import backend as b

    b.register('dani2', 'pass')
    assert b.check_user_name('dani2')

    c = b.auth('dani2', 'password', password='pass')
    b.set_password(c, 'nuevo', 'xxxx', type='email',
            account='danigm', description="http://mail.danigm.net")
    p = b.get_password(c, 'nuevo')
    assert p.name == 'nuevo'

    b.del_password(c, 'nuevo')
    p = b.get_passwords_by(c, name='nuevo')
    assert not p

    b.unregister(c)
    assert not b.check_user_name('dani2')
Example #2
0
    def post(self):
        """
        Handle a POST request to the API.
        
        Take a list of actions from a POST request, and do the appropriate
        action. See the documentation for this module for more information.
        
        @raise common.InputError: If no search targets are given in the target
        POST variable or if an invalid action is given.
        """
        cpu_start = quota.get_request_cpu_usage()
        
        action = self.request.get("action")
        target = self.request.get("target", "public")
        spectra = self.request.get_all("spectrum") #Some of these will be in session data
        limit = self.request.get("limit", 10)
        offset = self.request.get("offset", 0)
        algorithm = self.request.get("algorithm", "bove")
        guess = self.request.get("guess")
        spectrum_type = self.request.get("type")
        raw = self.request.get("raw", False)
        session = appengine_utilities.sessions.Session()
        user = users.get_current_user()
        response = []
        
        # First check if the user has gone over quota.
        if session.get("cpu_usage") > self.CPU_LIMIT:
            raise common.ServerError("User has gone over quota.")
        
        spectra = [open('jcamp-test.jdx').read()] # Just for testing

        # If not operating on the main project, try getting the private one.
        # But abort if target is not supposed to be a project.
        if target and target != "public":
            target = backend.Project.get(target)
            if target is None:
                raise common.InputError(targets, "Invalid project ID.")
        # Start doing the request
        if action == "compare" and target == "public":
            # Search the database for something.
            for spectrum in spectra:
                # User wants to commit a new search with a file upload.
                result = backend.search(spectrum)
                # Extract relevant information and add to the response.
                response = [(str(i.key()), i.chemical_name, i.error, i.graph_data) for i in result]
        elif action == "compare":
            # Compare multiple spectra uploaded in this session.
            response.append(backend.compare(spectra, algorithm))
        elif action == "browse":
            # Get a list of spectra from the database for browsing
            backend.auth(user, target, "view")
            # Return the database key, name, and chemical type.
            results = [(str(spectrum.key()), spectrum.chemical_name, spectrum.chemical_type)
                       for spectrum in backend.browse(target, limit, offset, guess, spectrum_type)]
            response.extend(results)
        elif action == "add":
            # Add a new spectrum to the database. Supports multiple spectra.
            backend.auth(user, target, "spectrum")
            for spectrum_data in spectra:
                backend.add(spectrum_data, target, False)
        elif action == "bulkadd":
            # Add a new spectrum to the database. Supports multiple spectra.
            if session.key().name() != "uploader":
                raise common.AuthError(user, "Only the uploader can bulkadd.")
            for spectrum_data in spectra:
                backend.add(spectrum_data, target, True)
        elif action == "delete":
            # Delete a spectrum from the database.
            backend.auth(user, target, "spectrum")
            for spectrum_data in spectra:
                backend.delete(spectrum_data, target)
        elif action == "update":
            backend.auth(user, "public", "spectrum")
            backend.update()
        elif action == "projects":
            query = "WHERE :1 IN owners OR :1 IN collaborators OR :1 in viewers"
            response.extend([(proj.key(), proj.name) for proj in Project.gql(query, user)])
        else:
            # Invalid action. Raise an error.
            raise common.InputError(action, "Invalid API action.")
        # Pass it on to self.output for processing.
        self.output(response)
Example #3
0
 def m_auth(self, user, password):
     ''' Return the cookie. '''
     return backend.auth(user, 'password',
             password=password)