def new(db): """ Add a new tag, text is passed as query parameter. :return: on success new tag object is returned on error 'msg' gives reason message """ try: # Check if parameter is set if 'text' not in request.json: raise ValueError("Missing text parameter") text = request.json['text'] # Check if a tag with same tag already exists available_tags = Tag.query_find_all(db) if text in [t.text for t in available_tags]: raise ValueError("Tag already exists") # create tag if not already present tag = Tag(text) db.add(tag) db.commit() # returns new tag response.content_type = "application/json; charset=UTF-8" return tag_schema.dumps(tag).data except Exception as e: process_error(e)
def list(db): """ Search for all tags in TAG table using query. :return: on success 'items' contains a list of all tags on error 'msg' gives reason message """ try: available_tags = Tag.query_find_all(db) response.content_type = "application/json; charset=UTF-8" return { 'items': tag_schema.dump(available_tags, many=True).data, } except Exception as e: process_error(e)
from frontend.models.sqlobjects import Tag from api.common.sessions import session_transaction from lib.common.utils import decode_utf8 if len(sys.argv) != 2: print("usage: {0} <tag_list> (comma separated)".format(sys.argv[0])) sys.exit(1) # get tag list as argument tag_list = sys.argv[1] # split comma separated list tags = tag_list.split(",") # force all tags to lowercase tags = map(lambda x: decode_utf8(x.lower()), tags) with session_transaction() as session: # get all existing tags existing_tags = Tag.query_find_all(session) existing_text = [t.text for t in existing_tags] # filter only the one needed to be created to_create_tags = filter(lambda x: x not in existing_text, tags) print u"[+] Tags already existing: {0}".format(",".join(existing_text)) for tag in to_create_tags: t = Tag(tag) print u"[+] creating Tag: {0}".format(tag) session.add(t)
def test001___init__(self): text = "whatever" t = Tag(text=text) self.assertEqual(t.text, text)
def test003_query_find_all(self): m_session = MagicMock() Tag.query_find_all(m_session) m_session.query().all.assert_called_once()
def test002_to_json(self): text = "whatever" t = Tag(text=text) expected = {'text': text} self.assertEqual(t.to_json(), expected)
from frontend.models.sqlobjects import Tag from api.common.sessions import session_transaction from irma.common.utils import decode_utf8 if len(sys.argv) != 2: print("usage: {0} <tag_list> (comma separated)".format(sys.argv[0])) sys.exit(1) # get tag list as argument tag_list = sys.argv[1] # split comma separated list tags = tag_list.split(",") # force all tags to lowercase tags = map(lambda x: decode_utf8(x.lower()), tags) with session_transaction() as session: # get all existing tags existing_tags = Tag.query_find_all(session) existing_text = [t.text for t in existing_tags] # filter only the one needed to be created to_create_tags = filter(lambda x: x not in existing_text, tags) print u"[+] Tags already existing: {0}".format(",".join(existing_text)) for tag in to_create_tags: t = Tag(tag) print u"[+] creating Tag: {0}".format(tag) session.add(t)