def _fetch(self, song: QueuedSong): with self.bot.session_scope() as session: _tag = Tag.get(song.fetch_data, song.channel.guild.id, session) random_entry = _tag.get_random_entry() song.stream = io.BytesIO(random_entry.ByteContent) song.volume = _tag.Volume
def top(cls, tag=None, **kwargs): if tag is not None: kwargs['tag_id'] = Tag.get(name=tag)['_id'] expenses = sorted(cls.all(**kwargs), key=lambda x: float(x['cost']), reverse=True) return [ x.json for x in expenses[:5] ]
def update_tag(tag_id, meta): tag = Tag.get(id=tag_id) if (tag.meta or {}).get("admin", False): api_assert(current_user.is_admin, "Tag can only be modified by admin") return Tag.update(id=tag_id, fields={"meta": meta}, skip_if_value_none=True)
async def raw(self, ctx, name: clean_content): """raw tag data""" with self.bot.session_scope() as session: t = Tag.get(name, ctx.guild.id, session) msg = f"==== {t.Name} ====\n\n" for entry in t.entries.all(): msg += entry.TextContent await self.bot.sendc(ctx, fmt.box(msg))
async def volume(self, ctx, name: clean_content, vol): """adjust the volume of a sound tag (WIP)""" self.bot.log.info(f"set volume of {name} to {vol} from {ctx.guild.id}") with self.bot.session_scope() as session: if not Tag.exists(name, ctx.guild.id, session): raise NerpyException("tag doesn't exist!") with self.bot.session_scope() as session: _tag = Tag.get(name, ctx.guild.id, session) _tag.Volume = vol await self.bot.sendc(ctx, f"changed volume of {name} to {vol}")
def create_or_update_tag(tag_name, commit=True, session=None): tag = Tag.get(name=tag_name, session=session) if not tag: tag = Tag.create({"name": tag_name, "count": 1}, commit=commit, session=session) else: tag = Tag.update( id=tag.id, fields={"count": tag.count + 1}, commit=commit, session=session, ) return tag
def progress(self): from models.expense import Expense from models.expectation import Expectation expenses = Expense.all(user_id=self['_id']) savings = [ x for x in expenses if Tag.get(_id=x['tag_id'])['name'] == 'saving' ] expectation = Expectation.get(user_id=self['_id']) total_savings = sum(float(x['cost']) for x in savings) return int((float(expectation['cost']) - total_savings * 100) / float(expectation['cost']))
def delete(tag_name): if not Tag.exists(tag_name): return error_view(404, "tag not found") tag = Tag.get(tag_name) tag.delete() tag_links = TagDnIP.list_from_tag(tag.name) for t in tag_links: t.delete() return tag_deleted_view(tag)
async def add(self, ctx, name: clean_content, *content: clean_content): """add an entry to an existing tag""" with self.bot.session_scope() as session: if not Tag.exists(name, ctx.guild.id, session): raise NerpyException("tag doesn't exists!") async with ctx.typing(): with self.bot.session_scope() as session: _tag = Tag.get(name, ctx.guild.id, session) self._add_tag_entries(session, _tag, content) self.bot.log.info(f"added entry to tag {ctx.guild.name}/{name}.") await self.bot.sendc(ctx, f"Entry added to tag {name}!")
def tag(tag_id): """ Problem list by tag view. :param tag_id: int :return: HTTP Response """ tag = Tag.get(tag_id=tag_id)[0] problems = ProblemTag.get_problems_with_tag(tag) for i in range(len(problems)): problems[i].get_tags() return render_template('tag.html', tag=tag, problems=problems)
def admin_add_problem(): """ Add problem, input and tag view. :return: HTTP Response """ if request.method == 'POST': problem = Problems(problem_name=request.form['problem_name'], statement=request.form['statement'], contest_id=request.form['contest_id'], max_score=request.form['max_score']) problem.save() inp = Input(problem_id=problem.problem_id, testcase=request.form['sample'], expected_output=request.form['sampleout']) inp.save() for i in range(1, 30): if request.form.get('addinp' + str(i), '') and request.form.get( 'expout' + str(i), ''): inp = Input(problem_id=problem.problem_id, testcase=request.form['addinp' + str(i)], expected_output=request.form['expout' + str(i)]) inp.save() tags = [] for i in range(1, 30): if request.form.get('tagin' + str(i), ''): tag_name = request.form['tagin' + str(i)] tag = Tag.get(tag_name=tag_name) if tag: tag = tag[0] else: tag = Tag(tag_name=tag_name) tag.save() tags.append(tag) if tags: ProblemTag.save_tags_to_problem(problem, tags) return redirect(url_for('admin.admin_home')) else: contests = Contest.get_all() return render_template('admin_add_problem.html', contests=contests)
async def _send(self, ctx, tag_name): self.bot.log.info(f"{ctx.guild.name} requesting {tag_name} tag") with self.bot.session_scope() as session: _tag = Tag.get(tag_name, ctx.guild.id, session) if _tag is None: raise NerpyException("No such tag found") if TagType(_tag.Type) is TagType.sound: if ctx.author.voice is None: raise NerpyException("Not connected to a voice channel.") if not ctx.author.voice.channel.permissions_for( ctx.guild.me).connect: raise NerpyException( "Missing permission to connect to channel.") # song = QueuedSong(self.bot.get_channel(606539392319750170), _tag_volume, self._fetch) song = QueuedSong(ctx.author.voice.channel, self._fetch, tag_name) await self.bot.audio.play(ctx.guild.id, song) else: random_entry = _tag.get_random_entry() await self.bot.sendc(ctx, random_entry.TextContent)
async def info(self, ctx, name: clean_content): """information about the tag""" with self.bot.session_scope() as session: t = Tag.get(name, ctx.guild.id, session) await self.bot.sendc(ctx, fmt.box(str(t)))