Example #1
0
def create_data_account():
    role_admin = Role.objects(name="admin").first()
    role_academic = Role.objects(name="academic").first()
    role_cashier = Role.objects(name="cashier").first()

    list_admin = User.objects(role=role_admin)
    list_academic = User.objects(role=role_academic)
    list_cashier = User.objects(role=role_cashier)

    hashed_password = bcrypt.generate_password_hash("1").decode('utf-8')

    num = 1
    for item in list_admin:
        Account(username="******" + str(num),
                password=hashed_password,
                user=item).save()
        num += 1

    num = 1
    for item in list_academic:
        Account(username="******" + str(num),
                password=hashed_password,
                user=item).save()
        num += 1

    num = 1
    for item in list_cashier:
        Account(username="******" + str(num),
                password=hashed_password,
                user=item).save()
        num += 1
Example #2
0
def staff_info(id):  
    if current_user.user.role.name == 'admin' or current_user.user.role.name == 'cashier': 
        activate = list(range(10))
        activate[4] = "active"
        is_activate = True
        if current_user.is_authenticated:
            user = User.objects(id=id,is_activate=True).first()  
            form = UpdateStaffForm()  
            if id is None or user is None:
                return redirect(url_for('staff.index',page=1))  
            if form.validate_on_submit():
                if form.picture.data:
                    picture_file = save_picture(form.picture.data)
                    user.image_file = picture_file
                user.first_name=form.first_name.data 
                user.last_name=form.last_name.data 
                user.phone =form.phone.data 
                user.email =form.email.data
                user.salary =form.salary.data
                user.address =form.address.data
                user.birth =form.birth.data 
                user.gender = form.gender.data  
                role = form.role.data
                if role == 'cashier' or role == 'academic':
                    role = Role.objects(name=role).first()
                    # print(role.name)  
                    user.role = role
                    # print(user.role.id,role.id)
                else:  
                    flash('Can\'t find role, please reload page', 'danger')
                    return redirect(url_for('staff.staff_info',id=id))
                    
                user.save()
                flash('Staff has been updated!', 'success')
                return redirect(url_for('staff.staff_info',id=id))
            elif request.method == 'GET':
                form.first_name.data = user.first_name
                form.last_name.data = user.last_name
                form.phone.data = user.phone
                form.email.data = user.email
                form.salary.data = user.salary
                form.address.data = user.address 
                form.gender.data = user.gender 
                form.birth.data = user.birth
                form.role.data = user.role 
            receipts = PaymentReceipt.objects(staff=user)
            account = Account.objects(user=user).first()
            if account and account.is_activate == False:
                flash('Account of staff hasn\'t been acitved yet !!', 'info')  
                is_activate = False
            return render_template('staff_info.html', title='Staff Info',activate=activate,staff=user,form=form,receipts=receipts,is_activate = is_activate)
        return redirect(url_for('staff.index',page=1))
    else:
        flash('You\'re not admin. You can not access this page')
        return redirect(url_for('main.index'))
Example #3
0
def staff_inactived(page):    
    if current_user.user.role.name != 'admin': 
        flash('You\'re not admin. You can not access this page')
        return redirect(url_for('main.index'))
    activate = list(range(10))
    activate[4] = "active"  
    page = int(page) 
    role_academic = Role.objects(name='academic').first()
    role_cashier = Role.objects(name='cashier').first() 
    if page is None or page == 1:
        page = request.args.get('page', 1, type=int) 
        accounts = Account.objects(is_activate=False).limit(10)
    elif page <=-1:
        page = int((Account.objects(is_activate=False).count()-10) /10) + 2   
        accounts = Account.objects(is_activate=False).skip(Account.objects().count()-10) 
    else:
        accounts = Account.objects(is_activate=False).skip(page*10-10).limit(10) 
     
    users = list()
    for item in accounts: 
        users.append(item.user)
    return render_template('staff_inactived.html', title='Staff Inactive Yet',activate=activate,staffs=users,page_num=page)  
