def check_ready(): global ready if len(ready) > 0 and time.time() - ready_countdown > 4: users = [] print("Ready!!") for r in ready: print ("%s ready" % r) u = User.query.filter_by(username=r).first() users.append(u) avg = sum(map(lambda x: x.level, users)) // len(users) + 1 cave_name, init, chest, enemies = cavegen.generate_cave([0,1], avg) cave = Location(name=cave_name, init_pos="%s,%s" % (init[0],init[1])) chest = Entity(name="chest",type="c",health=1,location=cave.id, position="%s,%s"%(chest[0],chest[1])) db.session.add(cave) db.session.commit() for ex,ey,name in enemies: print(ex,ey,name) print() e = Entity(name=name,type="e",health=avg,location=cave.id, position="%s,%s" % (ex,ey)) db.session.add(e) for u in users: u.location = cave.id u.position = cave.init_pos ready = [] db.session.commit()
def create_player(cid, uid): """Create a player character entity.""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( chronicle_id=cid, type="character", category="player", title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) # current_user.p_characters.append(entity) db.session.commit() # print("\n\nPC PC PC PC PC") # pprint(entity.to_dict()) return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def load_entities_from_oral_histories(): oral_histories = OralHistory.query.all() for oh in oral_histories: entity = Entity(name=full_name(oh)) entity.description = "Oral History ID: {}".format(oh.id) db.session.add(entity) db.session.commit()
def add_new_entity(): form = AddNewEntityForm() if form.validate_on_submit(): if Entity.query.filter_by(name=form.name.data).one_or_none(): flash('entity with that name already exists', 'error') else: entity = Entity(name=form.name.data, description=form.description.data) db.session.add(entity) db.session.flush() entity_id = entity.id entity_name = entity.name wikipedia_page_title = form.wikipedia_page_title.data if wikipedia_page_title: # if the user entered a url, just get the page title if wikipedia_page_title.startswith('http'): wikipedia_page_title = wikipedia_page_title.split('/')[-1] ws = WikipediaSuggest(entity_id=entity_id, wikipedia_page_title=wikipedia_page_title, confirmed=True) db.session.add(ws) wikipedia_added = True else: wikipedia_added = False db.session.commit() if wikipedia_added is True: flash('added entity {} (id {}) (with Wikipedia link)'.format(entity_name, entity_id), 'info') else: flash('added entity {} (id {})'.format(entity_name, entity_id), 'info') return render_template('experimental/add_new_entity.html', form=form)
def save(): # app.logger.debug(request.data) jsonData = json.loads(request.data) if 'Event-Name' in request.headers: save_event_data(request) if 'optOut' in jsonData and jsonData['optOut']: return get_entities() entity = None data = jsonData['entity'] data["ip"] = request.remote_addr data["edit_type"] = None if data['id']: entity = Entity.query.get(data['id']) elif data['name']: data["edit_type"] = "create" entity = Entity(data['name']) db.add(entity) db.commit() if entity: if not data["edit_type"]: data["edit_type"] = "update" update(entity, data) return get_entities()
def install_data(): for setting in default_settings: db.session.add(setting) for i, (name, url) in enumerate(pages, 1): page = Page(name=name, url=url, template=u'page.html', position=i) db.session.add(page) for i, (name, page_id, parent_id) in enumerate(menu, 1): menuitem = MenuItem(name=name, page_id=page_id, parent_id=parent_id, position=i) db.session.add(menuitem) sec1html = u'<p>Content for pages can be written in the administration panel' \ u' under Content -> Pages or Content -> Sections.</p>' section = Section(name=u'Introduction', html=sec1html, page_id=1, template=u'section.html', position=1) db.session.add(section) section = Section(name=u'Members', html=u'', page_id=2, template=u'members.html', position=1) db.session.add(section) mem1desc = u'<p>Information about team members can be written in the administration panel' \ u' under Content -> Entities.</p><p>Entities with role assigned as Member will be shown here.</p>' member = Entity(name=u'Team Member 1', description=mem1desc, role=u'Member', position=1) db.session.add(member) member = Entity(name=u'Team Member 2', description=u'', role=u'Member', position=2) db.session.add(member) db.session.commit()
def createPair(players): entity = Entity() db.session.add(entity) db.session.commit() for player in players: db.session.add(EntityParticipant(entity_id = entity.id, player_id = player)) db.session.commit()
def create_entity(node): entity = Entity(node['name']) entity.nickname = node['nickname'] entity.entitytype = node['type'] entity.influence = node['influence'] if entity.influence: entity.influence = entity.influence.capitalize() entity.employees = node['employees'] entity.url = node['url'] entity.twitter_handle = node['twitter_handle'] entity.followers = node['followers'] categorize(entity, node['categories']) add_finances(entity, node['revenue'], 'revenue') add_finances(entity, node['expenses'], 'expenses') #add_relations(entity, node['relations']) connect_key_people(entity, node['key_people']) db.add(entity) db.flush() add_location(entity, node['location']) old_to_new[node['ID']] = entity.id
def admin_index_view(): stats = {'all_active_users': User.count(), 'today_active_users': User.today_all_active_users(), 'all_chats': Chat.count(), 'today_chats': Chat.today_new_count(), 'all_messages': sum(u.msg_count for u in UserStat.all()), 'today_messages': Message.today_all_count(), 'last_chats': Chat.last_chats_list()} return render_template('admin/index.html', stats=stats, entities=Entity.generate_list()[0], format_time=format_time)
def create_entity(node): entity = Entity(node['name']) entity.nickname = node['nickname'] if node['description']: entity.description = node['description'] entity.entitytype = node['type'] entity.influence = node['influence'] if entity.influence: entity.influence = entity.influence.capitalize() entity.employees = node['employees'] entity.url = node['url'] entity.twitter_handle = node['twitter_handle'] entity.followers = node['followers'] categorize(entity, node['categories']) add_finances(entity, node['revenue'], 'revenue') add_finances(entity, node['expenses'], 'expenses') #add_relations(entity, node['relations']) connect_key_people(entity, node['key_people']) db.add(entity) db.flush() add_location(entity, node['location']) old_to_new[node['ID']] = entity.id
def create_entity(entity): """Create a new entity""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( user=current_user, type=entity, category=form["category"].data, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) db.session.commit() return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def createPlayer(name, slackUser, password, username): if not name or not password or not username: raise ValueError('Invalid input when creating a new player') player = Player(name = name, slack_user = slackUser, password = password, username = username) db.session.add(player) db.session.commit() entity = Entity() db.session.add(entity) db.session.commit() entityParticipant = EntityParticipant(entity_id = entity.id, player_id = player.id) db.session.add(entityParticipant) db.session.commit()
def save(): app.logger.debug('SAVING') app.logger.debug(request.data) entity = None data = json.loads(request.data)['entity'] if data['id']: entity = Entity.query.get(data['id']) elif data['name']: app.logger.debug('ADDING NEW ENTITY ' + str(data['name'])) entity = Entity(str(data['name'])) db.add(entity) db.commit() if entity: app.logger.debug('UPDATING ENTITY ' + entity.name) update(entity, data) cache.clear() else: app.logger.debug('NO UPDATE') return get_entities()
def save(): app.logger.debug('SAVING') app.logger.debug(request.data) entity = None data = json.loads(request.data)['entity'] data["ip"] = request.remote_addr data["edit_type"] = None if data['id']: entity = Entity.query.get(data['id']) elif data['name']: app.logger.debug('ADDING NEW ENTITY ' + data['name']) data["edit_type"] = "create" entity = Entity(data['name']) db.add(entity) db.commit() if entity: if not data["edit_type"]: data["edit_type"] = "update" app.logger.debug('UPDATING ENTITY ' + entity.name) update(entity, data) cache.clear() else: app.logger.debug('NO UPDATE') return get_entities()
from app.models import Article, Entity from app.parser import parse_entities import pandas as pd import os import json import re import requests processed_articles = pd.read_csv("processed_translated_articles.csv") processed_articles['translated_body'] = processed_articles[ 'translated_body'].astype(str) # upload articles into sqlite with app.app_context(): for index, row in processed_articles.iterrows(): print(index) entities = eval(row['entities']) article = Article(title=row['title'], language=row['language'], body=row['body'], translated_body=row['translated_body'], summary=row['summarized_body']) db.session.add(article) db.session.commit() for key, ent_lst in entities.items(): ent_lst = list(set(ent_lst)) for ent_name in ent_lst: ent = Entity(name=ent_name, type=key, origin=article) db.session.add(ent) db.session.commit()
def xmlImport(inputFile): def named_parameters(model): columns = [m.key for m in model.__table__.columns] return columns # Truncate DB Sentence.query.delete() Entity.query.delete() Interaction.query.delete() OntologyAnnotation.query.delete() db.session.commit() tree = ET.parse(inputFile) root = tree.getroot() for sentence in root: sentence_attr = { el.tag: el.text for el in sentence if el.text and el.tag in named_parameters(Sentence) } # Entity entities = [] for entity in sentence.iter("entity"): entity_attr = { el.tag: el.text for el in entity if el.text and el.tag in named_parameters(Entity) } # Ontology Annotation ontologyAnnotations = [] for ontologyAnnotation in entity.iter("ontologyAnnotation"): ontologyAnnotation_attr = { el.tag: el.text for el in ontologyAnnotation if el.text and el.tag in named_parameters(OntologyAnnotation) } ontologyAnnotation_attr["default"] = ( ontologyAnnotation_attr["default"] == 'True') ontologyAnnotations.append( OntologyAnnotation(**ontologyAnnotation_attr)) if not "comment" in entity_attr: entity_attr["comment"] = "" entity_attr["ontologyAnnotations"] = ontologyAnnotations entities.append(Entity(**entity_attr)) # Interaction interactions = [] for interaction in sentence.iter("interaction"): interaction_attr = { el.tag: el.text for el in interaction if el.text and el.tag in named_parameters(Interaction) } if not "comment" in interaction_attr: interaction_attr["comment"] = "" interactions.append(Interaction(**interaction_attr)) sentence_attr["entities"] = entities sentence_attr["interactions"] = interactions if not "comment" in sentence_attr: sentence_attr["comment"] = "" s = Sentence(**sentence_attr) db.session.add(s) db.session.commit()
def nodes(): return Entity.all_as_json()
def group(chat_hash): # Get chat statistics chat_stats = ChatStat.where('hash', chat_hash).limit(21).get() if chat_stats: # Chat cid = chat_stats[0].cid chat = Chat.get(cid) # Chat title chat_title = chat.title # Bot add date, dd.mm.yy add_date = datetime.fromtimestamp(chat_stats[0].last_time).strftime('%d.%m.%y') # Today messages msg_count = chat_stats[-1].msg_count # Today active users active_users = chat_stats[-1].users_count # Last update last_update = datetime.fromtimestamp(chat_stats[-1].last_time).strftime('%d.%m.%y (%H:%M)') # Link for public chats public_link = chat.public_link average_users = 0 chart = {'labels': [], 'msg_values': [], 'users_values': []} # Charts generator i = 0 for chat in chat_stats: average_users += chat.users_count # Dates, dd/mm d = datetime.fromtimestamp(chat.last_time).strftime('%d') chart['labels'].append(str(d)) chart['msg_values'].append(chat.msg_count) chart['users_values'].append(chat.users_count) i += 1 # Average number of users average_users = round(average_users / i) # Generating user list users = [] user_stats = UserStat.where('cid', cid).order_by('msg_count', 'desc').limit(50).get().all() for ustat in user_stats: user = User.get(ustat.uid) users.append({'name': user.fullname, 'msg_count': ustat.msg_count, 'uid': ustat.uid, 'public': user.public}) # Generating entities entities = {'total': 0, 'photo': 0, 'audio': 0, 'video': 0, 'document': 0, 'url': 0, 'hashtag': 0, 'bot_command': 0, 'mention': 0} urls = [] i = 0 _entities = Entity.where('cid', cid).order_by('count', 'desc').get().all() for entity in _entities: if entity.type == 'voice': entities['audio'] += entity.count else: entities[entity.type] += entity.count entities['total'] += entity.count # Generating urls list if entity.type == 'url' and i < 9: urls.append({'link': entity.title, 'count': entity.count}) i += 1 return render_template('group.html', page_title='{} - Confstat'.format(chat_title), chat_title=chat_title, add_date=add_date, msg_count=msg_count, active_users=active_users, average_users=average_users, chart=chart, users=users, entities=entities, urls=urls, last_update=last_update, public_link=public_link) else: return redirect('/')
def seed_entities(): """Seed assets needed for Demo Tale.""" # assets asset1 = Asset( user_id=1, category="item", title="Red Key", description= "A tiny red key found in the blue room. It probably unlocks red doors, because why not?", color="#e05265ff", icon="key", value=100, ) asset2 = Asset( user_id=1, category="item", title="Candy", description= "Colorful pieces of candy wrapped in crinkly celophane. They look delicious.", color="#964a70ff", icon="candy-cane", value=0.25, ) asset3 = Asset( user_id=1, category="deed", title="Took Candy", color="#964a70ff", description="A candy has been taken.", icon="hand-sparkles", ) asset4 = Asset( user_id=1, category="item", title="Ball", description="A ball from the Sweet Fairy.", color="#8cb15eff", icon="baseball-ball", value=10, ) asset5 = Asset( user_id=1, category="currency", title="Gold", description="The currency of the world of Demolandia.", color="#f0bc62ff", icon="coins", image="close-up-coin-textures.jpg", value=1, ) # places place1 = Entity( user_id=1, type="place", title="The Beginning", description= "There is a red door on the left and a blue door on the right", icon="dungeon", image="vintage-binocular-lens.jpg") place2 = Entity( user_id=1, type="place", title="The Red Room", description= "The room is very red, and there is another red door on the far wall. There is a table with a vase of flowers and a pile of candy.", color="#e05265ff", icon="door-closed", ) place3 = Entity( user_id=1, type="place", title="The Blue Room", description= "The room is very blue, and there is another blue door on the far wall. There is a table with a red key on it.", color="#5a70ccff", icon="door-open", ) place4 = Entity( user_id=1, type="place", title="The Joined Room", description= "Looking back, there is a red and blue door. An angry fairy is in the corner of the room.", color="#4f4686ff", icon="door-open", image="vintage-binocular-lens.jpg", ) # characters character1 = Entity( user_id=1, type="character", category="fairy", title="Sweet Fairy", description="A young fairy with a sweet tooth", color="#885c58ff", icon="user-circle", image= "https://i.vippng.com/png/small/320-3207217_vintage-fairy-pngs-daisy-fairy-cicely-mary-barker.png", ) # statuses status1 = Status( user_id=1, category="mood", title="Angry", description="An angry mood", color="#e05265ff", icon="angry", ) status2 = Status( user_id=1, category="mood", title="Happy", description="A happy mood", color="#f0bc62ff", icon="grin", ) # TODO Figure out how to add assets/etc. to entities via join table # ba1 = BearerAsset( # bearer=character1, # asset=asset4, # ) # ba2 = BearerAsset( # bearer=character1, # asset=condition1, # ) meter1 = Meter( user_id=1, title="Popularity", description="This indicates how well people like you.", color="#f0bc62ff", icon="users", image= "https://previews.123rf.com/images/torky/torky1112/torky111200013/11377065-vintage-heart-of-roses.jpg", min=0, max=10, base=100, mod=10, ) meter2 = Meter( user_id=1, title="Perception", description= "How sharp is your senses? Can you notice when something seems off around you?", color="#1b9d8eff", icon="eye", min=0, max=20, base=10, mod=1, ) meter3 = Meter( user_id=1, title="Killing Arts", description= "Do you have what it takes to become America's most deadly assassin?", color="#1b9d8eff", icon="wrench", min=0, max=50, base=10, mod=2, ) meter4 = Meter( user_id=1, title="Gardening", description="How green is your thumb?", color="#1b9d8eff", icon="seedling", min=0, max=10, base=1000, mod=5, ) # em1 = EntityMeter( # entity=character1, # meter=meter1, # progress=50, # ) db.session.add(asset1) db.session.add(asset2) db.session.add(asset3) db.session.add(asset4) db.session.add(place1) db.session.add(place2) db.session.add(place3) db.session.add(place4) db.session.add(character1) db.session.add(status1) db.session.add(status2) db.session.add(meter1) db.session.add(meter2) db.session.add(meter3) db.session.add(meter4) db.session.commit()
def createBlock(blockLines): headLineComponents = blockLines[0].split("\t") ids = headLineComponents[1].split("__") pmid = ids[0] sentenceID = ids[1] sentence = headLineComponents[2] score = float(headLineComponents[3]) # add Sentence s = Sentence(pubmedID=pmid, sentenceID=sentenceID, literal=sentence, score=score, grade=defaultGrade, comment=defaultComment) db.session.add(s) for line in blockLines[1:]: lineComponents = line.split("\t") # Possible forms: # PROTEIN_EXACT\tstart end\tword\tdatabase id # PROTEIN_GENIA\tstart end\tword\tPROTEIN_REFLECT\tstart end\tword\tdatabase id # PATTERN\tstart end\tinteraction_type try: kind = lineComponents[0] if kind == "PATTERN": start, end = startEnd(lineComponents[1]) type = lineComponents[2] # add Interaction i = Interaction(type=type, start=start, end=end, sentence=s, grade=defaultGrade, comment=defaultComment) db.session.add(i) else: type, software = typeSoftware( kind.replace("GO_PROCESS", "GO-PROCESS").replace("CHEM", "CHEMICAL")) if type.upper() != "PROTEIN" or software.upper() == "EXACT": start, end = startEnd(lineComponents[1]) name = lineComponents[2] databaseID = lineComponents[3] # add Entity e = Entity(type=type, software=software, name=name, databaseID=databaseID, start=start, end=end, sentence=s, grade=defaultGrade, comment=defaultComment) db.session.add(e) else: # partial overlap databaseID = lineComponents[6] type1, software1 = typeSoftware(lineComponents[0]) start1, end1 = startEnd(lineComponents[1]) name1 = lineComponents[2] # add Entity e1 = Entity(type=type1, software=software1, name=name1, databaseID=databaseID, start=start1, end=end1, sentence=s, grade=defaultGrade, comment=defaultComment) db.session.add(e1) type2, software2 = typeSoftware(lineComponents[3]) start2, end2 = startEnd(lineComponents[4]) name2 = lineComponents[5] # add Entity e2 = Entity(type=type2, software=software2, name=name2, databaseID=databaseID, start=start2, end=end2, sentence=s, grade=defaultGrade, comment=defaultComment) db.session.add(e2) except: raise "Error: {}\n\nLine Components: {}".format( sys.exc_info()[0], lineComponents) db.session.commit()
def group(chat_hash): chat = Chat.where('hash', chat_hash).first() if chat: cid = chat.cid # Get chat statistics chat_stats = db.select('(SELECT * FROM chat_stats ' 'WHERE cid = "{}" ' 'ORDER BY id DESC LIMIT 21) ' 'ORDER BY id ASC'.format(cid)) # Chat title chat_title = chat.title # Bot add date, dd.mm.yy add_date = datetime.fromtimestamp(chat.add_time).strftime('%d.%m.%y') # Today messages NOT USED YET today_messages = Message.today_chat_count(cid) # All number of users all_users = Chat.all_chat_users(cid) # Today active users active_users = Chat.today_chat_users(cid) # Last update last_update = datetime.fromtimestamp(chat_stats[-1].last_time).strftime('%d.%m.%y (%H:%M)') # Link for public chats public_link = chat.public_link average_users = 0 chart = {'labels': [], 'msg_values': [], 'users_values': []} # Charts generator i = 0 for chat in chat_stats: average_users += chat.users_count # Dates, dd/mm d = datetime.fromtimestamp(chat.last_time).strftime('%d') chart['labels'].append(str(d)) chart['msg_values'].append(chat.msg_count) chart['users_values'].append(chat.users_count) i += 1 # Average number of users average_users = round(average_users / i) # Generating user list users = [] user_stats = UserStat.where('cid', cid).order_by('msg_count', 'desc').limit(50).get().all() for ustat in user_stats: user = User.get(ustat.uid) users.append({'name': user.fullname, 'msg_count': ustat.msg_count, 'uid': ustat.uid, 'public': user.public}) # Generating entities entities = Entity.generate_list(cid) return render_template('group.html', page_title='{} - Confstat'.format(chat_title), chat_title=chat_title, add_date=add_date, today_messages=today_messages, all_users=all_users, active_users=active_users, average_users=average_users, chart=chart, users=users, entities=entities[0], urls=entities[1], last_update=last_update, public_link=public_link) else: return redirect('/')