async def add(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] label = Label.get_or_none(uuid=data) if label is not None: logger.warn( f"{user.name} tried to add a label with an id that already exists." ) return if data["user"] != user.name: logger.warn(f"{user.name} tried to add a label for someone else.") return data["user"] = User.by_name(data["user"]) label = Label.create(**data) for psid in state.get_sids(skip_sid=sid, room=room): if state.get_user(psid) == user or label.visible: await sio.emit("Label.Add", label.as_dict(), room=psid, namespace="/planarally")
def save_label(request): """ Used when a user saves a new label. """ name = request.POST.get('name') query_string = request.POST.get('query_string') # Look for required fields if not name: logging.debug("Name not defined") return HttpResponseBadRequest("Required fields: name, query_string") if not query_string: logging.debug("Query_String not defined") return HttpResponseBadRequest("Required fields: name, query_string") # Look if label name already exists, overwrite it if it does. try: label = Label.objects.get(label_name=name, user=request.user) label.query_string=query_string except Label.DoesNotExist: label = Label(user=request.user, label_name=name, query_string=query_string) label.save() return HttpResponse('/label/'+name)
def init_labels(self): """ set up labels in the DB (if not already there) """ label_defs = { 'dodgy_pr': dict( prettyname="Dodgy PR", description="Statistics, survey results and/or 'research' in this article was sponsored by a PR company.", icon="warn_pr.png"), 'churn': dict(prettyname='Churnalism', description="This article is basically just a press release, copied and pasted.", icon="warn_churn.png"), } session = self.Session() for id,inf in label_defs.iteritems(): label = session.query(Label).get(id) if label is None: label = Label(id=id, prettyname=inf['prettyname'], description=inf['description'], icon=inf['icon']) session.add(label) logging.warn("label '%s' missing from db - now installed", id) else: if label.description != inf['description'] or label.prettyname != inf['prettyname'] or label.icon != inf['icon']: label.prettyname = inf['prettyname'] label.description = inf['description'] label.icon = inf['icon'] logging.warn("Update definition for label '%s'" % (label.id,)) session.commit()
def init_labels(self): """ set up labels in the DB (if not already there) """ label_defs = { 'dodgy_pr': dict( prettyname="Dodgy PR", description= "Statistics, survey results and/or 'research' in this article was sponsored by a PR company.", icon="warn_pr.png"), 'churn': dict( prettyname='Churnalism', description= "This article is basically just a press release, copied and pasted.", icon="warn_churn.png"), 'bad_headline': dict(prettyname='Misleading headline', description="Massively misleading headline", icon="warn_badheadline.png"), 'bogus_data': dict(prettyname='Bogus research', description= "Claims in this article are based on bogus research", icon="warn_generic.png"), 'misrep': dict( prettyname='Misrepresented research', description= "This article misrepresents the research/statistics on which it claims to be based", icon="warn_twisteddata.png"), } session = self.Session() for id, inf in label_defs.iteritems(): label = session.query(Label).get(id) if label is None: label = Label(id=id, prettyname=inf['prettyname'], description=inf['description'], icon=inf['icon']) session.add(label) logging.warn("label '%s' missing from db - now installed", id) else: if label.description != inf[ 'description'] or label.prettyname != inf[ 'prettyname'] or label.icon != inf['icon']: label.prettyname = inf['prettyname'] label.description = inf['description'] label.icon = inf['icon'] logging.warn("Update definition for label '%s'" % (label.id, )) session.commit()
def loadLabelsFromFile(in_file, track_id=track_id): infile = os.path.abspath(os.path.join(os.path.dirname(__file__), in_file)) logReader = csv.reader(open(infile, "rb"), delimiter="\t", quotechar='"') for row in logReader: y = row[0] x = row[1] name = row[2].title() desc = row[3].title() point = "POINT(%s %s)" % (x, y) label = Label(name=name, geom=point, track_id=track_id) label.save() print point, name, track_id
def loadLabelsFromFile(in_file, track_id=track_id): infile = os.path.abspath(os.path.join(os.path.dirname(__file__), in_file)) logReader = csv.reader(open(infile, 'rb'), delimiter="\t", quotechar='"') for row in logReader: y = row[0] x = row[1] name = row[2].title() desc = row[3].title() point = "POINT(%s %s)" % (x, y) label = Label(name=name, geom=point, track_id=track_id) label.save() print point, name, track_id
def create_label(): data = request.get_json() if not 'name' in data or data['name'].strip() == '': return 'Label name is empty', 400 # (Disabled because somehow I do like spaces in my label names..) # if re.match('^[\w]+[\w-]*$', data['name']) is None: # return 'Invalid label name "' + name + '"', 422 label = Label(data['name']) db.session.add(label) db.session.commit() return jsonify(label.to_dict()), 201
async def delete(sid, data): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] location = sid_data["location"] label = Label.get_or_none(uuid=data) if label is None: logger.warn(f"{user.name} tried to delete a non-existing label.") return if label.user != user: logger.warn(f"{user.name} tried to delete another user's label.") return label.delete_instance(True) await sio.emit( "Label.Delete", { "user": user.name, "uuid": data }, skip_sid=sid, namespace="/planarally", )
def searchVideos(): videos = [] #Subject.name ** ('%' + keyword + '%'), myDB.connect() try: record = json.loads(request.data) labels = record['labels'] labelArray = [] for record in labels: oneLabel = Label.select().where((Label.category == record['name']) & (Label.label == record['value'])).get() #commaLabelIds = commaLabelIds + str(oneLabel.labelId) + ',' labelArray.append(str(oneLabel.labelId)) labelArray.sort() csvLabels = ",".join([str(x) for x in labelArray]) #for record in Video.select().where(Video.status == 'N').get(): for record in Video.select().where(Video.labelIds % ('%' + csvLabels + '%')): videos.append({'videoId':record.videoId, 'fileName':record.fileName, 'folderName':record.folderName, 'boxLink':record.boxLink}) #print videos except Exception as e: myDB.close() print e return 'Search Videos Error', 404 if len(videos) == 0: return 'No Videos' return jsonify(data=videos)
def main(): import connection labels = Label.objects(name='_testing') if labels.count(): label = labels.first() else: label = labels.upsert_one(name='_testing') post1 = Page(title="Foo", content="<div>Foo bar</div>", url='https://foo.bar.com', label=label) print(repr(post1)) post1.save() print() post2 = Page(title="Bar", content="<div>Fizz</div>", url='https://fub.buzz.com', label=label) print(repr(post2)) post2.save() print() posts = Page.objects(label=label) print(posts.count()) print(posts) print() print("Deleting") post1.delete() post2.delete()
def saveVideo(): #Saving Video# print 'saveVideo called' try: myDB.connect() record = json.loads(request.data) #Get all the label ids here# labels = record['labels'] videoId = record['videoId'] labelArray = [] #for k,v in sorted(label,key=itemgetter('year')): # print k, v for record in labels: oneLabel = Label.select().where((Label.category == record['name']) & (Label.label == record['value'])).get() #commaLabelIds = commaLabelIds + str(oneLabel.labelId) + ',' labelArray.append(str(oneLabel.labelId)) video = Video.select().where(Video.videoId == videoId).get() labelArray.sort() csvLabels = ",".join([str(x) for x in labelArray]) video.labelIds = csvLabels #commaLabelIds[:-1] video.status = 'Y' video.save() myDB.close() except Exception as e: myDB.close() traceback.print_exc(file=sys.stdout) return 'Save Video Error', 404 return 'Video/Labels saved'
def api_label_insert(): new_label = request.get_json() label = Label(id=new_label['id'], name=new_label['name']) db.session.add(label) db.session.commit() label_json = {"id": label.id, "name": label.name} return jsonify(label_json)
def main(): """ Main command-line function. """ name = 'Testing' label = Label.objects(name=name).upsert_one(name=name) lines = lib.read('var/chrome_onetab.txt') pages = [ dict(zip(('url', 'title'), line.split(' | ', 1))) for line in lines ] for page in pages[:10]: url = page['url'] print(url) resp = requests.get(url) print(resp.status_code, resp.reason) body = resp.text print(len(body)) page_rec = Page( title=page['title'], url=url, content=body, status_code=resp.status_code, label=label, ) page_rec.save() print()
def post_add(): """ Add new post to database. :return: """ if not request.method == 'POST': return MethodNotAllowed title = request.form.get('title') content = request.form.get("content") print "%s %s" % (title, content) try: post = Post.create(title=title, content=content, user=User.get(User.id == 1)) label_texts = request.form.get("labels", "", type=str) if label_texts != "": labels = Label.select().where(Label.text << label_texts.split("_")) for label in labels: labelRelationship = LabelRelationship() labelRelationship.label = label labelRelationship.post = post labelRelationship.save() except IntegrityError as e: return jsonify({"status": 500}) except Exception as e: return jsonify({"status": 500}) res = {"status": 200, "data": post.to_json()} return jsonify(res)
async def delete(sid: str, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) label = Label.get_or_none(uuid=data) if label is None: logger.warn(f"{pr.player.name} tried to delete a non-existing label.") return if label.user != pr.player: logger.warn(f"{pr.player.name} tried to delete another user's label.") return label.delete_instance(True) await sio.emit( "Label.Delete", { "user": pr.player.name, "uuid": data }, room=sid, skip_sid=sid, namespace=GAME_NS, )
def import_labels(filename): with open(filename) as f: all_labels = json.loads(f.read()) labels = all_labels["labels"] for name, value in labels.items(): label = Label(value["label"], name) db.session.add(label) db.session.commit()
def delLabel(request): ''' 删除标签 ''' params = yield from request.json() kw = dict(**params) id = kw['id'] if not id or not id.strip(): result = dict(code=0, msg="id不能为空") else: label = yield from Label.find(id) if not label: result = dict(code=0, msg="没有该标签") else: yield from Label.update2(id=id, is_del=1) result = dict(code=1, msg="标签删除成功") return result
def get_standard_labels(): """ Get the list of standard labels that apply to every repository. @return: the list of standard labels """ labels_dict = load_yaml_from_file("labels") standard_labels = [] for group_info in labels_dict["groups"]: label_names = group_info.pop("labels", []) group = Group(**group_info) for label_info in label_names: label = Label(**label_info, group=group) standard_labels.append(label) for label_info in labels_dict["standalone"]: label = Label(**label_info) standard_labels.append(label) return standard_labels
def __loadLocalization(self): local = None try: local = Localization.get(Localization.name==config.LOCALIZATION) except orm.DoesNotExist: local = Localization.get(Localization.name == config.DEFAULT_LOCALIZATION) labels = Label.select().where(Label.localization==local) for label in labels: self.localization[label.name]=label.text
def addLabelToPatient(request,lbl_name,patient_id): patients_with_id = PatientInfo.objects.filter(pid = patient_id) labels_with_name = Label.objects.filter(label_name = lbl_name) patient = None label = None print "Adding label to Patient" if len(patients_with_id)>0: patient = patients_with_id[0] if len(labels_with_name)>0: label = labels_with_name[0] else: label = Label() label.label_name = lbl_name label.save() label.patients_with_label.add(patient) return HttpResponse()
def addLabel(request): ''' 添加标签 ''' params = yield from request.json() kw = dict(**params) name = kw.get('name', None) if not name or not name.strip(): result = dict(code=0, msg="标签名字不能为空") else: labels = yield from Label.findAll('name=?', [name]) if len(labels) > 0: result = dict(code=0, msg="已经存在该标签名称") else: id = next_id() label = Label(id=id, name=name) yield from label.save() result = dict(code=1, msg="标签添加成功") return result
def addLabelToPatient(request, lbl_name, patient_id): patients_with_id = PatientInfo.objects.filter(pid=patient_id) labels_with_name = Label.objects.filter(label_name=lbl_name) patient = None label = None print "Adding label to Patient" if len(patients_with_id) > 0: patient = patients_with_id[0] if len(labels_with_name) > 0: label = labels_with_name[0] else: label = Label() label.label_name = lbl_name label.save() label.patients_with_label.add(patient) return HttpResponse()
def label_index(): """ Get all labels :return: """ if not request.method == 'GET': return MethodNotAllowed public_labels = (Label.select().order_by(Label.timestamp.desc())) return render_template("bloglist.html", public_labels)
def label_relative_posts(text, page): """ Get relative post with label. :return: """ if not request.method == 'GET': return MethodNotAllowed print "test is %s " % text # page = request.args.get('page', 1, type=int) label = Label.select().where(Label.text == text).get() public_posts = label.get_posts(page) public_labels = Label.select() return render_template("label-index.html", posts=public_posts, labels=public_labels, total=get_label_posts_total_page(text), page=page, label=label)
def editLabel(request): ''' 修改标签 ''' params = yield from request.json() kw = dict(**params) id = kw['id'] name = kw['name'] if not id or not id.strip(): result = dict(code=0, msg="id不能为空") elif not name or not name.strip(): result = dict(code=0, msg="标签名字不能为空") else: label = yield from Label.find(id) if not label: result = dict(code=0, msg="不存在该标签名称") else: yield from Label.update2(id=id, name=name) result = dict(code=1, msg="标签修改成功") return result
def load_images_to_db(path): for dirname, dirnames, filenames in os.walk(path): for subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) label = Label.get_or_create(name=subdirname)[0] label.save() for filename in os.listdir(subject_path): path = os.path.abspath(os.path.join(subject_path, filename)) # logging.info('saving path %s' % path) image = Image.get_or_create(path=path, label=label)[0] image.save()
def get_label_posts_total_page(label): """ Get the page numbsers of label's relative posts. :return: """ labelModel = Label.get(Label.text == label) count = (Post.select().join( LabelRelationship, on=LabelRelationship.post).where( LabelRelationship.label == labelModel).count()) return (count / PER_PAGE_NUM + 1) \ if count % PER_PAGE_NUM != 0 else (count / PER_PAGE_NUM)
def getLabels(): #Getting Labels# print 'getting all the labels' app.logger.info(str(datetime.now()) + '- /getLabels called') labels = [] myDB.connect() for label in Label.select(): labels.append({'category':label.category, 'label':label.label}) print str(labels) myDB.close() return json.dumps(labels)
def parse_metadata(self, metadata, stack): # ignore if not aligned to 0 if metadata.indent != 0: return match = regex_label.match(metadata.line) if match: label = match["label"] self.current_label = Label(label, metadata) parser = ContextParser(self, metadata) stack.push(parser) return
def get_label_id(session, uid, label_text): """ Get the id of the label with the given label text from the database. Adds a new label for the given label text to the database if it's not already there. """ label = session.query(Label).filter_by(uid=uid, label=label_text).first() if not label: session.add(Label(uid=uid, label=label_text)) session.commit() label = session.query(Label).filter_by(uid=uid, label=label_text).first() return label.id
def load_images_from_db(): images, labels = [], [] for label in Label.select(): for image in label.image_set: try: cv_image = cv2.imread(image.path, cv2.IMREAD_GRAYSCALE) if cv_image is not None: cv_image = cv2.resize(cv_image, (100, 100)) images.append(np.asarray(cv_image, dtype=np.uint8)) labels.append(label.id) except IOError, (errno, strerror): print "IOError({0}): {1}".format(errno, strerror)
async def add_filter(sid: str, uuid: str): pr: PlayerRoom = game_state.get(sid) label = Label.get_or_none(uuid=uuid) LabelSelection.create(label=label, user=pr.player, room=pr.room) for psid in game_state.get_sids(skip_sid=sid, room=pr.room): if game_state.get_user(psid) == pr.player: await sio.emit("Labels.Filter.Add", uuid, room=psid, namespace=GAME_NS)
def labelDetail(request, *, id): ''' 获取标签详情 ''' if not id or not id.strip(): result = dict(code=0, msg="id不能为空") else: label = yield from Label.find(id) if not label: result = dict(code=0, msg="没有该标签") else: result = dict(code=1, msg="获取标签成功", list=label) return result
def save_to_db(self): for track_info in self.tracks_info: artist, created = Artist.get_or_create(name=track_info['artist']) if "label" in track_info: label, created = Label.get_or_create(name=track_info['label']) track, created = Track.create_or_get(artist=artist.id, title=track_info['title']) track.label = label.id track.save() else: track, created = Track.create_or_get(artist=artist.id, title=track_info['title']) self.setlist_trackids.append(track.id) return
def saveLabel(): #Saving Label# print 'saveLabel is called' print request.data #print request.form['category'] #print request.form['label'] myDB.connect() record = json.loads(request.data) user = Label.create( category=record['category'], label=record['label'] ) myDB.close() return 'Label saved'
async def add_filter(sid, uuid): sid_data = state.sid_map[sid] user = sid_data["user"] room = sid_data["room"] label = Label.get_or_none(uuid=uuid) LabelSelection.create(label=label, user=user, room=room) for psid in state.get_sids(skip_sid=sid, room=room): if state.get_user(psid) == user: await sio.emit("Labels.Filter.Add", uuid, room=psid, namespace="/planarally")
def get_groups(): """ Get the list of label groups. @return: the list of label groups. """ labels_dict = load_yaml_from_file("labels") groups = [] for group_info in labels_dict["groups"]: group = Group(**group_info) label_names = group_info.pop("labels", []) groups.append(group) for label_info in label_names: Label(**label_info, group=group) return groups
async def add(sid: str, data: Dict[str, Any]): pr: PlayerRoom = game_state.get(sid) label = Label.get_or_none(uuid=data) if label is not None: logger.warn( f"{pr.player.name} tried to add a label with an id that already exists." ) return if data["user"] != pr.player.name: logger.warn(f"{pr.player.name} tried to add a label for someone else.") return data["user"] = User.by_name(data["user"]) label = Label.create(**data) for psid in game_state.get_sids(skip_sid=sid, room=pr.room): if game_state.get_user(psid) == pr.player or label.visible: await sio.emit("Label.Add", label.as_dict(), room=psid, namespace=GAME_NS)
async def remove_filter(sid: str, uuid: str): pr: PlayerRoom = game_state.get(sid) label = Label.get_or_none(uuid=uuid) ls = LabelSelection.get_or_none(label=label, room=pr.room, user=pr.player) if ls: ls.delete_instance(True) for psid in game_state.get_sids(skip_sid=sid, room=pr.room): if game_state.get_user(psid) == pr.player: await sio.emit("Labels.Filter.Remove", uuid, room=psid, namespace=GAME_NS)
def addtodb(page, lookupurl, classes): ''' Add page to MongoDB :param page: info about page :type page: dict :param lookupurl: URL of parent site :type lookupurl: str :param classes: result of classification for given page :type classes: array of ints ''' parent = Site.objects(url=lookupurl).first() nov = Page(url=page['url'], btext=page['btext']) if parent: nov.parent = parent for c in classes: label = Label.objects(id=c).first() nov.label_model.append(label) print "label added" nov.save() add_to_lookup(page['url']) print "Page added to db"
from models import Localization,Label en = Localization() en.name = "RU" en.save() labels = { "read":"Читать", "search":"Поиск", "delete":"Удалить", "delete_ask":"Вы хотите удалить", "add":"Добавить", "edit":"Редактировать", "rating":"Рейтинг", "date":"Дата", "more":"Больше", "less":"Меньше", "cancel":"Отмена", "save":"Сохранить", "title":"Название", "keyword":"Ключевые слова" } label = None for key in labels: label = Label() label.name = key label.text = labels[key] label.localization = en label.save()
if 'grant_type' not in kwargs: kwargs['grant_type'] = 'authorization_code' kwargs['client_id'] = '0gd3ftzthjbr0zu0lsseh8lgp16rwmg2' kwargs['client_secret'] = 'J2OW2LYQsiNIXd7QEwUxfIE9hMZR97PK' kwargs['response_type'] = 'code' #kwargs['redirect_url'] = 'http://localhost:5000/' token_response = requests.post(url, data=kwargs) return token_response.json def build_box_api_url(endpoint, params=''): if params != '': params = '&'.join(['%s=%s' % (k, v) for k, v in params.iteritems()]) url = '%s%s?%s' % (BASE_URL, endpoint, params) return url if __name__ == '__main__': # Bind to PORT if defined, otherwise default to 5000. port = int(os.environ.get('PORT', 5000)) app.debug = False app.secret_key = '12345abcde' myDB.connect() Label.create_table(True) User.create_table(True) Video.create_table(True) Folder.create_table(True) myDB.close() app.run(host='localhost', port=port)
action = query.get('action') if action.endswith('task'): action_method = experiment.add_task if action.endswith('domain'): action_method = experiment.add_domain tag_id = action_method( request.user, query.get('label'), query.get('label_free', None), query.get('parent_id', None)) return redirect('%s#tag-%s' % (request.path, tag_id)) tasks = Label.get_tasks() domains = Label.get_domains() # logging.debug(u"Tasks: %s" % str(tasks)) # logging.debug(u"Domains: %s" % str(tasks)) context = { 'paper': paper, 'tasks': tasks, 'domains': domains, } return render_to_response('paper.html', context, RequestContext(request)) def search(request):