Skip to content

Commit

Permalink
Fix 500 during contact search; add comments and format files
Browse files Browse the repository at this point in the history
  • Loading branch information
ms committed Sep 10, 2022
1 parent 89f45e6 commit 1b51a0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

app = Flask(__name__)

# WARNING: Do not put your secret key in the code for real applications!
# Use an environment variable instead to keep it secret
app.secret_key = b'hypermedia rocks'


Expand Down Expand Up @@ -96,7 +98,8 @@ def contacts_edit_get(contact_id=0):
@app.route("/contacts/<contact_id>/edit", methods=["POST"])
def contacts_edit_post(contact_id=0):
c = Contact.find(contact_id)
c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
c.update(request.form['first_name'], request.form['last_name'],
request.form['phone'], request.form['email'])
if c.save():
flash("Updated Contact!")
return redirect("/contacts/" + str(contact_id))
Expand Down Expand Up @@ -163,7 +166,8 @@ def json_contacts_view(contact_id=0):
@app.route("/api/v1/contacts/<contact_id>", methods=["PUT"])
def json_contacts_edit(contact_id):
c = Contact.find(contact_id)
c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
c.update(request.form['first_name'], request.form['last_name'],
request.form['phone'], request.form['email'])
if c.save():
return c.__dict__
else:
Expand Down
9 changes: 6 additions & 3 deletions contacts_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# ========================================================
PAGE_SIZE = 100


class Contact:
# mock contacts database
db = {}
Expand All @@ -34,7 +35,8 @@ def update(self, first, last, phone, email):
def validate(self):
if not self.email:
self.errors['email'] = "Email Required"
existing_contact = next(filter(lambda c: c.id != self.id and c.email == self.email, Contact.db.values()), None)
existing_contact = next(filter(
lambda c: c.id != self.id and c.email == self.email, Contact.db.values()), None)
if existing_contact:
self.errors['email'] = "Email Must Be Unique"
return len(self.errors) == 0
Expand Down Expand Up @@ -72,7 +74,7 @@ def all(cls, page=1):
def search(cls, text):
result = []
for c in cls.db.values():
if text in c.first or text in c.last or text in c.email or text in c.phone:
if (c.first != None and text in c.first) or (c.last != None and text in c.last) or (c.email != None and text in c.email) or (c.phone != None and text in c.phone):
result.append(c)
return result

Expand All @@ -82,7 +84,8 @@ def load_db(cls):
contacts = json.load(contacts_file)
cls.db.clear()
for c in contacts:
cls.db[c['id']] = Contact(c['id'], c['first'], c['last'], c['phone'], c['email'])
cls.db[c['id']] = Contact(
c['id'], c['first'], c['last'], c['phone'], c['email'])

@staticmethod
def save_db():
Expand Down

0 comments on commit 1b51a0a

Please sign in to comment.