Example #4
0
def restore(id): 
    activate = list(range(10))
    activate[4] = "active"     
    staff = User.objects(id=id,is_activate=False).first() 
    account = Account.objects(user=staff).first()
    if staff: 
        staff.is_activate = True
        account.is_activate = True
        staff.save()
        account.save()
        flash('Staff restore success !!', 'success')  
        return redirect(url_for('staff.staff_removed',page=1))
    else:
        flash('Student with id ' + id+ ' can\'t find  !!', 'danger')
    return redirect(url_for('staff.staff_removed',page=1))
Example #5
0
def reset_request():
    form = RequestResetForm()
    if form.validate_on_submit():
        user = User.objects(email=form.email.data).first()
        account = Account.objects(user=user).first()
        if user and account and account.is_activate:
            send_reset_email(user)
            flash(
                'An email has been sent with instructions to reset your password.',
                'info')
            return redirect(url_for('users.login'))
        elif user and account and account.is_activate == False:
            flash(
                'You haven\'t login yet. Please contact admin to get more info.',
                'info')
    return render_template('reset_request.html',
                           title='Reset Password',
                           form=form)
Example #6
0
def remove(id): 
    activate = list(range(10))
    activate[4] = "active"  
    role = Role.objects(name="staff").first()
    staff = User.objects(id=id,is_activate=True).first()
    account = Account.objects(user=staff).first()
    
    if staff and account:
        TuitionReceipts = TuitionReceipt.objects(student=staff)
        if len(TuitionReceipts) > 0:
            flash('Staff can\'t remove !!', 'danger')
            return redirect(url_for('staff.staff_info',id=staff.id)) 
        staff.is_activate = False 
        account.is_activate = False
        staff.save()  
        account.save()
        flash('Staff delete success !!', 'success') 
        return redirect(url_for('staff.index',page=1))
    flash('Can\'t find staff !!', 'danger')
    return redirect(url_for('staff.index',page=1))
Example #7
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    form = LoginForm()
    if form.validate_on_submit():
        account = Account.objects(username=form.username.data).first()
        if account and account.user.is_activate is False:
            flash(
                'Login unsuccessful, because you are not belong to system. Please contact admin',
                'danger')
        elif account and account.user.is_activate and bcrypt.check_password_hash(
                account.password, form.password.data):
            login_user(account, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('main.index'))
        else:
            flash('Login unsuccessful. Please check username and password',
                  'danger')
    return render_template('login.html', title='Login', form=form)
Example #8
0
def reset_token(token):
    user = User.verify_reset_token(token)
    account = Account.objects(user=user).first()
    if user is None or account is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        account.password = hashed_password
        user.is_confirmed = True
        account.is_activate = True
        user.save()
        account.save()
        flash('Your password has been updated! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html',
                           title='Reset Password',
                           form=form)
Example #9
0
def create():  
    if current_user.user.role.name != 'admin': 
        flash('You\'re not admin. You can not access this page')
        return redirect(url_for('main.index'))
    activate = list(range(10))
    activate[4] = "active"  
    form = CreateStaffForm()  
    image_file = ""
    if id is None:
        return redirect(url_for('staff.index',page=1))

    if form.validate_on_submit():
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            image_file = picture_file
        first_name=form.first_name.data 
        last_name=form.last_name.data 
        phone =form.phone.data
        salary = form.salary.data 
        email =form.email.data
        address =form.address.data
        birth =form.birth.data 
        gender = form.gender.data  
        if form.role.data == 'cashier' or form.role.data == 'academic' or form.role.data == 'admin':
            role = Role.objects(name=form.role.data).first() 
        else:  
            flash('Can\'t find role, please reload page', 'danger')
            return redirect(url_for('staff.create'))
        if image_file == "":
            user = User(first_name=first_name,last_name=last_name,phone=phone,email=email,gender=gender,address=address,birth=birth,role=role,image_file=image_file,salary=salary).save()
        else:
            user = User(first_name=first_name,last_name=last_name,phone=phone,email=email,gender=gender,address=address,birth=birth,role=role,salary=salary).save()
        account = Account(username=phone,password="******",user=user,is_activate=False).save()
        send_reset_email(user)  
        flash('Staff has been created! Your account is also created, please use phone to login to system. Verify is sending to your gmail, please wait a few minutes. Within 3 hour, you must login to system to change your new password', 'success')
        return redirect(url_for('staff.staff_info',id=user.id)) 
    return render_template('staff_create.html', title='Staff Create Staff\'s Info',activate=activate,form=form)
