Esempio n. 1
0
    def test_get_by_id(self):
        """Get user by ID."""
        user = User("foo", "*****@*****.**")
        user.save()

        retrieved = User.get_by_id(user.id)
        assert retrieved == user
Esempio n. 2
0
def register():
    """Register new user."""
    form = RegisterForm(request.form)
    if form.validate_on_submit():
        User.create(
            username=form.username.data,
            email=form.email.data,
            password=form.password.data,
            active=True,
        )
        flash("Thank you for registering. You can now log in.", "success")
        return redirect(url_for("public.home"))
    else:
        flash_errors(form)
    return render_template("public/register.html", form=form)
Esempio n. 3
0
 def test_check_password(self):
     """Check password."""
     user = User.create(username="******",
                        email="*****@*****.**",
                        password="******")
     assert user.check_password("foobarbaz123") is True
     assert user.check_password("barfoobaz") is False
Esempio n. 4
0
def register():
    """Register new user."""

    with open("co2data/co2clean.csv") as csvfile:
        reader = csv.reader(csvfile)
        countries = [rows[0] for rows in reader]

    form = RegisterForm(request.form)
    if form.validate_on_submit():
        User.create(
            username=form.username.data,
            email=form.email.data,
            password=form.password.data,
            active=True,
            country=form.country.data,
        )
        flash("Thank you for registering. You can now log in.", "success")
        return redirect(url_for("public_blueprint.home"))
    else:
        flash_errors(form)
    return render_template("public/register.html",
                           form=form,
                           countries=countries)
Esempio n. 5
0
def load_user(user_id):
    """Load user by ID."""
    return User.get_by_id(int(user_id))
