Source

PIGS / default / knowledge / query / set.py

Full commit
from my_PIGS.default.models import *

def create_basic(d):
    knowledge = Knowledge(
        category=Category.objects.get(id=int(d.category)),
        title=d.title,
        brief=d.brief,
        content=d.content,
    )
    knowledge.save()
    if d.parent or d.brother:
        p = Knowledge.objects.filter(title=d.parent)
        b = Knowledge.objects.filter(title=d.brother)
        if p:
            knowledge.parents.add(p.get())
        if b:
            knowledge.brothers.add(b.get())
    return knowledge.id

def change(d):
    print 'in change'
    knowledge = Knowledge.objects.get(id=int(d.id))
    print 'get k'
    # change #
    knowledge.category=Category.objects.get(id=int(d.category))
    knowledge.title=d.title
    knowledge.brief=d.brief
    knowledge.content=d.content
    knowledge.parents.clear()
    if d.parent!='':
        print 'p cl ed'
        print d.parent
        p = Knowledge.objects.filter(title=d.parent)
        if p:
            knowledge.parents.add(p.get())
    if d.brother!='':
        knowledge.brothers.clear()
        b = Knowledge.objects.filter(title=d.brother)
        if b:
            knowledge.brothers.add(b.get())
    knowledge.save()
    return knowledge.id

def delete(k_id):
    k = Knowledge.objects.get(id=int(k_id))
    if k.itsChildren.all():
        status = ''
    else:
        status = k.title
        k.delete()
    return status

def add_parent(d):
    knowledge = create_basic(d,1)
    # brother & parent #
    if d.brother!='':
        print 'hs bro'
        brother = Knowledge.objects.get(id=int(d.brother))
        print brother.id
        knowledge.brothers.add(brother)
        print 'brother added'
        if brother.parents.all():
            knowledge.parents.add(brother.parents.all().get())
        else: print 'no parents'
        print 'ok'
    else:
        print 'basic created'
        parent = Knowledge.objects.get(id=int(d.parent))
        print 'get parent'
        knowledge.parents.add(parent)
        print 'par saved'
    # tips #
    if d.tips!='':
        print 'hs tip'
        tips_ids = (d.tips).split(',') # type: []
        for tip_id in tips_ids:
            tip = Tip.objects.get(id=tip_id)
    knowledge.save()
    print 'well'

def add_tip(k_id,c):
    #first create
    tip = Tip(content=c)
    tip.save()
    #then add to proper Knowledge
    k = Knowledge.objects.get(id=int(k_id))
    k.tips.add(tip)
    return tip.id

def change_tip(t_id,c):
    tip = Tip.objects.get(id=int(t_id))
    tip.content = c
    tip.save()

def remove_tip(t_id):
    tip = Tip.objects.get(id=int(t_id))
    k = tip.inKnowledge.all().get()
    k.tips.remove(tip)
    tip.delete()