Example #10
0
def get_inactived_staff(key_word):  
    if current_user.is_authenticated:
        data = list()  
        account_inactived = Account.objects(is_activate=False).all()  
        staff = list()  
        print(account_inactived)
        for item in account_inactived:
            if item.user.role.name == 'academic' or item.user.role.name == 'cashier':
                staff.append(item.user)  

        print(staff)
        if key_word == 'all': 
            if staff:  
                count = 0
                for item in staff:
                    if count < 10:
                        ele = {
                            "id": str(item.id),
                            "address": item.address,
                            "birth": item.birth.strftime("%m-%d-%Y"),
                            "email": item.email,
                            "first_name": item.first_name,
                            "gender": item.gender,
                            "image_file": item.image_file,
                            "last_name": item.last_name,
                            "role": item.role.name,
                            "phone": item.phone,
                            "is_activate": item.is_activate
                        } 
                        data.append(ele) 
                        count+=1
            response = {
                'status':True,
                'message':'Get Students Success',
                'data':data
            }  
        else: 
            if staff:  
                count = 0
                for item in staff: 
                    print(key_word.lower() in item.first_name.lower(),key_word.lower() ,item.first_name.lower())
                    if count < 3 and key_word.lower() in item.first_name.lower() :
                        ele = {
                            "id": str(item.id),
                            "address": item.address,
                            "birth": item.birth.strftime("%m-%d-%Y"),
                            "email": item.email,
                            "first_name": item.first_name,
                            "gender": item.gender,
                            "image_file": item.image_file,
                            "last_name": item.last_name,
                            "role": item.role.name,
                            "phone": item.phone,
                            "is_activate": item.is_activate
                        } 
                        data.append(ele)
                        count+=1 
                count = 0
                for item in staff:
                    if count < 3 and key_word.lower() in item.last_name.lower() : 
                        flag = False                      
                        for ele in data:
                            if str(ele['id']) == str(item.id):
                                flag = True
                                break
                        if flag == False:   
                            ele = {
                                "id": str(item.id),
                                "address": item.address,
                                "birth": item.birth.strftime("%m-%d-%Y"),
                                "email": item.email,
                                "first_name": item.first_name,
                                "gender": item.gender,
                                "image_file": item.image_file,
                                "last_name": item.last_name,
                                "role": item.role.name,
                                "phone": item.phone,
                                "is_activate": item.is_activate
                            } 
                            data.append(ele)
                            count+=1 
                count = 0
                for item in staff:
                    if count < 3 and key_word.lower() in item.phone.lower() : 
                        flag = False                      
                        for ele in data:
                            if str(ele['id']) == str(item.id):
                                flag = True
                                break
                        if flag == False:   
                            ele = {
                                "id": str(item.id),
                                "address": item.address,
                                "birth": item.birth.strftime("%m-%d-%Y"),
                                "email": item.email,
                                "first_name": item.first_name,
                                "gender": item.gender,
                                "image_file": item.image_file,
                                "last_name": item.last_name,
                                "role": item.role.name,
                                "phone": item.phone,
                                "is_activate": item.is_activate
                            } 
                            data.append(ele)
                            count+=1 
                count = 0
                for item in staff: 
                    if count < 3 and key_word.lower() in item.email.lower():
                        flag = False                       
                        for ele in data:
                            if str(ele['id']) == str(item.id):
                                flag = True
                                break
                        if flag == False:   
                            ele = {
                                "id": str(item.id),
                                "address": item.address,
                                "birth": item.birth.strftime("%m-%d-%Y"),
                                "email": item.email,
                                "first_name": item.first_name,
                                "gender": item.gender,
                                "image_file": item.image_file,
                                "last_name": item.last_name,
                                "role": item.role.name,
                                "phone": item.phone,
                                "is_activate": item.is_activate
                            } 
                            data.append(ele)
                            count+=1 
            response = {
                'status':True,
                'message':'Get Students Success',
                'data':data
            } 
    else:
        response = {
            'status':False,
            'message':'Get Students Failed',
            'data': None
        } 
    return response 
Example #11
0
def verify_login(token):
    user = Account.verify_login_token(token)
    if user is None:
        return False
    return True