Esempio n. 6
0
def create_dummy_data():
    User.query.delete()
    admin = User(
        username="******",
        email="*****@*****.**",
        password="******",
        active=True,
        country="Kenia",
        is_admin=True,
    )
    db.session.add(admin)

    normal_user = User(
        username="******",
        email="*****@*****.**",
        password="******",
        active=True,
        country="Switzerland",
    )
    db.session.add(normal_user)

    normal_user2 = User(
        username="******",
        email="*****@*****.**",
        password="******",
        active=True,
        country="USA",
    )
    db.session.add(normal_user2)

    normal_user3 = User(
        username="******",
        email="*****@*****.**",
        password="******",
        active=True,
        country="Lebanon",
    )
    db.session.add(normal_user3)

    category1 = Category(name="Food", parent_id=None)
    id1 = db.session.add(category1)
    db.session.flush()

    category2 = Category(name="Energy", parent_id=category1.id)
    id2 = db.session.add(category2)

    category3 = Category(name="Transport", parent_id=category1.id)
    id2 = db.session.add(category3)

    category4 = Category(name="Health", parent_id=category1.id)
    id2 = db.session.add(category4)

    category5 = Category(name="Social", parent_id=category1.id)
    id2 = db.session.add(category5)

    db.session.flush()

    company1 = Company(name="Accenture", description="Description")
    db.session.add(company1)
    company2 = Company(name="McKinsey", description="McKinsey description")
    db.session.add(company2)
    company3 = Company(name="SmartSolation", description="McKinsey description")
    db.session.add(company3)
    db.session.flush()

    chat_room = ChatRoom.create(
        name="Ein schoener Raum", room_id="The cold and amazing shower!"
    )

    challenge = Challenge(
        challengename="The cold and amazing shower!",
        description="""
Thousands of people from all over the world already shower cold. Not only will you save energy, CO<sub>2</sub> and water but there are also many positive effects on your health connected with showering cold.  Scientists found out, that cold showers do not only relief stress and prevents depressions, but also help to develop a more robust immune response.
Find out more:
https://www.wimhofmethod.com/benefits-of-cold-showers
https://www.healthline.com/health/cold-shower-benefits#improved-metabolism


You will save:
0.5kg of CO<sub>2</sub> per shower (based on gas boilers)
Equiv. 3.3 km with an average car

Company supporting you:
3X Carbon offsets in addition
        """,
        active=True,
        category_id=category1.id,
        co2offset=0.005,
        company_id=company1.id,
        chat_room=chat_room,
    )
    db.session.add(challenge)
    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    chat_room = ChatRoom.create(name="Ein schoener Raum", room_id="Obvious Outdoor")

    challenge1 = Challenge(
        challengename="Obvious Outdoor",
        description="""
The world is calling. Get out and enjoy your surroundings today. You can choose between running or cycling.
Still undecided?
Learn more: https://www.livestrong.com/article/372790-cycling-vs-running-calories/

Pick:
Run 4/8/12km
Cycle 15/30/45km
Company supporting you: Accenture!
Run: 20/20/30 kg Carbon offset
Eqvuiv. 66km/124km/200km with an average car
        """,
        active=True,
        co2offset=0.03,
        category_id=category2.id,
        company_id=company1.id,
        chat_room=chat_room,
    )
    db.session.add(challenge1)
    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    chat_room = ChatRoom.create(
        name="Ein schoener Raum", room_id="Just breathe and let us care about the rest!"
    )

    challenge2 = Challenge(
        challengename="Just breathe and let us care about the rest!",
        description="""

It sounds easy, but yet it can have a great impact on your life. Today, try to find three or more moments to stop and focus on your breath for two minutes.
Why does it matter to us? We want to give something back to society and support you to relief stress and balance your mental health. We are sure it will empower you to take better care of our planet too.
Find out more: link to instruction


Challenge:
3*2 min breathing!

Company supporting you: McKinsey
20 kg of Carbon offset
Equiv. 66km with an average car
        """,
        active=True,
        co2offset=0.02,
        category_id=category3.id,
        company_id=company2.id,
        chat_room=chat_room,
    )
    db.session.add(challenge2)

    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    chat_room = ChatRoom.create(
        name="Ein schoener Raum", room_id="Lower your thermostat by 1° C"
    )
    challenge3 = Challenge(
        challengename="Lower your thermostat by 1° C",
        description="""
Average Swiss household (44m^2 per person): 0.4 kg of CO<sub>2</sub> per heating day

Company supporting you: SmartSolation
10 kg of Carbon offset
        """,
        active=True,
        co2offset=0.1,
        category_id=category4.id,
        company_id=company3.id,
        chat_room=chat_room,
    )
    db.session.add(challenge3)

    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    chat_room = ChatRoom.create(
        name="Ein schoener Raum", room_id="Love your clothesline!"
    )
    challenge4 = Challenge(
        challengename="Love your clothesline!",
        description="""
Wash a load of laundry washed and dry it on a clothesline.
Find out more: https://www.theguardian.com/environment/ethicallivingblog/2008/may/02/treadlightlyswitchofftumbl

CO<sub>2</sub> savings:
1.8 kg of CO<sub>2</sub>
        """,
        active=True,
        co2offset=0.018,
        category_id=category5.id,
        company_id=company3.id,
        chat_room=chat_room,
    )
    db.session.add(challenge4)

    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    chat_room = ChatRoom.create(
        name="Ein schoener Raum", room_id="Food For Thought!"
    )
    challenge5 = Challenge(
        challengename="Food For Thought!",
        description="""
Thanks to the magic of data we have found a large number of sustainable recipes.
These were picked thanks their low CO<sub>2</sub> emissions per serving.
Today we suggest you cook:<br><br>

        """,
        active=True,
        co2offset=0.018,
        category_id=category1.id,
        company_id=company3.id,
        chat_room=chat_room,
    )
    db.session.add(challenge5)

    chat_message = ChatMessage.create(
        user=admin, text="Welcome to the challenge!", room=chat_room
    )

    db.session.flush()

    user_challenge_association11 = User_Challenge_Association(
        normal_user.id,
        challenge1.id,
        succeeded=True,
        done_at=dt.datetime.now() - timedelta(days=13),
        commited_to_at=dt.datetime.now() - timedelta(days=13, hours=1),
    )
    db.session.add(user_challenge_association11)

    user_challenge_association12 = User_Challenge_Association(
        normal_user.id,
        challenge1.id,
        succeeded=True,
        done_at=dt.datetime.now() - timedelta(days=13),
        commited_to_at=dt.datetime.now() - timedelta(days=13, hours=1),
    )
    db.session.add(user_challenge_association12)

    user_challenge_association12 = User_Challenge_Association(
        normal_user.id,
        challenge1.id,
        succeeded=True,
        done_at=dt.datetime.now() - timedelta(days=12),
        commited_to_at=dt.datetime.now() - timedelta(days=12, hours=1),
    )
    db.session.add(user_challenge_association12)

    for i in range(1, 8):
        user_challenge_association12 = User_Challenge_Association(
            normal_user.id,
            challenge1.id,
            succeeded=True,
            done_at=dt.datetime.now() - timedelta(days=i),
            commited_to_at=dt.datetime.now() - timedelta(days=i, hours=1),
        )
        db.session.add(user_challenge_association12)
Esempio n. 7
0
 def test_password_is_nullable(self):
     """Test null password."""
     user = User(username="******", email="*****@*****.**")
     user.save()
     assert user.password is None
Esempio n. 8
0
 def test_created_at_defaults_to_datetime(self):
     """Test creation date."""
     user = User(username="******", email="*****@*****.**")
     user.save()
     assert bool(user.created_at)
     assert isinstance(user.created_at, dt.datetime)