Commit 4484e496 authored by berumuron's avatar berumuron
Browse files

tec: Refactor items_* methods

parent ca24eba3
......@@ -21,7 +21,7 @@ bp = Blueprint("events", __name__, url_prefix="/events")
@bp.route("/")
def index():
if g.current_user is None:
events = Event.items_where(is_private=False)
events = Event.items(is_private=False)
else:
events = Event.items()
......
......@@ -63,52 +63,20 @@ class Event:
self.project = Project.find_by_id(self.project_id)
@classmethod
def items(cls):
database = db.get_db()
cursor = database.execute("SELECT * FROM events")
event = cursor.fetchone()
while event:
yield cls(
event["id"],
event["title"],
event["date"],
event["date_end"],
event["is_private"],
event["project_id"],
event["description"],
)
def items(cls, **kwargs):
attributes = kwargs.keys()
values = kwargs.values()
where = []
for attribute in attributes:
where.append("{0} = :{0}".format(attribute))
event = cursor.fetchone()
@classmethod
def items_where(cls, is_private):
database = db.get_db()
cursor = database.execute(
"SELECT * FROM events WHERE is_private = ?", (is_private,)
)
event = cursor.fetchone()
while event:
yield cls(
event["id"],
event["title"],
event["date"],
event["date_end"],
event["is_private"],
event["project_id"],
event["description"],
)
event = cursor.fetchone()
@classmethod
def items_by_project(cls, project_id):
database = db.get_db()
cursor = database.execute(
"SELECT * FROM events WHERE project_id = ?", (project_id,)
)
sql = "SELECT * FROM events"
if where:
sql = sql + " WHERE {}".format(" AND ".join(where))
cursor = database.execute(sql, tuple(values))
event = cursor.fetchone()
while event:
yield cls(
......@@ -202,32 +170,23 @@ class Project:
self.description = description
def events(self):
return Event.items_by_project(self.id)
return Event.items(project_id=self.id)
@classmethod
def items(cls):
database = db.get_db()
cursor = database.execute("SELECT * FROM projects")
project = cursor.fetchone()
while project:
yield cls(
project["id"],
project["name"],
project["is_private"],
project["responsible"],
project["description"],
)
def items(cls, **kwargs):
attributes = kwargs.keys()
values = kwargs.values()
where = []
for attribute in attributes:
where.append("{0} = :{0}".format(attribute))
project = cursor.fetchone()
@classmethod
def items_where(cls, is_private):
database = db.get_db()
cursor = database.execute(
"SELECT * FROM projects WHERE is_private = ?", (is_private,)
)
sql = "SELECT * FROM projects"
if where:
sql = sql + " WHERE {}".format(" AND ".join(where))
cursor = database.execute(sql, tuple(values))
project = cursor.fetchone()
while project:
yield cls(
......
......@@ -19,7 +19,7 @@ bp = Blueprint("projects", __name__, url_prefix="/projects")
@bp.route("/")
def index():
if g.current_user is None:
projects = Project.items_where(is_private=False)
projects = Project.items(is_private=False)
else:
projects = Project.items()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment