示例#1
0
    def test_belongs_to_investment(app_and_db, active_user):
        db = app_and_db[1]
        investment = Investment(name="test invest")
        worker = Worker(position="pos1", user_id=1)
        investment.workers.append(worker)
        db.session.add(investment)
        db.session.commit()

        assert Worker.belongs_to_investment(email="*****@*****.**",
                                            investment_id=1)
        assert not Worker.belongs_to_investment(email="*****@*****.**",
                                                investment_id=1)
示例#2
0
def add_worker() -> str:
    if not Worker.is_admin(current_user.id, g.current_invest.id):
        return redirect(url_for("team.team"))
    form = CreateWorkerForm()
    if form.validate_on_submit():
        if Worker.belongs_to_investment(form.email.data, g.current_invest.id):
            flash("This user is already added to workers.")
            return redirect(url_for("team.team"))
        user = User.query.filter_by(email=form.email.data).first()
        if not user:
            user = User(
                username=form.email.data,
                email=form.email.data,
                password=uuid.uuid4().hex,
            )
            db.session.add(user)
            db.session.commit()
            user = User.query.filter_by(email=form.email.data).first()
            email.send_complete_registration_mail(user)
        worker = Worker(
            position=form.position.data,
            admin=form.admin.data,
            user_id=user.id,
        )
        g.current_invest.workers.append(worker)
        db.session.commit()
        flash("You have added new worker successfully.")
        return redirect(url_for("team.team"))
    return render_template("team/form.html", title="Add Worker", form=form)