forked from areeburrub/flask-contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
183 lines (155 loc) · 5.14 KB
/
app.py
File metadata and controls
183 lines (155 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from flask import Flask, redirect, url_for, render_template, request, flash
from models import db, Contact, Note
from forms import ContactForm
from sqlalchemy import desc
from filters import datetimeformat
import secrets
# Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = 'my secret'
app.config['DEBUG'] = False
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book.sqlite'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root@localhost/book'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
app.jinja_env.filters['datetimeformat'] = datetimeformat
@app.route("/")
def index():
'''
Home page
'''
return redirect(url_for('contacts'))
@app.route("/contact/<string:uid>")
def contact(uid):
contact = Contact.query.filter_by(uid=uid).first()
Notes = Note.query.filter_by(to=uid).order_by(desc(Note.date)).all()
if (contact):
return render_template('web/contact.html',Notes=Notes, contact=contact)
else:
flash('No Contact with ('+uid+') found','danger')
return redirect(url_for('contacts'))
@app.route("/remark", methods=('GET', 'POST'))
def remark():
if (request.method == 'POST'):
to = request.form.get('to')
text = request.form.get('remark')
new_note = Note(
rid=secrets.token_urlsafe(8),
to = to,
text = text
)
db.session.add(new_note)
db.session.commit()
return redirect(url_for('contact',uid=to))
else:
return 'This URL doesen\'t exist'
@app.route("/remark/delete/<string:rid>")
def delremark(rid):
rem = Note.query.filter_by(rid=rid).first()
to = rem.to
db.session.delete(rem)
db.session.commit()
return redirect(url_for('student',uid=to))
@app.route("/csv", methods=['GET','POST'])
def csvread():
if (request.method == 'POST'):
csvf = request.files['file']
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(csvf.filename)
filename = random_hex + f_ext
file_path = os.path.join(app.root_path, 'static/csv', filename)
csvf.save(file_path)
with open(file_path, newline='') as csvfile:
read = csv.DictReader(csvfile)
for row in read:
name = row['first_name']
surname = row['last_name']
email = row['email']
phone = row['phone']
new_contact = Contact(
name = name,
surname = surname,
email = email,
phone = phone
)
db.session.add(new_contact)
db.session.commit()
flash('Contacts added successfully', 'success')
return redirect(url_for('contacts'))
else:
return render_template('web/csv.html')
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if form.validate_on_submit():
my_contact = Contact()
form.populate_obj(my_contact)
db.session.add(my_contact)
try:
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
@app.route("/edit_contact/<id>", methods=('GET', 'POST'))
def edit_contact(id):
'''
Edit contact
:param id: Id from contact
'''
my_contact = Contact.query.filter_by(id=id).first()
form = ContactForm(obj=my_contact)
if form.validate_on_submit():
try:
# Update contact
form.populate_obj(my_contact)
db.session.add(my_contact)
db.session.commit()
# User info
flash('Saved successfully', 'success')
except:
db.session.rollback()
flash('Error update contact.', 'danger')
return render_template(
'web/edit_contact.html',
form=form)
@app.route("/contacts")
def contacts():
'''
Show alls contacts
'''
contacts = Contact.query.order_by(Contact.name).all()
return render_template('web/contacts.html', contacts=contacts)
@app.route("/search")
def search():
'''
Search
'''
name_search = request.args.get('name')
all_contacts = Contact.query.filter(
Contact.name.contains(name_search)
).order_by(Contact.name).all()
return render_template('web/contacts.html', contacts=all_contacts)
@app.route("/contacts/delete", methods=('POST',))
def contacts_delete():
'''
Delete contact
'''
try:
mi_contacto = Contact.query.filter_by(id=request.form['id']).first()
db.session.delete(mi_contacto)
db.session.commit()
flash('Delete successfully.', 'danger')
except:
db.session.rollback()
flash('Error delete contact.', 'danger')
return redirect(url_for('contacts'))
if __name__ == "__main__":
app.run()