Ejemplo n.º 1
0
    def get_users(self):
        """
        Get list of users.
        """

        keys_on = "PRAGMA foreign_keys = ON"
        query = "SELECT * FROM users"

        # user_nickname = None

        # connects (and creates if necessary) to the database. gets a connection object
        # con = sqlite3.connect(self.database_name)
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            # execute the statement
            cur.execute(query)
            # get results
            rows = cur.fetchall()
            if len(rows) == 0:
                return None

            users = []
            for row in rows:
                users.append(UserModel.create(row))
            return users
Ejemplo n.º 2
0
    def get_user(self, user_nickname):
        """
        returns user.
        Return "None" if user not found.
        """

        keys_on = "PRAGMA foreign_keys = ON"
        query = "SELECT * FROM users WHERE user_nickname = ?"
        pvalue = (user_nickname,)

        # user_nickname = None

        # connects (and creates if necessary) to the database. gets a connection object
        # con = sqlite3.connect(self.database_name)
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            # execute the statement
            cur.execute(query, pvalue)
            # just one result possible
            row = cur.fetchone()
            if row is None:
                return None
            else:
                # print row.keys()

                return UserModel.create(row)
Ejemplo n.º 3
0
 def _createuser(self, request, user_nickname):
     '''
     Create new user.
     Return 400 on bad request.
     Return 204 on success.
     '''
     if database.contains_user(user_nickname):
         return Response(status=status.HTTP_409_CONFLICT)
     usermodel = None
     try:
         #print request.DATA
         usermodel = UserModel(user_nickname, raw_data=request.DATA)
     except Exception as e:
         #print "Could not add the data "+ str(e)
         #traceback.print_exc()
         
         error = ErrorModel('Invalid data.').serialize()
         
         return Response(error, status=status.HTTP_400_BAD_REQUEST)
     database.add_user(usermodel)
     try:
         if sys.argv[1] != "test":
             user = AuthUser.objects.create_user(usermodel.user_nickname, usermodel.email, request.DATA.get('password', None))
             user.save()
     except IndexError:
         pass
     url = reverse("user", (user_nickname,), request=request)
     return Response(status=status.HTTP_204_NO_CONTENT,
                     headers={"Location":url})
Ejemplo n.º 4
0
    def get_users(self):
        '''
        Get list of users.
        '''

        keys_on = 'PRAGMA foreign_keys = ON'
        query = 'SELECT * FROM users'

        #user_nickname = None

        #connects (and creates if necessary) to the database. gets a connection object
        #con = sqlite3.connect(self.database_name)
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            #execute the statement
            cur.execute(query)
            #get results
            rows = cur.fetchall()
            if len(rows) == 0:
                return None

            users = []
            for row in rows:
                users.append(UserModel.create(row))
            return users
Ejemplo n.º 5
0
    def get_user(self, user_nickname):
        '''
        returns user.
        Return "None" if user not found.
        '''

        keys_on = 'PRAGMA foreign_keys = ON'
        query = 'SELECT * FROM users WHERE user_nickname = ?'
        pvalue = (user_nickname, )

        #user_nickname = None

        #connects (and creates if necessary) to the database. gets a connection object
        #con = sqlite3.connect(self.database_name)
        con = sqlite3.connect(self.database_name)
        with con:
            con.row_factory = sqlite3.Row
            cur = con.cursor()
            cur.execute(keys_on)
            #execute the statement
            cur.execute(query, pvalue)
            #just one result possible
            row = cur.fetchone()
            if row is None:
                return None
            else:
                #print row.keys()

                return UserModel.create(row)
Ejemplo n.º 6
0
    def _updateuser(self, request, user_nickname):
        '''
        Edit user.
        Return 400 if request is invalid.
        Return 204 on success.
        '''
        try:
            usermodel = UserModel(user_nickname)
        except Exception as e:
            print "Could not add the data "+ str(e)
            traceback.print_exc()
            return Response(status = 400)
        if usermodel is None:
            return Response(status = status.HTTP_404_NOT_FOUND)
        #request.DATA contains a dictionary with the entity body input.
        #Deserialize the data and modify the model
        try:
            #EXTRACT THE PRIVATE DATA

            email = request.DATA['email']
            description = request.DATA.get("description","")
            #SET VALUES TO USER
            usermodel.nickname = user_nickname
            usermodel.description = description
            usermodel.email = email
            try:
                if sys.argv[1] != "test":
                    u = AuthUser.objects.get(username__exact=user_nickname)
                    u.set_password(request.DATA.get("password", None))
                    u.save()
            except IndexError:
                pass
            
        except Exception as e:
            print "Could not add the data "+ str(e)
            traceback.print_exc()
            return Response(status = 400)
        #Update the model to the database
        database.edit_user(usermodel)
        url = reverse("user", (user_nickname,), request=request)
        return Response(status=status.HTTP_204_NO_CONTENT,
                        headers={"Location":url})