コード例 #1
0
ファイル: LocalAuth.py プロジェクト: annotons/genocrowd
    def set_group(self, data):
        """Assign a group to each student

        Parameters
        ----------
        groupsamount : str
            Number of groups

        Return
        ------
        dict
            error, error message and the list of grades
        """
        error = False
        error_message = []
        gradeList = self.users.distinct("grade")
        gradeList.remove('ADMIN')
        max_group = int(data["groupsAmount"])
        groupNumber = 1
        bson = BSONObjectIdConverter(BaseConverter)

        for element in gradeList:
            userCursor = list(self.users.find({'grade': element}))
            userList = []

            for document in userCursor:
                document['_id'] = str(document['_id'])
                userList.append(document)

            random.shuffle(userList)
            for user in userList:
                if groupNumber > max_group:
                    groupNumber = 1
                    self.users.find_one_and_update({
                        '_id': bson.to_python(user['_id'])}, {
                            '$set': {'group': groupNumber}}, return_document=ReturnDocument.AFTER)
                    self.groups.update({
                        'number': groupNumber}, {
                            '$push': {'student': user}})
                else:
                    self.users.find_one_and_update({
                        '_id': bson.to_python(user['_id'])}, {
                            '$set': {'group': groupNumber}}, return_document=ReturnDocument.AFTER)
                    self.groups.update({
                        'number': groupNumber}, {
                            '$push': {'student': user}})
                groupNumber += 1

        return {
            'error': error,
            'errorMessage': error_message,
            'gradeList': gradeList
        }
コード例 #2
0
    def test_bson_object_id_converter(self):
        converter = BSONObjectIdConverter("/")

        self.assertRaises(NotFound, converter.to_python, ("132"))
        assert converter.to_python("4e4ac5cfffc84958fa1f45fb") == \
               ObjectId("4e4ac5cfffc84958fa1f45fb")
        assert converter.to_url(ObjectId("4e4ac5cfffc84958fa1f45fb")) == \
               "4e4ac5cfffc84958fa1f45fb"
コード例 #3
0
    def test_bson_object_id_converter(self):
        converter = BSONObjectIdConverter("/")

        self.assertRaises(NotFound, converter.to_python, ("132"))
        assert converter.to_python("4e4ac5cfffc84958fa1f45fb") == \
            ObjectId("4e4ac5cfffc84958fa1f45fb")
        assert converter.to_url(ObjectId("4e4ac5cfffc84958fa1f45fb")) == \
            "4e4ac5cfffc84958fa1f45fb"
コード例 #4
0
ファイル: LocalAuth.py プロジェクト: annotons/genocrowd
    def update_password(self, inputs, user):
        """Update the password of a user

        Parameters
        ----------
        inputs : dict
            Curent password and the new one (and confirmation)
        user : dict
            The current user

        Returns
        -------
        dict
            error, error message and updated user
        """
        error = False
        error_message = ''
        updated_user = user

        # check if new passwords are identicals
        password_identical = (inputs['newPassword'] == inputs['confPassword'])

        if not inputs["newPassword"] == '':
            if password_identical:
                # Try to authenticate the user with his old password
                credentials = {
                    'login': user['username'],
                    'password': inputs['oldPassword']}
                authentication = self.authenticate_user(credentials)
                if not authentication['error']:
                    # Update the password
                    password = self.app.bcrypt.generate_password_hash(
                        inputs['newPassword']).decode('utf-8')
                    bson = BSONObjectIdConverter(BaseConverter)
                    updated_user = self.users.find_one_and_update({
                        '_id': bson.to_python(user['_id'])}, {
                            '$set': {
                                'password': password
                            }}, return_document=ReturnDocument.AFTER)
                else:
                    error = True
                    error_message = 'Incorrect old password'
            else:
                error = True
                error_message = 'New passwords are not identical'
        else:
            error = True
            error_message = 'Empty password'
        return {
            'error': error,
            'error_message': error_message,
            'user': updated_user}
コード例 #5
0
def delete_account():
    """ Delete the user account and logs him out
    Returns
    -------
    json
        user : empty dictionnary
        logged: False
    """
    users = ca.mongo.db.users
    bson = BSONObjectIdConverter(BaseConverter)
    users.find_one_and_delete({'_id': bson.to_python(session['user']['_id'])})
    session.pop('user', None)
    ca.logger.debug(session)

    return {'user': {}, 'logged': False}
コード例 #6
0
ファイル: LocalAuth.py プロジェクト: annotons/genocrowd
    def update_profile(self, data, user):
        """Update the profile of a user

        Parameters
        ----------
        inputs : dict
            fields to update
        user : dict
            The current user

        Returns
        -------
        dict
            error, error message and updated user
        """
        error = False
        error_message = []
        username = data['newUsername']
        email = data['newEmail']
        if len(username) == 0:
            assert username != user['username']
            username = user['username']
        if len(email) == 0:
            email = user['email']
        bson = BSONObjectIdConverter(BaseConverter)
        updated_user = self.users.find_one_and_update({
            '_id': bson.to_python(user['_id'])}, {
                '$set': {
                    'username': username,
                    'email': email
                }}, return_document=ReturnDocument.AFTER)
        assert updated_user['username'] == username
        return {
            'error': error,
            'errorMessage': error_message,
            'user': updated_user
        }