Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment 1 and activity 1 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
66 changes: 60 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
import os
import base64

from flask import Flask, render_template, request, redirect, url_for, session
from flask import Flask, render_template, request, redirect, url_for, session,jsonify

from model import Donation
from model import Donation, Donor,Login

app = Flask(__name__)
app.config['SECRET_KEY'] = '123456'


@app.route('/')
def home():
return redirect(url_for('all'))
return render_template('all_donations.jinja2')


@app.route('/login/', methods=['POST','GET'])
def login():
if request.method=='POST':
username = request.form['name']
password = request.form['password']
login_check=Login.select().where(Login.username == username,Login.password==password)
if login_check:
return render_template('base.jinja2')
else:
return render_template('login.jinja2',error='no usrename')
else:
return render_template('login.jinja2')


@app.route('/create/', methods=['POST','GET'])
def create():
if request.method=='POST':
donor = Donor()
donor.name=request.form['name']
donor.save()
donations=Donation()
donations.value = request.form['value']
donations.donor = donor.id
donations.save()
return jsonify({"data":"add success!"})
else:
return render_template('create.jinja2')

@app.route('/donations/')

@app.route('/donations/', methods=['GET', 'POST'])
def all():
donations = Donation.select()
return render_template('donations.jinja2', donations=donations)



@app.route('/donations2/', methods=['GET', 'POST'])
def donations_for():
if request.method == 'GET':
return render_template('all_donations.jinja2')

elif request.method == 'POST':
name = request.form['name']
value = request.form['value']
donations = Donation.select().join(Donor).where(Donor.name == name)

if donations:
donor = Donor()
donor.name = name
donor.save()
donations2 = Donation()
donations2.value = value
donations2.donor = donor.id
donations2.save()
return render_template('donations.jinja2',donations=donations)
else:
return render_template('donations.jinja2', error='wrong!')

if __name__ == "__main__":
port = int(os.environ.get("PORT", 6738))
app.run(host='0.0.0.0', port=port)
app.run(host='0.0.0.0', port=port,debug=True)

12 changes: 11 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@

db = connect(os.environ.get('DATABASE_URL', 'sqlite:///my_database.db'))


class Login(Model):
username = CharField(max_length=255, unique=False)
password = CharField(max_length=255, unique=False)

class Meta:
database = db


class Donor(Model):
name = CharField(max_length=255, unique=True)
name = CharField(max_length=255, unique=False)

class Meta:
database = db


class Donation(Model):
value = IntegerField()
donor = ForeignKeyField(Donor, backref='donations')
Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import random

from model import db, Donor, Donation
from model import db, Donor, Donation,Login

db.connect()

# This line will allow you "upgrade" an existing database by
# dropping all existing tables from it.
db.drop_tables([Donor, Donation])
db.drop_tables([Donor, Donation,Login])

db.create_tables([Donor, Donation])
db.create_tables([Donor, Donation,Login])

user = Login(username='admin',password='123456')
user.save()

alice = Donor(name="Alice")
alice.save()
Expand Down
13 changes: 13 additions & 0 deletions templates/all_donations.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.jinja2' %}

{% block subtitle %}All the things{% endblock subtitle %}

{% block content %}
<ul>
{% for donation in donations %}
<li>
{{ donation.donor.name }} {% if donation.value %}Done on {{ donation.value }} by {{ donation.value }}{% else %} Undone {% endif %}
</li>
{% endfor %}
</ul>
{% endblock content %}
6 changes: 5 additions & 1 deletion templates/base.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
<h1>Donations</h1>
<h2>{% block subtitle %} {% endblock subtitle %}</h2>
{% block content %} {% endblock content %}
<ul>
<li><a href="{{ url_for('home') }}">home</a></li>
<li><a href="{{ url_for('create') }}">new donations</a></li>
</ul>
</body>
</html>
</html>
11 changes: 11 additions & 0 deletions templates/create.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.jinja2' %}

{% block subtitle %}Create a new donor{% endblock subtitle %}

{% block content %}
<form method="POST" action="/create/">
<label for="name-input">Name:</label><input id="name-input" type="text" name="name">
<label for="amount-input">Value:</label><input id="amount-input" type="text" name="value">
<input type="submit" value="Create donor">
</form>
{% endblock content %}
12 changes: 11 additions & 1 deletion templates/donations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
{% block subtitle %}Donations{% endblock subtitle %}

{% block content %}
<form method="POST" action="/donations2/">
<label for="name-input">Name:</label><input id="name-input" type="text" name="name">
<label for="amount-input">Value:</label><input id="amount-input" type="text" name="value">
<input type="submit" value="Create donor">
</form>
<ul>
{% for donation in donations %}
<li><b>{{ donation.donor.name }}</b>: {{ donation.value }}</li>
{% if donation.donor.name %}
<li><b>{{ donation.donor.name }}</b>: {{ donation.value }}</li>
{% else %}
<h1>No Username Exist!!</h1>
{% endif %}

{% endfor %}
</ul>
{% endblock content %}
12 changes: 12 additions & 0 deletions templates/login.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.jinja2' %}

{% block subtitle %}Create a new donor{% endblock subtitle %}

{% block content %}
<form method="POST" action="/login/">
<label for="name-input">UserName:</label><input id="name-input" type="text" name="name">
<label for="amount-input">PassWord:</label><input id="amount-input" type="password" name="password">
<input type="submit" value="Create donor">
</form>
{{ error }}
{% endblock content %}