From efd060c2a208a83f6ad08a2496c0db137416730f Mon Sep 17 00:00:00 2001 From: Luyao Xu Date: Mon, 13 Jul 2020 20:25:59 -0700 Subject: [PATCH 1/2] Initial commit --- .DS_Store | Bin 0 -> 6148 bytes main.py | 40 +++++++++++++++++++++++++++++---- model.py | 12 +++++++++- setup.py | 9 +++++--- templates/all_donations.jinja2 | 13 +++++++++++ templates/base.jinja2 | 6 ++++- templates/create.jinja2 | 11 +++++++++ templates/donations.jinja2 | 12 +++++++++- templates/login.jinja2 | 12 ++++++++++ 9 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 .DS_Store create mode 100644 templates/all_donations.jinja2 create mode 100644 templates/create.jinja2 create mode 100644 templates/login.jinja2 diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..257d3f91c26c3b3bc827059b034739ac6e1f3790 GIT binary patch literal 6148 zcmeHK%}T>S5dNk{Xz|jc$32P%OWz=rco+HtN-M3<(17VZPvJB89KMDx<2SoQ>6+#$ zqBAi2ZRTfp^Ce_50Az7k&w(j`F;#Ii;c!OOuiBH5S=1@I#%?oTuJ(1)i*&+EI??a#}mWExz`03yf zTL5A~a}>_Cm(ZMGSQ>T->7htUC8kud7DG}x?ZM(o!!BV;hgkC=wzF7=B5`-FA7VJ9 zOz5LC;0)vpT%^mX*8ltA{y$IhD`&tN_)`o>Q7x+lZ^>$F@8-1D2I?bKMdP}J#}rOt gDQ2ve;%jOY+JjDrrD2zl7K;A}L>hc>27Z)*FTpBDvH$=8 literal 0 HcmV?d00001 diff --git a/main.py b/main.py index a7b6794..d3b12f4 100644 --- a/main.py +++ b/main.py @@ -3,19 +3,51 @@ from flask import Flask, render_template, request, redirect, url_for, session -from model import Donation +from model import Donation, Donor app = Flask(__name__) @app.route('/') def home(): - return redirect(url_for('all')) + return redirect(url_for('all_donations')) -@app.route('/donations/') + +@app.route('/create/', methods=['Get','POST']) +def create(): + if request.method == 'POST': + donor = Donor() + donor.donor(name=request.form['name']) + value = Donor(value=request.form['value']) + donor.save() + value.save() + else: + return render_template('donations.jinja2') + + +@app.route('/donations/', methods=['GET', 'POST']) def all(): donations = Donation.select() return render_template('donations.jinja2', donations=donations) - + +@app.route('/donations/', methods=['GET', 'POST']) +def donations_for(name): + if request.method == 'GET': + return redirect(url_for('all_donations')) + + elif request.method == 'POST': + session['donor_name'] = request.form['name'] + session['donor_amount'] = request.form['value'] + donations = Donation.select().join(Donor).where(Donor.name == name) + + if donations is True: + create() + return redirect(url_for('all_donations')) + + return render_template('create.jinja2', error="Incorrect donor name.") + + else: + return render_template('donations.jinja2', donations=donations) + if __name__ == "__main__": port = int(os.environ.get("PORT", 6738)) diff --git a/model.py b/model.py index baa7e50..1459905 100644 --- a/model.py +++ b/model.py @@ -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') diff --git a/setup.py b/setup.py index 8854a12..8967d44 100644 --- a/setup.py +++ b/setup.py @@ -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() diff --git a/templates/all_donations.jinja2 b/templates/all_donations.jinja2 new file mode 100644 index 0000000..c177f98 --- /dev/null +++ b/templates/all_donations.jinja2 @@ -0,0 +1,13 @@ +{% extends 'base.jinja2' %} + +{% block subtitle %}All the things{% endblock subtitle %} + +{% block content %} +
    + {% for donation in donations %} +
  • + {{ donation.donor.name }} {% if donation.value %}Done on {{ donation.value }} by {{ donation.value }}{% else %} Undone {% endif %} +
  • + {% endfor %} +
+{% endblock content %} \ No newline at end of file diff --git a/templates/base.jinja2 b/templates/base.jinja2 index f10648b..f03e478 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -6,5 +6,9 @@

Donations

{% block subtitle %} {% endblock subtitle %}

{% block content %} {% endblock content %} + - + \ No newline at end of file diff --git a/templates/create.jinja2 b/templates/create.jinja2 new file mode 100644 index 0000000..31f70a3 --- /dev/null +++ b/templates/create.jinja2 @@ -0,0 +1,11 @@ +{% extends 'base.jinja2' %} + +{% block subtitle %}Create a new donor{% endblock subtitle %} + +{% block content %} +
+ + + +
+{% endblock content %} diff --git a/templates/donations.jinja2 b/templates/donations.jinja2 index 9e68916..f1dd600 100644 --- a/templates/donations.jinja2 +++ b/templates/donations.jinja2 @@ -3,9 +3,19 @@ {% block subtitle %}Donations{% endblock subtitle %} {% block content %} +
+ + + +
    {% for donation in donations %} -
  • {{ donation.donor.name }}: {{ donation.value }}
  • + {% if donation.donor.name %} +
  • {{ donation.donor.name }}: {{ donation.value }}
  • + {% else %} +

    No Username Exist!!

    + {% endif %} + {% endfor %}
{% endblock content %} diff --git a/templates/login.jinja2 b/templates/login.jinja2 new file mode 100644 index 0000000..b3e783d --- /dev/null +++ b/templates/login.jinja2 @@ -0,0 +1,12 @@ +{% extends 'base.jinja2' %} + +{% block subtitle %}Create a new donor{% endblock subtitle %} + +{% block content %} +
+ + + +
+{{ error }} +{% endblock content %} From 98a8bd3cc1926add4eba05b1af0828a94fb05f20 Mon Sep 17 00:00:00 2001 From: Luyao Xu Date: Wed, 15 Jul 2020 00:43:06 -0700 Subject: [PATCH 2/2] change --- main.py | 70 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index d3b12f4..c6eda4e 100644 --- a/main.py +++ b/main.py @@ -1,27 +1,46 @@ 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, Donor +from model import Donation, Donor,Login app = Flask(__name__) +app.config['SECRET_KEY'] = '123456' + @app.route('/') def home(): - return redirect(url_for('all_donations')) + 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=['Get','POST']) +@app.route('/create/', methods=['POST','GET']) def create(): - if request.method == 'POST': + if request.method=='POST': donor = Donor() - donor.donor(name=request.form['name']) - value = Donor(value=request.form['value']) + donor.name=request.form['name'] donor.save() - value.save() + donations=Donation() + donations.value = request.form['value'] + donations.donor = donor.id + donations.save() + return jsonify({"data":"add success!"}) else: - return render_template('donations.jinja2') + return render_template('create.jinja2') @app.route('/donations/', methods=['GET', 'POST']) @@ -29,27 +48,30 @@ def all(): donations = Donation.select() return render_template('donations.jinja2', donations=donations) -@app.route('/donations/', methods=['GET', 'POST']) -def donations_for(name): + +@app.route('/donations2/', methods=['GET', 'POST']) +def donations_for(): if request.method == 'GET': - return redirect(url_for('all_donations')) + return render_template('all_donations.jinja2') elif request.method == 'POST': - session['donor_name'] = request.form['name'] - session['donor_amount'] = request.form['value'] + name = request.form['name'] + value = request.form['value'] donations = Donation.select().join(Donor).where(Donor.name == name) - if donations is True: - create() - return redirect(url_for('all_donations')) - - return render_template('create.jinja2', error="Incorrect donor name.") - - else: - return render_template('donations.jinja2', donations=donations) - + 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)