You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from itertools import groupby
|
|
from app import get_db_connection
|
|
|
|
conn=get_db_connection()
|
|
|
|
todos=conn.execute('SELECT c.id, c.content, cat.category_name \
|
|
FROM cards c JOIN categories cat \
|
|
ON c.category_id = cat.id ORDER BY cat.category_name').fetchall()
|
|
|
|
categories={}
|
|
|
|
# for each category and group of cards for each cat in groupby() grouper object
|
|
for k, g in groupby(todos, key=lambda t: t['category_name']):
|
|
cards=[]
|
|
|
|
# for each card get the topic it is assigned to
|
|
for card in g:
|
|
topics = conn.execute('SELECT t.id, t.content FROM topics t \
|
|
JOIN topic_cards t_c \
|
|
ON t.id = t_c.topic_id \
|
|
WHERE t_c.card_id = ?',
|
|
(card['id'],)).fetchall()
|
|
|
|
#convert the row into a disctionary to add topics
|
|
card=dict(card)
|
|
card['topics'] = topics
|
|
print('card is:', card)
|
|
|
|
cards.append(card)
|
|
# print(categories[k])
|
|
|
|
categories[k]=list(cards)
|
|
|
|
|
|
print('THE GROUP category.items() is :', categories.items())
|
|
for cat, cards in categories.items(): # ♥ .items is a build in attribute of the dictionary(?)
|
|
|
|
for card in cards:
|
|
topic_names = [t['content'] for t in card['topics']]
|
|
print('card:', card['content'])
|
|
print(' category:', cat)
|
|
print(' topic:', list(topic_names))
|
|
|
|
conn.close()
|
|
|
|
|
|
# ♥ ♥ ♥ ♥ |