Example #1
0
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 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
Example #3
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)
Example #4
0
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
Example #5
0
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")
Example #6
0
	def _update(self, key):
		user = self.user()
		doc = self._postdata()
		if not key:
			info("adding new text")
			text = Text(owner=user)
			try:
				del doc['old_content']
			except KeyError: pass
		else:
			info("updating text with key=%r, title=%s" % (key,doc.get('title', None)))
			text = Text.find(owner=user, key=key)

		if text is None:
			raise HttpError(404, "no such textarea to edit!")
		try:
			if doc['old_content'] != text.content:
				raise HttpError(409, "existing content does not match datastore")
			logging.info("old_content matched datastore")
		except KeyError:
			logging.info("no old_content given - assuming no conflict")
		text.content = doc['content']
		text.title = doc['title']
		text.expanded = doc.get('expanded', True)
		text.save()
		return text
Example #7
0
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
Example #8
0
	def delete(self, key):
		text = Text.find(owner=self.user(), key=key)
		if text:
			text.delete()
			self._render()
		else:
			info("could not find text" % (key,))
			raise HttpError(404, "could not find text")
Example #9
0
    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))
Example #10
0
def cached_model(text_id, update = False):
	key = 'm' + str(text_id)
	model = cache.get(key)
	if model is None or update:
		logging.error("Model DB Query")
		text = get_text(text_id)
		content = text.content
		model = Text.generatemodel(content)
		cache.set(key, model)
	return model
Example #11
0
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
Example #12
0
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)
Example #13
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
Example #14
0
def text():
    form = TextForm()
    if form.is_submitted():
        if form.validate_on_submit():
            new_text = Text.create_text_from_form(form)
            store(new_text)
            flash("Saved")
        else:
            for _field, error_messages in form.errors.items():
                for error_message in error_messages:
                    flash(error_message)
    return render_template("text.html", form=form)
Example #15
0
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)
Example #16
0
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
Example #17
0
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
Example #18
0
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)
Example #19
0
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'))
Example #20
0
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)
Example #21
0
    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))
Example #22
0
    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))
Example #23
0
def approve_text():
    if 'text_id' not in request.form:
        return redirect('/', code=302)
    text_id = int(request.form.get('text_id'))

    try:
        text = Text.get(Text.id == text_id)
    except Exception:
        return redirect('/', code=302)

    if text.review.exists():
        return redirect('/', code=302)

    Review.create(text=text)
    return redirect('/', code=302)
Example #24
0
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'))
Example #25
0
async def set_text_value(sid: str, data: TextUpdateData):
    pr: PlayerRoom = game_state.get(sid)

    if not data["temporary"]:
        shape: Text = Text.get_by_id(data["uuid"])
        shape.text = data["text"]
        shape.save()

    await sio.emit(
        "Shape.Text.Value.Set",
        data,
        room=pr.active_location.get_path(),
        skip_sid=sid,
        namespace=GAME_NS,
    )
Example #26
0
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'))
Example #27
0
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
Example #28
0
async def update_text_size(sid: str, data: TextSizeData):
    pr: PlayerRoom = game_state.get(sid)

    if not data["temporary"]:
        shape = Text.get_by_id(data["uuid"])

        shape.font_size = data["font_size"]
        shape.save()

    await sio.emit(
        "Shape.Text.Size.Update",
        data,
        room=pr.active_location.get_path(),
        skip_sid=sid,
        namespace=GAME_NS,
    )
Example #29
0
 def tokenize(self, query):
     self.format = "default"
     if query[0] == "@":
         self.tokenize_default(query)
     else:
         key = self.extract_key(query)
         text = Text.objects(key=key).first()
         if not text:
             text = self.store_text(query, key)
             text.text_parts.append(self.store_text_part(query))
             text.save()
             self.partial_text = True
         else:
             text.text_parts.append(self.store_text_part(query))
             text.save()
             if text.total_expected == len(text.text_parts):
                 self.extract_tokens(self.form_text(text))
             else:
                 self.partial_text = True
Example #30
0
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)
Example #31
0
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
Example #32
0
	def get(self, key):
		self._render(Text.find(self.user(), key))
Example #33
0
 def get(self, request):
     return self._render_to_json_response([
         self._text2dict(text) for text in
         Text.query(Text.user == users.get_current_user())
         .order(-Text.updated_at).fetch(100)
     ])
Example #34
0
def add(request):

	if not request.user.is_authenticated():
		next = '/login/?next=/add'
		if 'HTTP_REFERER' in request.META:
			if '/signup' in request.META.get('HTTP_REFERER'):
				next = '/signup/?next=/add'
		return redirect(next)

	def render_form(content="", title="", author="", error="", quote = "", text_id=""):
		# include text author, titles, and IDs for dropdown menu
		texts = text_info()
		return render(request, "generator/add.html", {'content': content, 'title': title, 'author': author,
			'error': error, 'quote': quote, 'text_id': text_id, 'texts': texts})
	if request.method == 'POST':
		# Generate text based on user's input (left side of form)
		if 'generate' in request.POST:
			content = request.POST.get('content')
			author = request.POST.get('author')
			title = request.POST.get('title')
			text_id = request.POST.get('text_id')
			if len(content) < 500:
				error = ("The text you submitted is only {number} character{pl} long.  Please \
					submit a longer text.".format(number = len(content), pl = 's' if len(content)!=1 else ""))
				return render_form(error = error)
			# use a Markov chain to generate a random output based on the input text
			MINLENGTH = 50
			if text_id:
				model = cached_model(text_id)
				quote = Text.generatequote(content, MINLENGTH, model)
			else:
				quote = Text.generatequote(content, MINLENGTH)
			return render_form(content = content, title = title, author = author, quote = quote, text_id = text_id)

		# Save user's text and quotations (right side of form)
		else:
		 	content = request.POST.get('content')
			author = request.POST.get('author')
			title = request.POST.get('title')
			quote = request.POST.get('quote')
			text_id=request.POST.get('text_id')
			
			if text_id:
				text = get_text(text_id)
			else:
				text = Text.objects.create_text(content, title, author, request.user)
				# update cache
				get_text(text.pk, True)
				text_info(True)
			quote = Quotation.objects.create_quotation(quote, request.user, text)
			text_quotes(text, True)
			user_quotes(request.user, True)
			all_quotes(True)
			return redirect('/objects')
	else:
		# generate from existing text
		if request.GET.get('t'):
			text_id=request.GET.get('t')
			text = get_text(text_id)
			content = text.content
			author = text.author
			title = text.title
		# blank form for adding new text
		else:
			content = ""
			text_id = ""
			title = ""
			author = ""
		return render_form(content, title, author, text_id=text_id)
Example #35
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
Example #36
0
	def get(self):
		self._render(Text.find_all(self.user()))