def test_two_texts(session): t1 = Text(text='text1', active=True) t2 = Text(text='text2', active=True) session.add(t1) session.add(t2) session.commit() assert t1.id > 0 assert t2.id > 0
def post(self): selection = self.request.get("selection") selection = selection.lstrip() selection = selection.rstrip() path = self.request.get("path") today = datetime.date.today().isoformat() if selection and len(selection) <= 500: sha = hashlib.sha1(selection.encode('utf-8')).hexdigest() if path: CapturedSelection(text=selection, checksum=sha, path=path).put() else: CapturedSelection(text=selection, checksum=sha).put() text_key = ndb.Key("Text", sha) text_entity = text_key.get() if not text_entity: Text(id=sha, text=selection).put() count_key_id = today + "." + sha count_key = ndb.Key('Summary', count_key_id) count = count_key.get() if count: count.count = count.count + 1 count.put() else: Summary(id=count_key_id, text=selection, checksum=sha, count=1, size=len(selection)).put() headers.set_cors_headers(self.response, host=CORS_HOST)
def write_heading_to_pin(self, txt_img, top_margin=15, fontsize=40, font_index=1): txt_objs = [] for p_h in self.product_header: txt_objs.append( Text(txt=p_h, font_path=None, font_size=fontsize, max_width=txt_img.width, font_index=font_index)) y = min(top_margin, pinproperties.MAX_NUM.value) header_txt_h = 0 for txt_obj in txt_objs: width = txt_obj.txt_width x = (txt_img.width - width) // 2 loc = (abs(x), y) txt_obj.draw_text(img=txt_img, color=pinproperties.FONT_COLOR_HEADER.value, loc=loc) y += txt_obj.max_height header_txt_h += txt_obj.max_height return header_txt_h
def test_single_letter(): text = "a" test_text = Text(text=text) expected_binary = HEADER + bytes(text, 'ascii') + bytes([0x01]) + FOOTER observed_binary = BinaryTextEncoder.serialize(test_text) assert observed_binary == expected_binary
def add_text(): """ Route for adding new texts to the database It stores the text itself and sends id of the stored text for further processing in the redis queue :return: JSON {text=1} where 1 is id of the stored text """ if request.is_json: content = request.get_json() if content['text']: # Store newly added text to the database text = Text(text=content['text'].strip()) db.session.add(text) db.session.commit() # Push extraction job to the queue with Connection( redis.from_url( os.getenv('REDISTOGO_URL', app.config['REDIS_URL']))): q = Queue() job = q.enqueue_call(func=process_worker, args=(text.id, ), result_ttl=3000) return jsonify(id=text.id, text=text.text) else: raise Exception("Please add text variable to the json") else: raise Exception( "Please add content-type application/json to the request")
def test_longer_text(): text = "test" test_text = Text(text=text) expected_binary = HEADER + bytes.fromhex( '74 01 65 01 73 01 74 01') + FOOTER observed_binary = BinaryTextEncoder.serialize(test_text) assert observed_binary == expected_binary
def parse_metadata(self, metadata, stack): if self.indent is None: self.indent = metadata.indent if metadata.indent < self.indent: self.close(metadata, stack) return self.script.add_line(Text(metadata.line, metadata))
def texts(note_id=None, id=None): if request.method == 'GET': if id is not None and note_id is not None: text = Text.query.filter_by(note_id=note_id, id=id).first() if text: return jsonify(text.serialize()), 200 else: return jsonify({"text": "Not found"}), 404 elif id is not None: text = Text.query.get(id) if text: return jsonify(text.serialize()), 200 else: return jsonify({"text": "Not found"}), 404 elif note_id is not None: texts = Text.query.filter_by(note_id=note_id).all() texts = list(map(lambda text: text.serialize(), texts)) return jsonify(texts), 200 else: texts = Text.query.all() texts = list(map(lambda text: text.serialize(), texts)) return jsonify(texts), 200 if request.method == 'POST': text = Text() text.date = request.json.get('date') text.time = request.json.get('time') text.content = request.json.get('content') text.url = request.json.get('url') text.note_id = request.json.get('note_id') db.session.add(text) db.session.commit() return jsonify(text.serialize()), 201 if request.method == 'PUT': text = Text.query.get(id) text.content = request.json.get('content') text.url = request.json.get('url') text.note_id = request.json.get('note_id') db.session.commit() return jsonify(text.serialize()), 200 if request.method == 'DELETE': text = Text.query.get(id) db.session.delete(text) db.session.commit() return jsonify({'text': 'Deleted'}), 200
def count(message): Text(title=f'{message.from_user.username}', type='comment', text=message.text, date=time.strftime("%y.%m.%d (%H:%M:%S)")).save() bot.send_message( 180856655, text= f'Новое сообщение от @{message.from_user.username}:\n{message.text}') bot.send_message(message.chat.id, text=f'Сообщение успешно отправлено 👍') User.set_state(message.from_user.id, 0)
def create_argument(node, arg_type='n', title="", text="", authors=()): arg_type = Argument.short_arg_type(arg_type) arg = Argument(arg_type=arg_type, title=title) arg.node_type = Node.ARGUMENT arg.concerns = node arg.save() text_obj = Text(node=arg, text=text) text_obj.save() for author in authors: text_obj.authors.add(author) text_obj.save() return arg
def create_textNode(long_title, text="", authors=()): text_node = Node() text_node.node_type = Node.STRUCTURE_NODE text_node.title = long_title text_node.save() text_obj = Text() text_obj.node = text_node text_obj.text = text text_obj.save() for author in authors: text_obj.authors.add(author) text_obj.save() return text_node
def addtext(): form = AddTextForm() if form.validate_on_submit(): title = form.title.data text = form.text.data new_text_item = Text(title=title, body=text) db.session.add(new_text_item) db.session.commit() return redirect(url_for("viewtext", text_id=new_text_item.id)) return render_template("addform.html", form=form)
def seed_db(): text1 = Text( text= "Hello world, Lorem Ipsum dolar sit amet, Detected new table, docker compose" ) db.session.add(text1) text2 = Text(text="Hello. I am grut. No no no. Covid - 19") db.session.add(text2) text3 = Text( text="Hello.I am grut. No no no. Covid - 20. Covidka. Covid-1984") db.session.add(text3) db.session.commit() with Connection( redis.from_url(os.getenv('REDISTOGO_URL', app.config['REDIS_URL']))): q = Queue() q.enqueue_call(func=process_worker, args=(text1.id, ), result_ttl=3000) q.enqueue_call(func=process_worker, args=(text2.id, ), result_ttl=3000) q.enqueue_call(func=process_worker, args=(text3.id, ), result_ttl=3000)
def create_text(corpus, text_name, content, debug=False): text = Text(text_name=text_name, corpus=corpus) text.save() tokens = tokenize(text, content) try: coll_text = CollText.objects.get(corpus__id=corpus.id) except CollText.DoesNotExist: # Coll text does not exist, use it as base text. coll_text = init_coll_text(corpus, tokens) text.is_base = True text.save() return text collate(coll_text, tokens, debug=debug) return text
def save_text(): header = request.form.get('header').strip() text = request.form.get('text').strip() level = request.form.get('level') translation = request.form.get('translation').strip() # TODO: ADD VALIDATORS TO A FORM new_text = Text(header=header, text=text, translation=translation, level=level, user_id=current_user.id) db.session.add(new_text) db.session.commit() return redirect(url_for('index'))
def get_text(self, url): try: task_id = self.request.id url_text = extract_text_from_html(url) text = Text(url, str(task_id), url_text) session.add(text) session.commit() return "" except Exception as e: return "ERROR " + str(e)
def parse_metadata(self, metadata, stack): if self.indent is None: self.indent = metadata.indent if metadata.indent < self.indent: self.close(metadata, stack) return match = regex_jump.match(metadata.line) if match: label = match["label"] jump = Jump(label, metadata) self.context.add_child(jump) return match = regex_call.match(metadata.line) if match: label = match["label"] call = Call(label, metadata) self.context.add_child(call) return match = regex_script.match(metadata.line) if match: script_code = match["script"] script = Script(script_code, metadata) self.context.add_child(script) return match = regex_condition.match(metadata.line) if match: parser = ConditionParser(self, metadata) stack.push(parser) stack.parse_metadata(metadata) return match = regex_menu.match(metadata.line) if match: parser = MenuParser(self, metadata) stack.push(parser) return match = regex_python_block.match(metadata.line) if match: parser = PythonScriptParser(self, metadata) stack.push(parser) return self.context.add_child(Text(metadata.line, metadata))
def post(self, request, key): data = json.loads(request.raw_post_data) text = Text(text=data['text'], approvals=[]) if 'approvals' in data: for approval in data['approvals']: if approval: text.approvals.append(approval) if 'password' in data: # TODO: hash text.password = data['password'] text.put() return self._render_to_json_response(self._text2dict(text))
def text(): text = request.form.get('text', None) if not text: flash('没有文本,请重试') # 将信息存入数据库 text = Text(content=text) try: db.session.add(text) db.session.commit() except Exception as e: logger.error(e) return redirect(url_for('index')) return redirect(url_for('index'))
def create_structureNode(long_title, text="", authors=(), validate=False): if validate and not valid_title.match(long_title): raise ValueError('Invalid title "{}"'.format(long_title)) if validate: head = general_heading.match(text) if head is not None: raise ValueError('Headings are not allowed in text: {}'.format( head.group())) structure = Node(node_type=Node.STRUCTURE_NODE, title=long_title) structure.save() text_obj = Text(node=structure, text=text) text_obj.save() for author in authors: text_obj.authors.add(author) text_obj.save() return structure
def tianjia(): if session.get('session_name'): #这个也是判断看用户是否登录,若未登录则不能添加案例,直接跳转到登录页面 if request.method == 'GET': return render_template('tianjia.html') else: title = request.form.get('title') content = request.form.get('content') user_name = session.get('session_name') user = User.query.filter(User.name == user_name).first() text = Text(title=title, content=content) #固定格式,还没搞懂意思 text.author = user db.session.add(text) db.session.commit() return redirect(url_for('anli')) else: return redirect(url_for('denglu'))
def new(): text = Text() session.add(text) payload = json.loads(request.data) for pl in payload: sentence = Sentence(pl['sentence'], pl['language'], pl['order'], pl['translated'], text) words = pl['words'] for w in words: word = session.query(Word).filter_by(base_form=w['base_form'], language=w['language']).first() if not word: word = Word(w['base_form'], w['language']) sentence.words.append(word) seen_word = session.query(SeenWord).filter_by(seen_form=w['seen_form'], language=w['language']).first() if not seen_word: seen_word = SeenWord(w['seen_form'], w['language'], word) session.add(seen_word) session.add(sentence) session.commit() output = {'Message': 'Sentences saved in database'} return json.dumps(output)
def post_text(): if not request.is_json: return jsonify({"msg": "No json request"}), 400 user_id = request.json.get('user_id', None) text = request.json.get('text', None) if not text: return jsonify({"msg": "No text inserted"}), 400 if get_jwt_identity() == User.query.filter_by(id=user_id).first().username: try: t = Text(user_id=user_id, text=text) db.session.add(t) db.session.commit() except Exception as e: print(e) return jsonify({"msg": "error texting"}), 400 else: return jsonify({"msg": "error"}), 400 return jsonify({"msg": "text created succesfully"}), 200
def test_simple_text(session): t = Text(text='test text', active=True) session.add(t) session.commit() assert t.id > 0
def test_no_text(): test_text = Text(text="") expected_binary = bytes(HEADER + FOOTER) observed_binary = BinaryTextEncoder.serialize(test_text) assert observed_binary == expected_binary
def test_unicode(): text = "€" test_text = Text(text=text, active=True, created=datetime.now()) with pytest.raises(UnicodeEncodeError, match=r".* ordinal not in range.*"): BinaryTextEncoder.serialize(test_text)
def store_text(self, query, key): text = Text(key=key, total_expected=self.get_num_texts_expected(query)) text.save() return text