2022-06-13 14:22:46 +02:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from config import CONFIG
|
2022-06-14 14:47:12 +02:00
|
|
|
from create_db import User, Card, get_session
|
2022-06-13 14:22:46 +02:00
|
|
|
|
2022-06-13 14:25:36 +02:00
|
|
|
from hashlib import md5
|
2022-06-13 14:22:46 +02:00
|
|
|
|
2022-06-23 21:14:00 +02:00
|
|
|
#@TODO najbrž je treba narediti tako da org card tudi dobi share id in se ostalo, kar to obsega
|
2022-06-13 14:22:46 +02:00
|
|
|
def share(card, user_id):
|
2022-06-14 14:47:12 +02:00
|
|
|
dbsession = get_session()
|
|
|
|
|
2022-06-13 14:22:46 +02:00
|
|
|
#tu bi lahko naredili nek autoincrement ampak i guess da hash unique idja tudi daje unique share_id, tega potem uporabljamo, da preverimo matche
|
|
|
|
h = md5(str(card.id).encode("utf-8")).hexdigest()
|
|
|
|
card.share_id = h
|
2022-06-23 21:14:00 +02:00
|
|
|
|
|
|
|
owner_card = dbsession.query(Card).filter(Card.id == card.id).first()
|
|
|
|
owner_card.share_id = h
|
|
|
|
dbsession.commit()
|
2022-06-13 14:22:46 +02:00
|
|
|
|
|
|
|
# all users
|
|
|
|
users = dbsession.query(User).filter(User.id != user_id).all()
|
|
|
|
for user in users:
|
2022-06-14 02:25:00 +02:00
|
|
|
#skip če že ima ta card v db.
|
|
|
|
existing = dbsession.query(Card).filter(Card.title == card.title, Card.owner_id==user.id).all()
|
|
|
|
if existing == []:
|
|
|
|
new_card = card = Card(title=card.title, interest_rate=-1.0, owner_id=user.id, item_location=card.item_location, last_review=None, share_id=h)
|
|
|
|
dbsession.add(new_card)
|
2022-07-14 22:29:37 +02:00
|
|
|
dbsession.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_shared(user_id):
|
|
|
|
"""adds all existing shared cards to this users collection"""
|
|
|
|
dbsession = get_session()
|
|
|
|
|
|
|
|
#get all cards with a shared id but make suer they are unique. add them to collection of user
|
2022-07-15 12:27:24 +02:00
|
|
|
#@TODO A je to resres ok?
|
|
|
|
all_shared_cards = dbsession.query(Card).filter(Card.share_id != "0").distinct(Card.share_id).group_by(Card.share_id)
|
|
|
|
|
2022-07-14 22:29:37 +02:00
|
|
|
for c in all_shared_cards:
|
|
|
|
new_card = Card(title= c.title, interest_rate=-1.0, owner_id=user_id, item_location=c.item_location, last_review=None, share_id=c.share_id)
|
|
|
|
dbsession.add(new_card)
|
2022-10-31 18:22:26 +01:00
|
|
|
dbsession.commit()
|