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

Rock Saharai Cante #66

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Rock Saharai Cante #66

wants to merge 11 commits into from

Conversation

scantea
Copy link

@scantea scantea commented May 13, 2021

No description provided.

@scantea scantea closed this May 13, 2021
@scantea scantea reopened this May 13, 2021
Copy link

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic job Saharai! You met all the learning goals and did so many cool enhancements. I had a lot of fun reading your code and seeing you experiment with lambda functions, helper methods/functions, and sorting! I challenge you to continue using these in future projects.

I've added a few comments about refactoring. Feel free to let me know if you ever revisit this project and would like another review in the future 👀 .

Overall, great job on this project. Keep up the hard work!

Comment on lines +33 to +37
from .routes import task_bp
from .routes import goal_bp
app.register_blueprint(task_bp)
app.register_blueprint(goal_bp)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine imports like so:

Suggested change
from .routes import task_bp
from .routes import goal_bp
app.register_blueprint(task_bp)
app.register_blueprint(goal_bp)
from .routes import task_bp, goal_bp
app.register_blueprint(task_bp)
app.register_blueprint(goal_bp)

Comment on lines +4 to +7
from app.models.task import Task
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... Table, Column, etc. should all be coming from your db instance of sqlalchemy from __init__.py. So I don't think you need the sqlalchemy imports in this file as it's a little redundant.

Comment on lines +25 to +40
if self.goal_id == None:
task_dict={
"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
else:
task_dict={
"id": self.task_id,
"goal_id": self.goal_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
return task_dict

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Great job Saharai. You could refactor this into a single dictionary like so:

Suggested change
if self.goal_id == None:
task_dict={
"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
else:
task_dict={
"id": self.task_id,
"goal_id": self.goal_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
return task_dict
task_dict={
"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()
}
if self.goal_id:
task_dict['goal_id'] = self.goal_id
return task_dict

Comment on lines +15 to +37
def get_tasks():

title_query = request.args.get("title")
if title_query:
tasks = Task.query.filter_by(title=title_query)

else:
tasks = Task.query.all()

tasks_response = []
for task in tasks:
tasks_response.append(task.to_json())

if "asc" in request.full_path:
sort_tasks=sorted(tasks_response, key = lambda i: i['title'])
return jsonify(sort_tasks)

elif "desc" in request.full_path:
sort_tasks=sorted(tasks_response, key = lambda i: i['title'], reverse=True)
return jsonify(sort_tasks)

else:
return jsonify(tasks_response)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful sort by title function!

Comment on lines +44 to +47
if all(keys in request_body for keys in ("title","description","completed_at"))== False:
return {
"details": "Invalid data"
},400

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever way of checking for values in the request body !

Comment on lines +66 to +71
task = Task.query.get(task_id)

if task is None:
return make_response ("",404)
else:
return {"task":task.to_json()},200

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refine this function into a fewer lines by using Flask's own get_or_404() method

Suggested change
task = Task.query.get(task_id)
if task is None:
return make_response ("",404)
else:
return {"task":task.to_json()},200
task = Task.query.get_or_404(task_id)
return {"task":task.to_json()},200

Comment on lines +111 to +121
def mark_incomplete(task_id):
task = Task.query.get(task_id)
if task is None:
return make_response ("",404)

task.completed_at=None
db.session.commit()

return {
"task": task.to_json()
}, 200

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this function is really clean!

Comment on lines +134 to +150
url = f"https://slack.com/api/chat.postMessage?channel=task-notifications&text=Someone just completed the task {task.title}"



token=os.environ.get("bot_user_token")


headers_dict={"Authorization":token}
response = requests.request("POST", url, headers=headers_dict)
return {
"task": {
"id": task.task_id,
"title": task.title,
"description":task.description,
"is_complete": task.is_complete()
}
}, 200

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job in getting your slack bot to work. A future refactor can include putting this logic into a helper function.

Comment on lines +173 to +178
sort_goals=sorted(goals_response, key = lambda i: i['title'])

return jsonify(sort_goals)

elif "desc" in request.full_path:
sort_goals=sorted(goals_response, key = lambda i: i['title'], reverse=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOVING the use of sorted() and the lambda function 🔥

Comment on lines +275 to +292
goal=Goal.query.get(goal_id)
task = Task.query.get(goal_id)


if not goal:
return make_response("",404)
else:

outer_dict=goal.goal_to_json()
if task==None:
outer_dict['tasks']=[]
else:
inner_dict=task.to_json()

inner_dict["goal_id"]=goal.goal_id
outer_dict['tasks']=[inner_dict]
db.session.commit()
return outer_dict,200

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! This was a very clean way of getting the tasks for a specific goal_id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants