Пример #1
0
    def csv_import(self):

        if not (os.path.isfile("data/courses.csv") and \
        os.path.isfile("data/enrolments.csv") and \
        os.path.isfile("data/passwords.csv")):
            return 0  #failure

        with open('data/courses.csv', 'r') as csv_in:
            reader = csv.reader(csv_in)
            for row in reader:
                if not Course.query.filter_by(name=row[0],
                                              session=row[1]).first():
                    course = Course(name=row[0], session=row[1])
                    db.session.add(course)

            db.session.commit()

        with open('data/enrolments.csv', 'r') as csv_in:
            reader = csv.reader(csv_in)
            for row in reader:
                course = Course.query.filter_by(name=row[1],
                                                session=row[2]).first()
                if course:
                    c_id = course.id
                else:
                    return 0

                if not Enrolment.query.filter_by(u_id=row[0],
                                                 c_id=c_id).first():
                    enrolment = Enrolment(u_id=row[0], c_id=c_id)
                    db.session.add(enrolment)

            db.session.commit()

        with open('data/passwords.csv', 'r') as csv_in:
            reader = csv.reader(csv_in)
            for row in reader:
                u_type = 0
                if row[2] == "student":
                    u_type = 1
                elif row[2] == "staff":
                    u_type = 2

                if not User.query.filter_by(id=row[0]).first() and u_type:
                    user = User(id=row[0],
                                username=row[0],
                                password=generate_password_hash(row[1]),
                                type=u_type)
                    db.session.add(user)

            db.session.commit()

        return 1  #success
Пример #2
0
    def create_user(self, uid, username, password, type):
        #check parameters valid
        if password.isspace() or password == "" or username.isspace(
        ) or username == "":
            return 0  #failure username and password can't be blank (space)
        if User.query.filter_by(username=username).first():
            return 0  #failure: user with same username already exists
        if User.query.filter_by(id=uid).first():
            return 0  #failure: user with same id already exists
        if type > 4 or type < 1:
            return 0  #failure: incorrect type

        newU = User(id=uid,
                    username=str.lower(username),
                    password=generate_password_hash(password),
                    type=type)
        db.session.add(newU)
        db.session.commit()
        return 1  #success
Пример #3
0
def register():
    data = request.get_json()
    user = User(**data)
    db.session.add(user)
    db.session.commit()
    return jsonify(user.to_dict()), 201