def init_db(): import models import helpers engine, db_session = db_connect() models.Base.metadata.create_all(bind=engine) tag_highlight = models.Tag() tag_highlight.name = "Tag 1" tag_highlight.color = "#00aabb" db_session.add(tag_highlight) tag_exclude = models.Tag() tag_exclude.name = "Tag 2" tag_exclude.color = "#bc5a45" db_session.add(tag_exclude) config = models.Config() config.key = "workdir" config.set_value(str(settings.work_dir)) db_session.add(config) db_session.commit() user = models.User() user.username = "******" user.name = "local" user.set_password("!@#$%¨&¨BFGFGBFffglkdfk*") db_session.add(user) db_session.commit()
def add_recipe(recipe_dict): gen_id = generate_random_recipe_id() new_recipe = models.Recipe( id=gen_id, user_id=recipe_dict["user"], title=recipe_dict["title"], description=recipe_dict["description"], difficulty=recipe_dict["difficulty"], instructions=recipe_dict["instructions"], ready_in_minutes=recipe_dict["readyInMinutes"], servings=recipe_dict["servings"], images=recipe_dict["images"], ingredients=recipe_dict["ingredients"], ) for tag_text in recipe_dict["tags"]: tag = db.session.query(models.Tag).filter_by(name=tag_text).first() if tag: new_recipe.tags.append(tag) else: new_tag = models.Tag(name=tag_text) new_recipe.tags.append(new_tag) db.session.add(new_tag) db.session.add(new_recipe) db.session.commit() return gen_id
def post(self, user): name = self.request.get('name') value = value=self.request.get('value') tag = models.Tag(name=name, value=value) tag.put() time.sleep(0.10) return self.redirect('/admin/tag')
def validate_tags(form, field): current_user = form.kwargs.get("current_user", None) if current_user is None: return tags = list() for text in list(field.data): nd = models.Tag(user=current_user, text=text) tags.append(nd) field.data = tags
def test_search_by_tag(self): recipe = models.Recipe(**self.TEST_ADD_RECIPE) tag = models.Tag(**self.TEST_ADD_TAG) recipe.tags.append(tag) db.session.add(models.Levels(difficulty=self.DIFFICULTY)) db.session.add(models.Users(**self.TEST_ADD_USER)) db.session.add(recipe) searched_recipes = db_queries.search_by_tag(self.TEST_ADD_TAG["name"]) self.assertEqual(searched_recipes[0]["title"], self.TEST_ADD_RECIPE["title"])
def tags_form_input(self, tag_string): """Taks tag input field that is a comma separate string and splits them""" input_tags = tag_string.split(",") tag_label = [name.strip() for name in input_tags if name.strip()] tags = models.Tag.select().where(models.Tag.name.in_(tag_label)) updated_label = set(tag_label) - set([tag.name for tag in tags]) new_tags = [models.Tag(name=name) for name in updated_label] return list(tags) + new_tags
def alltags(cn): cn.query('SELECT * FROM tags') tag_rs = cn.store_result() for i in range(tag_rs.num_rows()): r = tag_rs.fetch_row()[0] tag = models.Tag(name=decode(r[1]), type_id=int(r[2]), order_id=int(r[3])) yield tag
def create_or_get_tag(self, tag): db = self.db tmp_tag = db.query(models.Tag).filter(models.Tag.url == tag['url']).first() if not tmp_tag: tmp_tag = models.Tag(**tag) try: db.add(tmp_tag) db.commit() except Exception: db.rollback() return tmp_tag
def append_tag_to_review(type, value, review): tag = models.Tag() tag.type = type try: value = int(value) except: value = 0 if (value > 0): tag.votes_pos = 1 if (value < 0): tag.votes_neg = 1 review.tags.append(tag)
def get_or_create_tag(tag_name): """Return tag with the specified name, creating it if it doesn't exist.""" tag_name = tag_name.strip().lower() tags = models.Tag.objects.filter(name=tag_name) tag = None if len(tags) == 0: tag = models.Tag() tag.name = tag_name tag.auto = False tag.save() else: tag = tags[0] return tag
def tag_create(data): logging.error(data) num = data.get('num') namespace = data.get('namespace') value = data.get('value') tag1 = models.Tag.get_key_by_namespace_and_value(namespace, value) if tag1: logging.info('tag1 namespace and value exists') return common.error_response(None, 400) parent = None if data.get('parentNum'): parentNum = data.get('parentNum') if not isinstance(parent, int): logging.error('parentNum must be int type') return common.error_response(None, 400) parent = models.get_by_num('Tag', parentNum) if not parent: logging.error('parent_num(%d) not found' % parentNum) return common.error_response(None, 400) if num: num = int(num) tag2 = models.get_by_num('Tag', num) if tag2: logging.info('tag2 namespace and value exists') return common.error_response(None, 400) tag = models.Tag(num=num, namespace=namespace, value=value, parent_tag=parent) else: tag = models.Tag(namespace=namespace, value=value, parent_tag=parent) tag.put() tag.num = tag.key().id() tag.put() return common.json_response(tag.format())
def insert_tag(iUUID="", iGalaxy="", iTag="", iType="", iMalwareShortName="", iIsList=False, iList=[]): if not iGalaxy and not iTag and not iList: return session = Session() #TO DO ADD A CHECK MAYBE THIS CAN BE DONE SOME OTHER WAY RATHER THAN INSERTING TABLE OR IS THAT FASTER? # manual_tag_check = get_manual_tags(iMalwareShortName, iGalaxy) try: if iIsList == False: dbInsert = md.Tag() dbInsert.uuid = iUUID dbInsert.galaxy = iGalaxy dbInsert.tag = iTag dbInsert.type = iType session.merge(dbInsert) else: for tag in iList: dbInsert = md.Tag() dbInsert.uuid = tag[0] dbInsert.galaxy = tag[1] dbInsert.tag = tag[2] dbInsert.type = tag[3] session.merge(dbInsert) session.commit() except Exception as error: session.rollback() print("f(x) insert_tag: DATABASE ERROR: {}".format(error)) sys.exit(error) finally: session.close()
def edit_recipe(recipe_id, recipe_dict): recipe = models.Recipe.query.filter_by(id=recipe_id).first() if not recipe: return "No recipe with ID {} in database".format(recipe_id) for key in recipe_dict.keys(): if key == "title": recipe.title = recipe_dict["title"] db.session.commit() elif key == "videos": recipe.videos = recipe_dict["videos"] db.session.commit() elif key == "ingredients": recipe.ingredients = recipe_dict["ingredients"] db.session.commit() elif key == "images": recipe.images = recipe_dict["images"] db.session.commit() elif key == "servings": recipe.servings = recipe_dict["servings"] db.session.commit() elif key == "readyInMinutes": recipe.ready_in_minutes = recipe_dict["readyInMinutes"] db.session.commit() elif key == "instructions": recipe.instructions = recipe_dict["instructions"] db.session.commit() elif key == "difficulty": recipe.difficulty = recipe_dict["difficulty"] db.session.commit() elif key == "description": recipe.description = recipe_dict["description"] db.session.commit() elif key == "tags": recipe.tags = [] db.session.commit() for tag in recipe_dict["tags"]: db_tag = db.session.query( models.Tag).filter_by(name=tag).first() if db_tag: recipe.tags.append(db_tag) db.session.commit() else: new_tag = models.Tag(name=tag) recipe.tags.append(new_tag) db.session.add(new_tag) db.session.commit()
def add_recipe(recipe_dict): gen_id = generate_random_recipe_id() new_recipe = models.Recipe( id=gen_id, user_id=recipe_dict["user"], title=recipe_dict["title"], description=recipe_dict["description"], difficulty=recipe_dict["difficulty"], instructions=recipe_dict["instructions"], ready_in_minutes=recipe_dict["readyInMinutes"], servings=recipe_dict["servings"], images=recipe_dict["images"], ingredients=recipe_dict["ingredients"], user_submitted_images=[], number_of_forks=0, ) if "videos" in recipe_dict.keys(): new_recipe.videos = recipe_dict["videos"] else: new_recipe.videos = [] if 'forked_from_recipe' in recipe_dict.keys(): new_recipe.forked_from_recipe = recipe_dict['forked_from_recipe'] forked_recipe = models.Recipe.query.filter_by( id=recipe_dict['forked_from_recipe']).first() num = forked_recipe.number_of_forks forked_recipe.number_of_forks = num + 1 for tag_text in recipe_dict["tags"]: tag = db.session.query(models.Tag).filter_by(name=tag_text).first() if tag: new_recipe.tags.append(tag) else: new_tag = models.Tag(name=tag_text) new_recipe.tags.append(new_tag) db.session.add(new_tag) db.session.add(new_recipe) db.session.commit() return gen_id
def add_review_tags_to_location(location, review): location_edited = False for rev_tag in review.tags: rev_tag_found = False for loc_tag in location.tags: if rev_tag.type == loc_tag.type: loc_tag.votes_pos += rev_tag.votes_pos loc_tag.votes_neg += rev_tag.votes_neg location_edited = True rev_tag_found = True break if (rev_tag_found == False): loc_tag = models.Tag() loc_tag.type = rev_tag.type loc_tag.votes_pos = rev_tag.votes_pos loc_tag.votes_neg = rev_tag.votes_neg location.tags.append(loc_tag) location_edited = True if (location_edited == True): location.put()
def post(self): document_id = int(self.request.get("document_id")) topic = int(self.request.get("topic")) word_string = self.request.get("word_string").lower() tag = models.Tag() tag.topic = topic tag.word = int(self.request.get("word")) tag.user_id = int(self.request.get("user_id")) tag.document_id = document_id tag.word_string = word_string tag.put() document_stat = models.DocumentStats.gql( "WHERE document_id = %d and topic = %d" % (document_id, topic)).get() if not document_stat: document_stat = models.DocumentStats() document_stat.document_id = document_id document_stat.topic = topic document_stat.count = 1 else: document_stat.count = document_stat.count + 1 document_stat.put() topic_stat = models.TopicStats.gql("WHERE topic = %d and word = '%s'" % (topic, word_string)).get() if not topic_stat: topic_stat = models.TopicStats() topic_stat.topic = topic topic_stat.word = word_string topic_stat.count = 1 else: topic_stat.count = topic_stat.count + 1 topic_stat.put() self.redirect('/?user_id=%s' % self.request.get("user_id"))
def import_from_json(channels, tracks): regex = re.compile(r'#[\w-]+\s') tags = {} tracks_map = {} keys = channels.keys() start = 100 stop = 110 for uid in tqdm(islice(keys, start, stop)): channel = get_channel_from_dict(channels[uid]) channel.save() tracks_list = channels[uid].get('tracks') if not tracks_list: continue for track_id in tqdm(tracks_list.keys()): url = tracks[track_id]['url'] if not url: continue if url in tracks_map.keys(): track = tracks_map[url] else: track = get_track_from_dict(tracks[track_id]) track.save() tracks_map[url] = track liked_timestamp = tracks[track_id].get('created', 0) // 1000 liked_date = datetime.datetime.fromtimestamp( liked_timestamp).date() track_rel = channel.likes.connect(track, {'date': liked_date}) track_body = tracks[track_id].get('body') if not track_body: continue track_rel.body = track_body track_rel.save() for tag_name in regex.findall(track_body): tag_name = tag_name.strip()[1:] # get rid of '#' if tag_name in tags.keys(): tag = tags[tag_name] else: tag = models.Tag(name=tag_name) tag.save() tags[tag_name] = tag track.tags.connect(tag, {'channel': channel.uid}) for uid in tqdm(islice(keys, start, stop)): slug = channels[uid]['slug'] channel = models.Channel.nodes.get_or_none(slug=slug) if not channel: continue followed_ls = channels[uid].get('favoriteChannels') if not followed_ls: continue for followed_id in followed_ls: channel_followed_dict = channels.get(followed_id) if not channel_followed_dict: continue followed_slug = channel_followed_dict['slug'] channel_followed = models.Channel.nodes.get_or_none( slug=followed_slug) if not channel_followed: continue channel.follows.connect(channel_followed)
def commit_to_db(self, post_data: dict): """ *************************************DICT STRUCTURE************************************* url: https://geekbrains.ru/posts/bezopasnost-veb-novyj-fakultativ-ot-hacktory post_title: «Безопасность веб» — новый факультатив от Hacktory post_publish_date: 2020-10-23T11:48:00+03:00 image_url: https://d2xzmw6cctk25h.cloudfront.net/geekbrains/public/ckeditor_assets/pictures/9860/retina-f0d622a24fa84ace868a3e5606fb1c09.png author: Geek Brains author_url: https://geekbrains.ru/users/63 post_tags: ['web', 'программирование'] comments: [] Saving data to BD :param post_data: :return: """ """ *************************************DB STRUCTURE************************************* Post id = Column(Integer, autoincrement=True, primary_key=True) url = Column(String, unique=True, nullable=False) title = Column(String, unique=False, nullable=False) publish_date = Column(Datetime, unique=False, nullable=False) img_url = Column(String, unique=False, nullable=True) author_id = Column(Integer, ForeignKey('author.id')) author = relationship("Author", back_populates='posts') tag = relationship('Tag', secondary=tag_post, back_populates='posts') Writer id = Column(Integer, autoincrement=True, primary_key=True) name = Column(String, unique=False, nullable=False) url = Column(String, unique=True, nullable=False) posts = relationship("Post") Tag id = Column(Integer, autoincrement=True, primary_key=True) name = Column(String, unique=False, nullable=False) url = Column(String, unique=True, nullable=False) posts = relationship('Post', secondary=tag_post) """ author = models.Author(name=post_data['author'], url=post_data['author_url']) author_check = self.db.query( models.Author).filter(models.Author.url == author.url).first() if author_check: author = author_check self.db.add(author) post = models.Post(url=post_data['url'], title=post_data['post_title'], publish_date=post_data['post_publish_date'], img_url=post_data['image_url'], author=author) self.db.add(post) tag = models.Tag(name=post_data['post_tags'][0][0], url=post_data['post_tags'][0][1], posts=[post]) tag_check = self.db.query( models.Tag).filter(models.Tag.url == tag.url).first() if tag_check: tag = tag_check self.db.add(tag) post.tag.append(tag) for itm in post_data['comments']: author = models.Author(name=itm[0], url=itm[1]) author_check = self.db.query( models.Author).filter(models.Author.url == author.url).first() if author_check: author = author_check self.db.add(author) comment = models.Comment(text=itm[2], author=author, posts=post) self.db.add(comment) self.db.commit()
def test_get_field_additions(self): tag = models.Tag(example_tag1, example_tag2) self.assertEquals(tag.get_field_additions(), ['cl2000', '-g', '-z'])
def test_get_field_omissions(self): tag = models.Tag(example_tag1, example_tag2) self.assertEquals(tag.get_field_omissions(), ['lnk2000', '-q', '-c'])
def test_is_shared_tag(self): tag = models.Tag(example_tag1, example_tag2) self.assertEquals(tag.is_shared_tag(), True)
def setup(): root = models.Tag('.root', hidden=True, isFolder=True) untagged = models.Tag('untagged')
#coding:UTF-8 import models as m nana = m.register_user(1, 'nana', m.STUDENT, 71, 'pku', 'rtus') yuri = m.register_user(2, 'yuri', m.STUDENT, 72, 'pku', '43yu') mochi = m.register_user(4, 'mochi', m.STUDENT, 332, 'pku', 'scaa') leader = m.register_user(3, 'leader', m.STAFF, 999, 'pku', 'miho') yuri.new_tag('ameda') nana.new_tag('mochi') leader.new_tag('ablum') tags = [x for x in m.get_tags()] yuri.post_task('no.4', tags=[m.Tag(tagname='ablum')]) nana.post_task('hale', tags=[m.Tag(tagname='mochi')]) leader.post_task('hand', tags=tags) students = list(m.all_users()) staffs = list(m.all_users(m.STAFF)) tasks = list(m.unsolved_tasks()) nana.receive_task(tasks[0]) nana.participate_tasks(m.RECEIVED) for event in yuri.unchecked_events(): yuri.check_event(event) nana.complete_task(tasks[0]) yuri.commit_task(tasks[0], 5)
import models as md descripcion_proyecto = 'Uno de los primeros proyectos de comunicación\ visual en monterrey en ser reconocido de \ manera internacional' tag1 = md.Tag(nombre='Branding').save() tag2 = md.Tag(nombre='Arquitectura').save() mencion proyect = md.Proyecto(titulo='Theurel & Thomas', descripcion=descripcion_proyecto, keywords=[tag1.id, tag2.id])