def get_albums(self, page=1, category='all', order_by='created_at', limit=None): if limit: albums = orm.select(rv for rv in collipa.models.Album if rv.user_id == self.id).order_by(lambda: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5)) return albums[:limit] if category == 'all': albums = orm.select(rv for rv in collipa.models.Album if rv.user_id == self.id) else: albums = orm.select(rv for rv in collipa.models.Album if rv.user_id == self.id and rv.role == category) if order_by == 'created_at': albums = albums.order_by(lambda: orm.desc(rv.created_at)) elif order_by == 'up_count': albums = albums.order_by(lambda: orm.desc(rv.up_count)) elif order_by == 'thank_count': albums = albums.order_by(lambda: orm.desc(rv.thank_count)) elif order_by == 'smart': albums = albums.order_by(lambda: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5)) if page: return albums[(page - 1) * config.paged: page * config.paged] else: return albums
def update_latest_post_urls(ts: datetime, data: [RawNews]) -> None: """ Updates the data about latest posts: If a shop is absent from a newsfeed, nothing changes for entries related to it; otherwise, all the entries related to the shop are deleted, and the new set of entries' added. :param ts: timestamp; :param data: a list of News objects. :return: - """ sources = {} for post in data: src_name, src_mp, text_link = post.parser.name, post.parser.mainpage, post.link if (src_name, src_mp) not in sources: if not Source.exists(name=src_name, newsfeed=src_mp): logging.info("A new source added: name = {}, newsfeed = {}".format(src_name, src_mp)) source = Source(name=src_name, newsfeed=src_mp) else: source = Source.get(name=src_name, newsfeed=src_mp) sources[(src_name, src_mp)] = source old_latest_posts = orm.select(p for p in SiteSnapshot if p.source == source) if old_latest_posts: logging.info("Previous day's news for source '{}' found: {}.".format(src_name, len(old_latest_posts))) for p in old_latest_posts: p.delete() else: source = sources[(src_name, src_mp)] logging.info("Added new post to the database: {}".format(post.link)) SiteSnapshot(source=source, is_latest=True, timestamp=ts, url=text_link) logging.info("Entries in the database: {}".format(len(orm.select(p for p in SiteSnapshot))))
def get_images(self, page=1, category='all', order_by='created_at', limit=None, desc=True): if category == 'all': images = collipa.models.Image.select(lambda rv: rv.album_id == self.id) else: if category == 'hot': images = collipa.models.Image.select(lambda rv: rv.album_id == self.id) limit = 10 order_by = 'smart' elif category == 'author': images = orm.select(rv for rv in collipa.models.Image if rv.topic_id == self.id and rv.user_id == self.user_id) else: images = orm.select(rv for rv in collipa.models.Image if rv.album_id == self.id and rv.role == category) if order_by == 'smart': images = images.order_by(lambda: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5)) else: if desc: images = images.order_by(lambda: orm.desc(rv.created_at)) else: images = images.order_by(lambda: rv.created_at) if limit: return images[:limit] elif page: return images[(page - 1) * config.paged: page * config.paged] else: return images
def checknewscomments(news): orm.sql_debug(False) if not news: with orm.db_session: first_newsitem = orm.select(orm.min(x.id) for x in NewsItem).first() last_newsitem = orm.select(orm.max(x.id) for x in NewsItem).first() newsitem_id = first_newsitem else: i = 0 while True: with orm.db_session: if not news: newsitem = NewsItem.select(lambda x: x.id >= newsitem_id and x.id <= last_newsitem).first() if not newsitem: break else: if i >= len(news): break if news[i].isdigit(): newsitem = NewsItem.get(id=int(news[i])) else: newsitem = NewsItem.get(name=news[i]) i += 1 if not newsitem: print('News item {} not found'.format(news[i - 1])) continue print('News item {} ({})'.format(newsitem.id, newsitem.name)) comments_list = newsitem.bl.select_comments().order_by('c.date, c.id') check_comments_for(newsitem, comments_list) newsitem_id = newsitem.id + 1
def album_list(): ltype = request.values['type'] size, offset = map(request.values.get, [ 'size', 'offset' ]) size = int(size) if size else 10 offset = int(offset) if offset else 0 query = select(t.folder for t in Track) if ltype == 'random': return request.formatter('albumList', dict( album = [ a.as_subsonic_child(request.user) for a in query.without_distinct().random(size) ] )) elif ltype == 'newest': query = query.order_by(desc(Folder.created)) elif ltype == 'highest': query = query.order_by(lambda f: desc(avg(f.ratings.rating))) elif ltype == 'frequent': query = query.order_by(lambda f: desc(avg(f.tracks.play_count))) elif ltype == 'recent': query = select(t.folder for t in Track if max(t.folder.tracks.last_play) is not None).order_by(lambda f: desc(max(f.tracks.last_play))) elif ltype == 'starred': query = select(s.starred for s in StarredFolder if s.user.id == request.user.id and count(s.starred.tracks) > 0) elif ltype == 'alphabeticalByName': query = query.order_by(Folder.name) elif ltype == 'alphabeticalByArtist': query = query.order_by(lambda f: f.parent.name + f.name) else: raise GenericError('Unknown search type') return request.formatter('albumList', dict( album = [ f.as_subsonic_child(request.user) for f in query.limit(size, offset) ] ))
def get_images(self, page=1, category='all', order_by='created_at', limit=None): if limit: images = orm.select(rv for rv in m.Image if rv.user_id == self.id).order_by(lambda rv: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5)) return images[:limit] if category == 'all': images = orm.select(rv for rv in m.Image if rv.user_id == self.id) else: images = orm.select(rv for rv in m.Image if rv.user_id == self.id and rv.role == category) if order_by == 'created_at': images = images.order_by(lambda rv: orm.desc(rv.created_at)) elif order_by == 'up_count': images = images.order_by(lambda rv: orm.desc(rv.up_count)) elif order_by == 'thank_count': images = images.order_by(lambda rv: orm.desc(rv.thank_count)) elif order_by == 'smart': images = images.order_by(lambda rv: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5 + rv.created_at / 4)) if page: return images[(page - 1) * config.paged: page * config.paged] else: return images
def print_userlist(bot, message): with db_session: chats_str = '' for chat in select(chat for chat in Chat): chats_str += u'{}. {} (@{}, {})'.format(chat.primary_id, chat.realname, \ chat.username, chat.group_id) if chat.silent_mode: chats_str += ' (silent mode)' if chat.deleted: chats_str += ' (deleted)' chats_str += '\n' try: send_large_message(bot, message.chat_id, chats_str) except telegram.TelegramError as error: print "TelegramError", error group1_str = u'Группа 1:\n' for chat in select(chat for chat in Chat if chat.group_id == "group1"): group1_str += u'{}. {} (@{})'.format(chat.primary_id, chat.realname, chat.username) if chat.silent_mode: group1_str += ' (silent mode)' if chat.deleted: group1_str += ' (deleted)' group1_str += '\n' try: send_large_message(bot, message.chat_id, group1_str) except telegram.TelegramError as error: print "TelegramError", error
def delete(self, author): from mini_fiction.models import AdminLog from mini_fiction.models import NewsComment, NewsCommentEdit, NewsCommentVote from mini_fiction.models import Subscription, Notification newsitem = self.model news_id = self.model.id # Неявная связь с подписками Subscription.select( lambda x: x.type in ('news_comment',) and x.target_id == news_id ).delete(bulk=True) # Неявная связь с уведомлениями comment_ids = orm.select(x.id for x in NewsComment if x.newsitem.id == news_id) Notification.select( lambda x: x.type in ('news_reply', 'news_comment') and x.target_id in comment_ids ).delete(bulk=True) # Да-да, поне надо помогать с удалением комментов orm.select(c for c in NewsCommentVote if c.comment in newsitem.comments).delete(bulk=True) orm.select(c for c in NewsCommentEdit if c.comment in newsitem.comments).delete(bulk=True) newsitem.comments.select().order_by(NewsComment.id.desc()).delete() AdminLog.bl.create(user=author, obj=newsitem, action=AdminLog.DELETION) newsitem.delete()
def fetch_all_elements( self ): dep_ids = set([dep.dep.id for dep in this_module.Module[self.module_id].deps]) if dep_ids: query = select(rec for rec in this_module.Module if rec.id not in dep_ids) else: query = select(rec for rec in this_module.Module) return list(map(self.rec2element, query.order_by(this_module.Module.id)))
def get_users(page=1, category='all', limit=None): if category == 'all': users = orm.select(rv for rv in User).order_by(lambda: orm.desc(rv.created_at)) elif category == 'hot': users = mc.get('hot_users') if not users: if not limit: limit = 8 users = orm.select(rv for rv in User).order_by(lambda: orm.desc(rv.thank_count * 4 + rv.up_count * 3 + rv.topic_count * 2 + rv.reply_count))[:limit] mc.set('hot_users', list(users), 60 * 60 * 12) elif category == 'new': users = orm.select(rv for rv in User).order_by(lambda: orm.desc(rv.created_at)) else: return [] if limit: return users[:limit] if page: return users[(page - 1) * config.user_paged: page * config.user_paged] else: return users
def get_replies(self, page=1, category='all', order_by='created_at', limit=None): if category == 'all': replies = m.Reply.select(lambda rv: rv.topic_id == self.id) else: if category == 'hot': replies = m.Reply.select(lambda rv: rv.topic_id == self.id) limit = 10 order_by = 'smart' elif category == 'author': replies = orm.select(rv for rv in m.Reply if rv.topic_id == self.id and rv.user_id == self.user_id) else: replies = orm.select(rv for rv in m.Reply if rv.topic_id == self.id and rv.role == category) if order_by == 'smart': replies = replies.order_by(lambda rv: orm.desc((rv.collect_count + rv.thank_count) * 10 + (rv.up_count - rv.down_count) * 5)) else: replies = replies.order_by(lambda rv: rv.created_at) if limit: return replies[:limit] elif page: return replies[(page - 1) * config.reply_paged: page * config.reply_paged] else: return replies
def checkstoryvoting(story_ids): orm.sql_debug(False) if not current_app.story_voting: print('Story voting is disabled.') return if not story_ids: with orm.db_session: first_story = orm.select(orm.min(x.id) for x in Story).first() last_story = orm.select(orm.max(x.id) for x in Story).first() story_id = first_story else: story_ids_queue = story_ids[::-1] # reversed while True: with orm.db_session: if not story_ids: stories = Story.select(lambda x: x.id >= story_id and x.id <= last_story).order_by(Story.id)[:50] if not stories: break else: if not story_ids_queue: break stories = list(Story.select(lambda x: x.id in story_ids_queue[-50:]).order_by(Story.id)) story_ids_queue = story_ids_queue[:-50] if not stories: continue changed_stories = [] for story in stories: print('Story {}:'.format(story.id), end=' ', flush=True) old_count = story.vote_total # TODO: rename to story.vote_count old_value = story.vote_value old_extra = story.vote_extra current_app.story_voting.update_rating(story) new_count = story.vote_total new_value = story.vote_value new_extra = story.vote_extra print('{} -> {}'.format(old_value, new_value), end='', flush=True) if old_count != new_count or old_value != new_value or json.loads(old_extra) != json.loads(new_extra): print(' (changed)') changed_stories.append(story) else: print('') print('Saving...', end=' ', flush=True) for story in changed_stories: story.bl.search_update(update_fields={'vote_total', 'vote_value'}) orm.commit() print('Done.', flush=True) if not story_ids: story_id = stories[-1].id + 1
def get_starred(): folders = select(s.starred for s in StarredFolder if s.user.id == request.user.id) return request.formatter('starred', dict( artist = [ dict(id = str(sf.id), name = sf.name) for sf in folders.filter(lambda f: count(f.tracks) == 0) ], album = [ sf.as_subsonic_child(request.user) for sf in folders.filter(lambda f: count(f.tracks) > 0) ], song = [ st.as_subsonic_child(request.user, request.client) for st in select(s.starred for s in StarredTrack if s.user.id == request.user.id) ] ))
def get_followers(self, page=1): follower_ids = orm.select(rv.who_id for rv in m.Follow if rv.whom_id == self.id).order_by(lambda rv: orm.desc(rv)) or [0] followers = orm.select(rv for rv in User if rv.id in follower_ids) if page: return followers[(page - 1) * config.user_paged: page * config.user_paged] else: return followers
def main(csvfile): db, models = csv_to_db({csvfile: HapMapAllele}) hmalleles = models[HapMapAllele] with db_session: print select(x for x in hmalleles if x.rsid == 'rs6423165')[:][0].ref_freq print len(select(x.rsid for x in hmalleles if x.alt_freq > 0.01))
def get_followings(self, page=1): following_ids = (orm.select(rv.whom_id for rv in collipa.models.Follow if rv.who_id == self.id and rv.whom_id) .order_by(lambda: orm.desc(rv.created_at)) or [0]) followings = orm.select(rv for rv in User if rv.id in following_ids) if page: return followings[(page - 1) * config.user_paged: page * config.user_paged] else: return followings
def admin(bot, update): chat_id = update.message.chat_id username = update.message.chat.username with db_session: users = len(select(u.user_id for u in User)) last5 = '\n'.join(select(u.title for u in User)[:][-5:]) if username == 'TafarelYan': bot.sendMessage(chat_id, text="{} users registered.\n\n{}".format(users, last5))
def test_filling_database(feed, b, a): """ The test checks whether the replacement of news page stamps is executed correctly. """ # If a test failed here, parametrized tests' order may have become random :( assert len(orm.select(i for i in SiteSnapshot)) == b # Then, trying to add four news from the first set. td = datetime.now() update_latest_post_urls(td, feed) assert len(orm.select(i for i in SiteSnapshot)) == a
def list_(message): if "calendar" in message: calendar = get_entity(message.get("calendar", None), db.Calendar, "a calendar") with db_session: events = list(select(e.id for e in db.Event if e.calendar == calendar)) reply = dict(ids=events) else: with db_session: calendars = list(select(c.id for c in db.Calendar)) reply = dict(ids=calendars) return reply
def get_nodes(page=1, category='all', limit=None): if category == 'all': nodes = orm.select(rv for rv in Node).order_by(lambda rv: orm.desc(rv.created_at)) elif category == 'hot': nodes = orm.select(rv for rv in Node).order_by(lambda rv: orm.desc(rv.topic_count)) elif category == 'new': nodes = orm.select(rv for rv in Node).order_by(lambda rv: orm.desc(rv.created_at)) if limit: return nodes[:limit] if page: return nodes[(page - 1) * config.node_paged: page * config.node_paged] else: return nodes
def get_thankers(self, after_date=None, before_date=None): if after_date: user_ids = orm.select(rv.user_id for rv in m.Thank if rv.topic_id == self.id and rv.created_at > after_date) elif before_date: user_ids = orm.select(rv.user_id for rv in m.Thank if rv.topic_id == self.id and rv.created_at < before_date) else: user_ids = orm.select(rv.user_id for rv in m.Thank if rv.topic_id == self.id) users = [] if user_ids: user_ids = user_ids.order_by(lambda rv: orm.desc(rv.created_at)) users = orm.select(rv for rv in m.User if rv.id in user_ids) return users
def get_uppers(self, after_date=None, before_date=None): if after_date: user_ids = orm.select(rv.user_id for rv in collipa.models.Up if rv.album_id == self.id and rv.created_at > after_date) elif before_date: user_ids = orm.select(rv.user_id for rv in collipa.models.Up if rv.album_id == self.id and rv.created_at < before_date) else: user_ids = orm.select(rv.user_id for rv in collipa.models.Up if rv.album_id == self.id) users = [] if user_ids: user_ids = user_ids.order_by(lambda: orm.desc(rv.created_at)) users = orm.select(rv for rv in collipa.models.User if rv.id in user_ids) return users
def get_timeline(self, page=1, from_id=None, count=config.paged): user_ids = self.followed_user_ids[:] or [0] user_ids.append(self.id) if not from_id: tweets = (orm.select(rv for rv in collipa.models.Tweet if rv.user_id in user_ids) .order_by(lambda: orm.desc(rv.created_at)) [(page - 1) * config.paged: page * config.paged]) return tweets all_ids_q = orm.select(rv.id for rv in collipa.models.Tweet if rv.user_id in user_ids).order_by(lambda: orm.desc(rv.created_at)) tweet_ids = collect_items_from_query(all_ids_q, from_id, count) return orm.select(rv for rv in collipa.models.Tweet if rv.id in tweet_ids)[:][::-1]
def get_topics(self, page=1, category='all', order_by='created_at'): if category == 'all': topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id) order_by = 'last_reply_date' else: if category == 'hot': topics = mc.get('node_%s_hot_topics' % self.id) if not topics: now = int(time.time()) ago = now - 60 * 60 * 24 topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id and rv.created_at > ago) topics = topics.order_by(lambda rv: orm.desc((rv.collect_count + rv.thank_count - rv.report_count) * 10 + (rv.up_count - rv.down_count) * 5 + rv.reply_count * 3)) mc.set('node_%s_hot_topics' % self.id, list(topics), 60 * 60 * 3) order_by = 'none' elif category == 'latest': topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id) elif category == 'desert': topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id and rv.reply_count == 0) else: topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id and rv.role == category) order_by = 'last_reply_date' if order_by == 'last_reply_date': topics = topics.order_by(lambda rv: orm.desc(rv.last_reply_date)) elif order_by == 'created_at': topics = topics.order_by(lambda rv: orm.desc(rv.created_at)) elif order_by == 'active': topics = topics.order_by(lambda rv: orm.desc(rv.active)) elif order_by == 'smart': topics = topics.order_by(lambda rv: orm.desc((rv.collect_count + rv.thank_count - rv.report_count) * 10 + (rv.up_count - rv.down_count) * 5 + rv.reply_count * 3)) if page: return topics[(page - 1) * config.paged: page * config.paged] else: return topics
def list_all(): for project in db.Project.select(): print(project.name.upper()) print("-" * len(project.name)) for task in project.tasks: print(task) print() # List tasks that aren't part of a project tasks = orm.select(task for task in db.Task if task.project is None) if tasks: print("TASKS") print("-----") for task in tasks: print(task) print() ideas = db.Idea.select() if ideas: print("IDEAS") print("-----") for idea in ideas: print(idea) scratchpad = db.get_scratchpad() if scratchpad.content: print("SCRATCHPAD") print("----------") print(scratchpad.content)
def get_users(self): return [ ObjectDict( id=u.id, name=u.name, ) for u in orm.select(u for u in self.User) ]
def get_all(cls, *args): results = [] for p in pny.select(ecole for ecole in cls): results.append({s: getattr(p, s) for s in args}) return results
def album_list_id3(): ltype = request.values['type'] size, offset = map(request.values.get, [ 'size', 'offset' ]) size = int(size) if size else 10 offset = int(offset) if offset else 0 query = Album.select() if ltype == 'random': return request.formatter('albumList2', dict( album = [ a.as_subsonic_album(request.user) for a in query.random(size) ] )) elif ltype == 'newest': query = query.order_by(lambda a: desc(min(a.tracks.created))) elif ltype == 'frequent': query = query.order_by(lambda a: desc(avg(a.tracks.play_count))) elif ltype == 'recent': query = Album.select(lambda a: max(a.tracks.last_play) is not None).order_by(lambda a: desc(max(a.tracks.last_play))) elif ltype == 'starred': query = select(s.starred for s in StarredAlbum if s.user.id == request.user.id) elif ltype == 'alphabeticalByName': query = query.order_by(Album.name) elif ltype == 'alphabeticalByArtist': query = query.order_by(lambda a: a.artist.name + a.name) else: raise GenericError('Unknown search type') return request.formatter('albumList2', dict( album = [ f.as_subsonic_album(request.user) for f in query.limit(size, offset) ] ))
def get_blacklisted_tags(self, tags): """Принимает множество строк и возвращает словарь запрещённых тегов с описанием причины запрета. Если ничего не запрещено, словарь пуст. """ from mini_fiction.models import Tag result = {} inames = [] # Проверяем корректность синтаксиса for x in tags: reason = self.validate_tag_name(x) if reason is not None: result[x] = reason inames.append(None) else: iname = normalize_tag(x) if not iname: result[x] = lazy_gettext('Empty tag') inames.append(iname) # Проверяем чёрный список в базе данных inames_for_search = [x for x in inames if x] if inames_for_search: bl_tags = {t.iname: t for t in orm.select( x for x in Tag if x.iname in inames_for_search and x.is_blacklisted )} for x, iname in zip(tags, inames): if iname in bl_tags: result[x] = bl_tags[iname].reason_to_blacklist return result
def send_broad(bot, text): with db_session: for chat_id in select(chat.chat_id for chat in Chat if not (chat.silent_mode or chat.deleted)): try: bot.sendMessage(chat_id=chat_id, text=text) except telegram.TelegramError: pass
def get_root_user(cls): return orm.select(x for x in cls if x.name == config.root_user).first()
def topMessages(n: int=10): statusFilter = ["left", "kicked", ""] data = select((user, count(user.messages)) for user in User if user.lastStatus not in statusFilter).order_by(-2) return data.limit(n)
import time from models import Journal from pony.orm import db_session, select LEVEL_CHOICE = [10, 20, 30, 40, 50] start = time.time() count = 0 with db_session(): for _ in range(10): for level in LEVEL_CHOICE: res = [ obj.to_dict() for obj in select(j for j in Journal if j.level == level) ] count += len(res) now = time.time() print(f'Pony ORM, G: Rows/sec: {count / (now - start): 10.2f}')
def getOldestTime(sensor, registry, metric): return orm.select(r.time for r in registry if r.sensor == sensor and getattr(r, metric)).order_by(1).first()
def membershipDays(user): now = datetime.today().timestamp() daysActive = int((now - min(select(x.date for x in user.messages))) / 86400) return daysActive
def old_search(): artist, album, title, anyf, count, offset, newer_than = map( request.values.get, ["artist", "album", "title", "any", "count", "offset", "newerThan"], ) count = int(count) if count else 20 offset = int(offset) if offset else 0 newer_than = int(newer_than) / 1000 if newer_than else 0 min_date = datetime.fromtimestamp(newer_than) if artist: query = select(t.folder.parent for t in Track if artist in t.folder.parent.name and t.folder.parent.created > min_date) elif album: query = select( t.folder for t in Track if album in t.folder.name and t.folder.created > min_date) elif title: query = Track.select( lambda t: title in t.title and t.created > min_date) elif anyf: folders = Folder.select( lambda f: anyf in f.name and f.created > min_date) tracks = Track.select( lambda t: anyf in t.title and t.created > min_date) res = folders[offset:offset + count] fcount = folders.count() if offset + count > fcount: toff = max(0, offset - fcount) tend = offset + count - fcount res = res[:] + tracks[toff:tend][:] return request.formatter( "searchResult", dict( totalHits=folders.count() + tracks.count(), offset=offset, match=[ r.as_subsonic_child(request.user) if isinstance(r, Folder) else r.as_subsonic_child(request.user, request.client) for r in res ], ), ) else: raise MissingParameter("search") return request.formatter( "searchResult", dict( totalHits=query.count(), offset=offset, match=[ r.as_subsonic_child(request.user) if isinstance(r, Folder) else r.as_subsonic_child(request.user, request.client) for r in query[offset:offset + count] ], ), )
def validate_fnc_bounds(bound, target: Binary): size = bound.stop_addr - bound.start_addr return orm.select(f for f in target.functions if f.addr == bound.start_addr and f.size >= size)
def get_updated_channels(cls): return select(g for g in cls if g.subscribed and (g.local_version < g.timestamp))
def print_random_poem(self): ''' lol not random ''' p = orm.select(p for p in Poetry).first() print(p.title) print(p.poem)
def reply(msg): chatId = msg['chat']['id'] name = msg['from']['first_name'] if "text" in msg: text = msg['text'] else: bot.sendMessage(chatId, "🤨 Formato file non supportato. /help") return if not User.exists(lambda u: u.chatId == chatId): User(chatId=chatId) if not Data.exists(lambda d: d.chatId == chatId): Data(chatId=chatId) user = User.get(chatId=chatId) data = Data.get(chatId=chatId) if text == "/about": bot.sendMessage(chatId, "ℹ️ <b>Informazioni sul bot</b>\n" "IliadInfoBot è un bot creato e sviluppato da Filippo Pesavento, che ti permette " "di visualizzare tutte le info sul tuo account Iliad in mancanza dell'app.\n" "Prova ad usarlo per scoprire quanto è comodo!\n\n" "<b>Sviluppo:</b> Filippo Pesavento\n" "<b>Hosting:</b> Filippo Pesavento\n" "<b>Info sicurezza:</b> /aboutprivacy\n\n" "<i>IliadInfoBot non è in alcun modo affiliato con Iliad Italia S.p.A ed è una creazione " "esclusiva di Filippo Pesavento.</i>", parse_mode="HTML") elif text == "/aboutprivacy": bot.sendMessage(chatId, "ℹ️ <b>Informazioni sulla privacy</b>\n" "La mia password è al sicuro? 🤔\n\n" "🔐 <b>Sì: la tua password viene criptata.</b>\n" "Il bot conserva la tua password in maniera sicura, salvandola in un formato non leggibile da " "persone estranee. Sei al sicuro: i tuoi dati non verranno visti nè rubati da nessuno!\n\n" "🔐 <b>Spiegazione dettagliata:</b>\n" "Tecnicamente potrei decriptare a mano le password e vederle, ma sostanzialmente è complicato, " "perchè il bot genera una chiave per l'algoritmo (visto che il cripting deve essere reversibile, " "per poter mandare le notifiche automatiche) prendendo come dati una chiave comune (che salvo nella RAM " "e inserisco ad ogni avvio, per evitare che qualcuno che non sia io possa leggere il database e i dati degli utenti) " "e anche l'username dell'utente. Quindi ogni utente ha la propria password criptata con una chiave diversa da tutti " "gli altri, e sarebbe difficile anche per me risalire alla password, dovendo sapere di chi è l'username collegato a " "quella password specifica.\n" "Questo non vuol dire che non possa farlo: con un po' di lavoro ci riuscirei. Quindi alla fine devi decidere tu: " "io ti posso assicurare che non leggerò mai nè proverò mai a decriptare le password, sia per un discorso di etica " "che per scelta personale, ma non sono tuo amico nè tuo conoscente: quindi se decidi di non fidarti di uno sconosciuto " "che ti scrive su Telegram (ti posso capire benissimo) sei libero di non usare il bot 🙂\n\n" "<a href=\"https://t.me/pesaventofilippo\">Contattami</a>\n\n" "<i>Se sei venuto qui prima di digitare la password per il login, scrivila adesso!</i>", parse_mode="HTML", disable_web_page_preview=True) elif user.status != "normal": if text == "/annulla": user.status = "normal" bot.sendMessage(chatId, "Comando annullato!") elif user.status == "login_0": if len(text) != 8 or not text.isdigit(): bot.sendMessage(chatId, "⚠️ Errore: l'username deve essere un numero 8 cifre. Riprova!") return user.username = text user.status = "login_1" bot.sendMessage(chatId, "👍 Ottimo. Adesso inviami la password.\n" "Ricorda che la password viene salvata solo per te e viene criptata, nessuno potrà leggerla.\n\n" "Sei preoccupato per la sicurezza della password? /aboutprivacy") elif user.status == "login_1": user.password = crypt_password(text, chatId) user.status = "normal" commit() api = IliadApi(user.username, decrypt_password(chatId)) try: api.load() except AuthenticationFailedError: helpers.clearUserData(chatId) try: bot.sendMessage(chatId, "😯 Le tue credenziali di accesso sono errate.\n" "Controlla i dati inseriti e rieffettua il /login.") except (TelegramError, BotWasBlockedError): pass return bot.sendMessage(chatId, "Fatto 😊\n" "Premi /help per vedere la lista dei comandi disponibili.\n\n" "<i>Se vuoi, puoi eliminare il messaggio che mi hai mandato contenente la password: "******"non mi serve più!</i>", parse_mode="HTML") sent = bot.sendMessage(chatId, "🔍 Aggiorno il profilo...") helpers.fetchAndStore(api, chatId) bot.editMessageText((chatId, sent['message_id']), "✅ Profilo aggiornato!") elif user.status == "calling_support": user.status = "normal" for a in helpers.isAdmin(): bot.sendMessage(a, "🆘 <b>Richiesta di aiuto</b>\n" "Da: <a href=\"tg://user?id={0}\">{1}</a>\n\n" "<i>Rispondi al messaggio per parlare con l'utente.</i>".format(chatId, name), parse_mode="HTML") if "reply_to_message" in msg: bot.forwardMessage(a, chatId, msg["reply_to_message"]["message_id"]) bot.forwardMessage(a, chatId, msg['message_id'], disable_notification=True) bot.sendMessage(chatId, "<i>Richiesta inviata.</i>\n" "Un admin ti risponderà il prima possibile.", parse_mode="HTML") elif text == "/help": bot.sendMessage(chatId, "Ciao, sono il bot di <b>Iliad</b>! 👋🏻\n" "Posso aiutarti a <b>controllare</b> il tuo piano dati e posso mandarti <b>notifiche</b> (in futuro).\n\n" "<b>Lista dei comandi</b>:\n" "- /start - Avvia bot\n" "- /login - Effettua il login\n" "- /profilo - Informazioni sul profilo Iliad\n" "- /overview - Riepilogo generale dei consumi\n" "- /credito - Credito residuo\n" "- /internet - Visualizza piano dati\n" "- /chiamate - Visualizza piano chiamate\n" "- /sms - Visualizza piano SMS\n" "- /mms - Visualizza piano MMS\n" "- /logout - Disconnettiti\n" "- /aggiorna - Aggiorna tutti i dati. <b>Nota</b>: lo faccio già in automatico ogni mezz'ora per te!\n" "- /help - Mostra questa lista\n" "- /about - Informazioni sul bot\n" "- /aboutprivacy - Più informazioni sulla privacy\n" "- /support - Contatta lo staff (emergenze)\n\n" "<i>IliadInfoBot non è in alcun modo affiliato con Iliad Italia S.p.A ed è una creazione " "esclusiva di Filippo Pesavento.</i>", parse_mode="HTML") elif text == "/users" and helpers.isAdmin(chatId): totalUsers = len(select(u for u in User)[:]) loggedUsers = len(select(u for u in User if u.password != "")[:]) bot.sendMessage(chatId, "👤 Utenti totali: <b>{}</b>\n" "👤 Utenti loggati: <b>{}</b>".format(totalUsers, loggedUsers), parse_mode="HTML") elif text == "/globalupdate" and helpers.isAdmin(chatId): bot.sendMessage(chatId, "🕙 Inizio aggiornamento globale...") runUpdates() bot.sendMessage(chatId, "✅ Aggiornamento globale completato!") elif text.startswith("/broadcast ") and helpers.isAdmin(chatId): bdText = text.split(" ", 1)[1] pendingUsers = select(u.chatId for u in User)[:] userCount = len(pendingUsers) for u in pendingUsers: try: bot.sendMessage(u, bdText, parse_mode="HTML", disable_web_page_preview=True) except (TelegramError, BotWasBlockedError): userCount -= 1 bot.sendMessage(chatId, "📢 Messaggio inviato correttamente a {0} utenti!".format(userCount)) elif text.startswith("/sendmsg ") and helpers.isAdmin(chatId): selId = int(text.split(" ", 2)[1]) selText = str(text.split(" ", 2)[2]) bot.sendMessage(selId, selText, parse_mode="HTML") bot.sendMessage(chatId, selText + "\n\n- Messaggio inviato!", parse_mode="HTML") elif "reply_to_message" in msg: if helpers.isAdmin(chatId): try: userId = msg['reply_to_message']['forward_from']['id'] bot.sendMessage(userId, "💬 <b>Risposta dello staff</b>\n" "{0}".format(text), parse_mode="HTML") bot.sendMessage(chatId, "Risposta inviata!") except Exception: bot.sendMessage(chatId, "Errore nell'invio.") else: bot.sendMessage(chatId, "Scrivi /support per parlare con lo staff.") elif text == "/annulla": bot.sendMessage(chatId, "😴 Nessun comando da annullare!") elif helpers.hasStoredCredentials(chatId): if text == "/start": bot.sendMessage(chatId, "Bentornato, <b>{0}</b>!\n" "Cosa posso fare per te? 😊".format(name), parse_mode="HTML") elif text == "/login": bot.sendMessage(chatId, "Sei già loggato.\n" "Premi /logout per uscire.") elif text == "/logout": sent = bot.sendMessage(chatId, "Tutti i tuoi dati relativi all'account e le credenziali verranno eliminate dal bot.\n" "Sei <b>veramente sicuro</b> di voler uscire?", parse_mode="HTML") bot.editMessageReplyMarkup((chatId, sent['message_id']), keyboards.logout(sent['message_id'])) elif text == "/profilo": bot.sendMessage(chatId, f"👤 <b>Info profilo</b>\n\n" f"ℹ️ Nome: <b>{data.nome}</b>\n" f"📞 Numero: <b>{data.numero}</b>\n" f"🆔 ID Account: <b>{data.accountId}</b>\n\n" f"💶 Credito residuo: <b>{data.credito:.2f}€</b>\n" f"📅 Data rinnovo: <b>{data.dataRinnovo}</b>", parse_mode="HTML") elif text == "/overview": costo = data.costoChiamate + data.costoGiga + data.costoSms + data.costoMms sent = bot.sendMessage(chatId, f"ℹ️ <b>Riepilogo piano</b>\n\n" f"📞 Chiamate: <b>{data.totChiamate}</b>\n" f"🌐 Dati consumati: <b>{data.totGiga['count']}{data.totGiga['unit']}</b> su <b>" f"{data.pianoGiga['count']}{data.pianoGiga['unit']}</b>\n" f"✉️ SMS Inviati: <b>{data.totSms}</b>\n" f"📧 MMS Inviati: <b>{data.totMms}</b>\n\n" f"💸 Costi extra: {costo:.2f}€", parse_mode="HTML") bot.editMessageReplyMarkup((chatId, sent['message_id']), keyboards.overviewExt(sent['message_id'])) elif text == "/credito": bot.sendMessage(chatId, f"Il tuo credito residuo è di <b>{data.credito:.2f} euro</b>.", parse_mode="HTML") elif text == "/chiamate": bot.sendMessage(chatId, f"🇮🇹 <b>Chiamate in Italia</b>\n" f"🕙 Tempo: <b>{data.totChiamate}</b>\n" f"💸 Costi extra: <b>{data.costoChiamate:.2f}€</b>\n\n" f"🇪🇺 <b>Chiamate in Europa</b>\n" f"🕙 Tempo: <b>{data.ext_totChiamate}</b>\n" f"💸 Costi extra: <b>{data.ext_costoChiamate:.2f}€</b>", parse_mode="HTML") elif text == "/sms": bot.sendMessage(chatId, f"🇮🇹 <b>SMS in Italia</b>\n" f"✉️ Inviati: <b>{data.totSms} SMS</b>\n" f"💸 Costi extra: <b>{data.costoSms:.2f}€</b>\n\n" f"🇪🇺 <b>SMS in Europa</b>\n" f"✉️ Inviati: <b>{data.ext_totSms} SMS</b>\n" f"💸 Costi extra: <b>{data.ext_costoSms:.2f}€</b>", parse_mode="HTML") elif text == "/mms": bot.sendMessage(chatId, f"🇮🇹 <b>MMS in Italia</b>\n" f"📧 Inviati: <b>{data.totMms} MMS</b>\n" f"💸 Costi extra: <b>{data.costoSms:.2f}€</b>\n\n" f"🇪🇺 <b>MMS in Europa</b>\n" f"📧 Inviati: <b>{data.ext_totMms} MMS</b>\n" f"💸 Costi extra: <b>{data.ext_costoMms:.2f}€</b>", parse_mode="HTML") elif text == "/internet": bot.sendMessage(chatId, f"🇮🇹 <b>Piano dati in Italia</b>\n" f"📶 Consumati: <b>{data.totGiga['count']}{data.totGiga['unit']}</b> su <b>" f"{data.pianoGiga['count']}{data.pianoGiga['unit']}</b>\n" f"💸 Costi extra: <b>{data.costoGiga:.2f}€</b>\n\n" f"🇪🇺 <b>Piano dati in Europa</b>\n" f"📶 Consumati: <b>{data.ext_totGiga['count']}{data.ext_totGiga['unit']}</b> su <b>" f"{data.ext_pianoGiga['count']}{data.ext_pianoGiga['unit']}</b>\n" f"💸 Costi extra: <b>{data.ext_costoGiga:.2f}€</b>", parse_mode="HTML") elif text == "/support": user.status = "calling_support" bot.sendMessage(chatId, "🆘 <b>Richiesta di supporto</b>\n" "Se hai qualche problema che non riesci a risolvere, scrivi qui un messaggio, e un admin " "ti contatterà il prima possibile.\n\n" "<i>Per annullare, premi</i> /annulla.", parse_mode="HTML") elif text == "/aggiorna": if user.remainingCalls > 0: user.remainingCalls -= 1 commit() sent = bot.sendMessage(chatId, "📙📙📙 Aggiorno il profilo... 0%") api = IliadApi(user.username, decrypt_password(chatId)) bot.editMessageText((chatId, sent['message_id']), "📗📙📙 Cerco aggiornamenti... 10%") try: api.load() except AuthenticationFailedError: helpers.clearUserData(chatId) bot.editMessageText((chatId, sent['message_id']), "⚠️ Le tue credenziali non sono corrette.\n" "Rieffettua il /login!.") return bot.editMessageText((chatId, sent['message_id']), "📗📙📙 Cerco aggiornamenti... 50%") helpers.fetchAndStore(api, chatId) bot.editMessageText((chatId, sent['message_id']), "📗📗📗 Cerco aggiornamenti... 100%") bot.editMessageText((chatId, sent['message_id']), "✅ Profilo aggiornato!") else: bot.sendMessage(chatId, "⛔️ Hai usato troppi /aggiorna recentemente. Aspetta un po'!") else: bot.sendMessage(chatId, "Non ho capito...\n" "Serve aiuto? Premi /help") else: if text == "/login": user.status = "login_0" bot.sendMessage(chatId, "Per favore, inviami il tuo <b>username</b> (il codice da 8 cifre che usi per accedere " "all'Area Personale).\n" "Usa /annulla se serve.", parse_mode="HTML") else: bot.sendMessage(chatId, "Benvenuto, <b>{0}</b>!\n" "Per favore, premi /login per utilizzarmi.\n\n" "Premi /help se serve aiuto.".format(name), parse_mode="HTML")
def runUpdates(): pendingUsers = select(user.chatId for user in User if user.password != "")[:] for currentUser in pendingUsers: Thread(target=runUserUpdate, args=[currentUser]).start()
def dispatch_request(self): less = select(x for x in BlogPost if x.post_type == BlogPostType.LESSON.value).order_by(BlogPost.id.desc()) return render_template("lessons.html", title='Master', subtitle='courses', lessons=(less[x: x + 3] for x in range(0, len(less), 3)))
def dispatch_request(self): studs = select(x for x in TeamPost if x.post_type == TeamPostType.STUDENT.value).order_by(TeamPost.id.desc()) return render_template("students.html", title='Laboratory', subtitle='students', students=(studs[x: x + 4] for x in range(0, len(studs), 4)))
async def check_random_tracker(self): """ Calling this method will fetch a random tracker from the database, select some torrents that have this tracker, and perform a request to these trackers. Return whether the check was successful. """ if self._should_stop: self._logger.warning( "Not performing tracker check since we are shutting down") return False tracker = self.get_valid_next_tracker_for_auto_check() if tracker is None: self._logger.warning("No tracker to select from, skip") return False self._logger.debug("Start selecting torrents on tracker %s.", tracker.url) # get the torrents that should be checked with db_session: dynamic_interval = TORRENT_CHECK_RETRY_INTERVAL * ( 2**tracker.failures) # FIXME: this is a really dumb fix for update_tracker_info not being called in some cases if tracker.failures >= MAX_TRACKER_FAILURES: self.update_tracker_info(tracker.url, False) return False torrents = select( ts for ts in tracker.torrents if ts.last_check + dynamic_interval < int(time.time())) infohashes = [ t.infohash for t in torrents[:MAX_TORRENTS_CHECKED_PER_SESSION] ] if len(infohashes) == 0: # We have no torrent to recheck for this tracker. Still update the last_check for this tracker. self._logger.info("No torrent to check for tracker %s", tracker.url) self.update_tracker_info(tracker.url, True) return False try: session = self._create_session_for_request(tracker.url, timeout=30) except MalformedTrackerURLException as e: # Remove the tracker from the database self.remove_tracker(tracker.url) self._logger.error(e) return False # We shuffle the list so that different infohashes are checked on subsequent scrape requests if the total # number of infohashes exceeds the maximum number of infohashes we check. random.shuffle(infohashes) for infohash in infohashes: session.add_infohash(infohash) self._logger.info("Selected %d new torrents to check on tracker: %s", len(infohashes), tracker.url) try: await self.connect_to_tracker(session) return True except: return False
def messagesPerDay(user): now = datetime.today().timestamp() daysActive = int((now - min(select(x.date for x in user.messages))) / 86400) return count(user.messages) / daysActive
def test_update_from_ipif(db, data1): "A simple update on an existing object" Statement = db.entities['Statement'] with orm.db_session: data = data1['factoids'][0]['statements'][0] data_new = copy.deepcopy(data) data_new['createdBy'] = 'Foo Foo Bar' data_new['uris'] = ['foooo'] data_new['places'] = [{'label': 'foo', 'uri': 'foo'}] data_new['relatesToPersons'] = [{'label': 'bar', 'uri': 'bar'}] data_new['statementType'] = {'label': 'foo', 'uri': 'foo'} data_new['role'] = {'label': 'foo', 'uri': 'foo'} data_new['memberOf'] = {'label': 'foo', 'uri': 'foo'} data_new['date'] = { 'label': 'foo', 'sortDate': datetime.date(1805, 3, 17).isoformat() } s = Statement.create_from_ipif(data) Date = db.entities['Date'] Role = db.entities['Role'] StatementType = db.entities['StatementType'] MemberGroup = db.entities['MemberGroup'] RelatesToPerson = db.entities['RelatesToPerson'] Place = db.entities['Place'] StatementURI = db.entities['StatementURI'] s.update_from_ipif(data_new) assert s.id == data_new['@id'] assert s.createdBy == data_new['createdBy'] assert s.name == data_new['name'] assert s.statementContent == data_new['statementContent'] assert s.date.label == data_new['date']['label'] assert s.date.sortDate == datetime.datetime.fromisoformat( data_new['date']['sortDate']).date() assert s.role.uri == data_new['role']['uri'] assert s.role.label == data_new['role']['label'] assert s.statementType.uri == data_new['statementType']['uri'] assert s.statementType.label == data_new['statementType']['label'] assert s.memberOf.uri == data_new['memberOf']['uri'] assert s.memberOf.label == data_new['memberOf']['label'] assert len(s.relatesToPersons) == len(data_new['relatesToPersons']) assert sorted([r.uri for r in s.relatesToPersons]) == sorted( [r['uri'] for r in data_new['relatesToPersons']]) assert sorted([r.label for r in s.relatesToPersons]) == sorted( [r['label'] for r in data_new['relatesToPersons']]) assert len(s.places) == len(data_new['places']) assert sorted([p.uri for p in s.places ]) == sorted([p['uri'] for p in data_new['places']]) assert sorted([p.label for p in s.places ]) == sorted([p['label'] for p in data_new['places']]) assert len(s.uris) == len(data_new['uris']) assert sorted([u.uri for u in s.uris]) == sorted(data_new['uris']) # Test if deletion of orphaned sub-objects works: # we replaced all sub-element, but still only 1 of each type may exists in db. assert orm.select(s for s in Statement).count() == 1 assert orm.select(d for d in Date).count() == 1 assert orm.select(r for r in Role).count() == 1 assert orm.select(s for s in StatementType).count() == 1 assert orm.select(m for m in MemberGroup).count() == 1 assert orm.select(r for r in RelatesToPerson).count() == 1 assert orm.select(p for p in Place).count() == 1 assert orm.select(u for u in StatementURI).count() == 1
def reply(msg): chatId = msg['chat']['id'] name = msg['from']['first_name'] if "text" in msg: text = msg['text'] elif "caption" in msg: text = msg['caption'] else: text = "" if not Category.exists(lambda c: c.name == "General"): Category(name="General") if not User.exists(lambda u: u.chatId == chatId): User(chatId=chatId) user = User.get(chatId=chatId) ## Text Message if msg.get('text'): if user.status.endswith("uploading_file"): if text == "/cancel": user.status = "normal" bot.sendMessage(chatId, "📕 File upload cancelled.") else: bot.sendMessage( chatId, "❓ Please upload an ebook file.\n" "Type /cancel to abort.") elif user.status.startswith("selecting_category"): book_id = int(user.status.split('#', 1)[1]) book = Book.get(id=book_id) if text == "/cancel": book.category = Category.get(name="General") user.status = "normal" bot.sendMessage(chatId, "📗 Book moved to category <b>General</b>.", parse_mode="HTML") return categories = [ cat.name.lower() for cat in select(c for c in Category)[:] ] if text.lower() not in categories: cat = Category(name=text) commit() book.category = cat user.status = "normal" bot.sendMessage( chatId, f"📗 New category <b>{text}</b> successfully added!\n" f"I also added the book to the new category for you.", parse_mode="HTML") else: bot.sendMessage( chatId, f"📙 Category <b>{text}</b> already exists!\n" f"Try with a different name, or select one from the list above.", parse_mode="HTML") elif text == "/getusers" and isAdmin(chatId): users = len(select(u for u in User)[:]) bot.sendMessage( chatId, f"👥 There currently are <b>{users}</b> registered users.", parse_mode="HTML") elif text == "/getbooks" and isAdmin(chatId): books = len(select(b for b in Book)[:]) bot.sendMessage(chatId, f"📚 There currently are <b>{books}</b> books.", parse_mode="HTML") elif text == "/movebook" and isAdmin(chatId): sent = bot.sendMessage( chatId, "📦 Please choose the book you want to move:") bot.editMessageReplyMarkup( (chatId, sent['message_id']), keyboards.manageBooks("move", sent['message_id'])) elif text == "/delbook" and isAdmin(chatId): sent = bot.sendMessage(chatId, "🗑 Please choose the book to delete:") bot.editMessageReplyMarkup( (chatId, sent['message_id']), keyboards.manageBooks("del", sent['message_id'])) elif text == "/bulkupload": user.status = "bulk_uploading_file" bot.sendMessage( chatId, "📎 <b>Bulk Upload mode active.</b> Type /cancel to abort.\n" "<i>Only PDF files are supported.</i>", parse_mode="HTML") # General user commands elif text == "/start": bot.sendMessage( chatId, f"Hey <b>{name}</b>! I'm the Free Books Bot 👋🏻\n" f"I'm currently in <b>Beta Version</b>, but you can already use me to find some books: " f"type /search to find a book by category, or type /help if you have any question.", parse_mode="HTML") elif text == "/help": bot.sendMessage(chatId, "<b>Commands List</b>\n\n" "/start - Start bot\n" "/help - Show this page\n" "/search - Search books by category\n" "/submit - Send a new ebook for everyone to read\n" "/cancel - Reset current action", parse_mode="HTML") elif text == "/search": sent = bot.sendMessage(chatId, "🔍 <b>Book Search</b>\n" "Pick a category from the list below:", parse_mode="HTML") bot.editMessageReplyMarkup( (chatId, sent['message_id']), keyboards.search_cat(sent['message_id'])) elif text == "/submit": user.status = "uploading_file" bot.sendMessage( chatId, "📎 Ok, please send me the new ebook, or type /cancel to abort.\n" "<i>Only PDF files are supported.</i>", parse_mode="HTML") elif text == "/cancel": bot.sendMessage( chatId, "Operation cancelled!\n" "I was doing nothing, by the way... 😴") elif text.startswith("/start getbook"): book_id = int(text.split('_')[1]) book = Book.get(id=book_id) bot.sendDocument( chatId, book.telegramFileId, f"<b>Book Name:</b> {book.name}\n" f"<b>Category:</b> {book.category.name}", "HTML") # Unknown command else: bot.sendMessage(chatId, "😕 Sorry, I didn't understand.\n" "Need /help?") ## File Document elif msg.get('document') and supportedFile(msg): if not user.status.endswith("uploading_file"): bot.sendMessage( chatId, "📕 Sorry, you're currently not uploading a file.\n" "Type /submit if you would like to submit a new book.") return fileId = msg['document']['file_id'] fileSize = msg['document']['file_size'] fileName = msg['document']['file_name'] if fileSize > 50000000: # 50MB Telegram Limit (in bytes) bot.sendMessage( chatId, "📕 Sorry, the file size must be lower than <b>50MB</b>.", parse_mode="HTML") return if not Book.exists(lambda b: b.name == fileName): book = Book(name=fileName, telegramFileId=fileId) commit() if (not isAdmin(chatId)) or (user.status == "bulk_uploading_file"): book.category = Category.get(name="General") if user.status != "bulk_uploading_file": user.status = "normal" bot.sendMessage(chatId, f"📗 <b>{fileName}</b> successfully uploaded!", parse_mode="HTML") else: user.status = f"selecting_category#{book.id}" if not select(c for c in Category if c.name != "General")[:]: bot.sendMessage( chatId, f"📗 <b>{fileName}</b> successfully uploaded!\n" f"Please type a name to create a new category:", parse_mode="HTML") else: sent = bot.sendMessage( chatId, f"📗 <b>{fileName}</b> successfully uploaded!\n" f"Please select a category for the book, or type a name to create a new one:", parse_mode="HTML") bot.editMessageReplyMarkup( (chatId, sent['message_id']), keyboards.category(book.id, sent['message_id'])) # Book with same name already exists else: bot.sendMessage( chatId, f"📙 Warning: \"<b>{fileName}</b>\" already exists! If you think this is an error, please " f"change the file name and reupload it.", parse_mode="HTML") # Filetype not supported else: bot.sendMessage( chatId, "🤨 I'm sorry, but this is not a supported file type...\n" "Are you lost? Press /help")
def list_posts(): return render_template("drafts.html", posts=orm.select(p for p in Post).order_by(orm.desc(Post.modified)), title="Posts")
import time from random import choice from models import Journal from pony.orm import commit, db_session, select LEVEL_CHOICE = [10, 20, 30, 40, 50] with db_session(): objs = list(select(j for j in Journal)) count = len(objs) start = time.time() for obj in objs: obj.delete() commit() now = time.time() print(f"Pony ORM, K: Rows/sec: {count / (now - start): 10.2f}")
def __init__(self, structure, user, special=None): """ storing reaction in DB. :param structure: CGRtools ReactionContainer :param user: user entity :param special: Json serializable Data (expected dict) """ super().__init__(user=user) # preload all molecules and structures signatures = {bytes(m) for m in structure.reagents } | {bytes(m) for m in structure.products} ms, s2ms = defaultdict(list), {} for x in select(x for x in self._database_.MoleculeStructure if x.molecule in select( y.molecule for y in self._database_.MoleculeStructure if y.signature in signatures)).prefetch( self._database_.Molecule): # NEED PR # select(y for x in db.MoleculeStructure if x.signature in signatures_set # for y in db.MoleculeStructure if y.molecule == x.molecule) if x.signature in signatures: s2ms[x.signature] = x if x.last: x.molecule._cached_structure = x ms[x.molecule].append(x) for m, s in ms.items(): m._cached_structures_all = tuple(s) combinations, duplicates = [], {} for sl, is_p in ((structure.reagents, False), (structure.products, True)): for s in sl: sig = bytes(s) ms = s2ms.get(sig) if ms: mapping = ms.structure.get_mapping(s) self._database_.MoleculeReaction(reaction=self, molecule=ms.molecule, is_product=is_p, mapping=mapping) # first MoleculeStructure always last if ms.last: # last structure equal to reaction structure c = [s] c.extend( x.structure.remap(mapping, copy=True) for x in ms.molecule.all_editions if not x.last) else: # last structure remapping c = [ms.molecule.structure.remap(mapping, copy=True)] c.extend( x.structure.remap(mapping, copy=True ) if x != ms else s for x in ms.molecule.all_editions if not x.last) combinations.append(c) else: # New molecule if sig not in duplicates: m = duplicates[sig] = self._database_.Molecule(s, user) mapping = None else: m = duplicates[sig] mapping = m.structure.get_mapping(s) self._database_.MoleculeReaction(reaction=self, molecule=m, is_product=is_p, mapping=mapping) combinations.append([s]) reagents_len = len(structure.reagents) combinations = tuple(product(*combinations)) if len(combinations) == 1: # optimize self._cached_structures_all = (structure, ) self._cached_structure = structure self._database_.ReactionIndex(self, structure, True) else: x = combinations[0] self._cached_structure = ReactionContainer( reagents=x[:reagents_len], products=x[reagents_len:]) self._database_.ReactionIndex(self, self._cached_structure, True) cgr = {} for x in combinations[1:]: x = ReactionContainer(reagents=x[:reagents_len], products=x[reagents_len:]) cgr[~x] = x self._cached_structures_all = (self._cached_structure, *cgr.values()) for x in cgr: self._database_.ReactionIndex(self, x, False) if special: self.special = special
def dohvati_stavke(ids): q = select(s for s in StavkaIspita if s.id in ids) data = [x.to_dict("ocjena student brojBodova") for x in q] return data
def listaj(): q = select(s for s in Kolegij) data = [x.to_dict() for x in q] return data
def eval(self): tags = [(platform,) for platform in sorted(select(x.platform for x in Tag))] return EmbedTable(fields=['Platform'], table=tags, colour=self.EMBED_COLOR)
def getRegistryQuery(registry, deviceName, metric, fromDate, toDate): devicesRegistries = orm.select(r[metric] for r in registry if fromDate <= r.time and r.time <= toDate)
# fetch @orm.db_session def fetch_tbl(tticker: Optional[str]) -> None: if tticker: Utticker = tticker.upper() if (tbl := db.TableDb.get(tticker=Utticker)): srs = [srs.ticker for srs in tbl.series] return { "ticker": Utticker, "description": tbl.tbl_description, "series": srs } else: tbls = orm.select(t for t in db.TableDb) return [{ "ticker": t.tticker, "description": t.tbl_description, "series": [s.ticker for s in t.series] } for t in tbls] # fetch indexes to be ingested by search engine @orm.db_session def fetch_all_series() -> pd.DataFrame: srs = orm.select( (s.ticker, s.description, s.kind, s.group) for s in db.Series) lsrs = [{"ticker": s[0], "description": s[1], "group": s[3]} for s in srs] with open("./configuration.json") as fj:
def select_by_name_and_site(self, name: str, site: Website) -> Author: authors = orm.select(a for a in Author if a.name == name and a.website == site).limit(1) if len(authors) == 1: return authors[0]
def get_all(self, chat_id): # noinspection PyTypeChecker return select( quest for quest in Quest if quest.chat_id == chat_id).order_by(lambda t: t.created_at)[:]
def get_by_login_details(cls, user_name, password): passhash = utils.hash_pw(password) return orm.select( x for x in cls if x.name == user_name and x.passhash == passhash).first()
def renderChart(request): DateCombined = [] CommentDateCombined = [] DailyAverageCombined = [] CommentCombined = [] CommentReadingCombined = [] dATE = 0 aVERAGE = 1 with db_session: qry = select( (r.readingdate, r.avgreading) for r in Readings).order_by(1) try: recs = qry.fetch() except Exception as e: print(e) for rec in recs: dtdate, avg = rec dtdate = datetime.strptime(dtdate, "%Y-%m-%d") DateCombined.append(dtdate) DailyAverageCombined.append(avg) qry = select( (c.date, c.reading, c.comment) for c in Comments).order_by(1) try: recs = qry.fetch() except Exception as e: print(e) for rec in recs: dtdate, reading, comment = rec dtdate = datetime.strptime(dtdate, "%Y-%m-%d") CommentDateCombined.append(dtdate) CommentReadingCombined.append(reading) CommentCombined.append(comment) DateCombined = mpd.date2num(DateCombined) CommentDateCombined = mpd.date2num(CommentDateCombined) fig, ax1 = plt.subplots() lineTarg = ax1.axhline(y=6, linewidth=4, color='k', label='Glucose Target Range') ax1.axhline(y=9, linewidth=4, color='k') background = 0.30 lineCombined, = ax1.plot_date(DateCombined, DailyAverageCombined, label='Daily Blood Glucose', linestyle='-', linewidth=1, color='r', marker=None, tz=pst) # ax1.yaxis.grid(True, linewidth=1) for i in range(len(CommentDateCombined)): # text = f'<---{(CommentCombined[i], CommentDateCombined[i])}' text = f'<---{CommentCombined[i]}' # return pprint.pformat((text, CommentDateCombined[i], CommentAverageCombined[i])) ax1.annotate(text, (CommentDateCombined[i], CommentReadingCombined[i]), fontsize=14, color='b', weight='bold') # , rotation=0, #------------------------- DateRange = np.concatenate((DateCombined, )) minDate = min(DateRange) maxDate = max(DateRange) ax1.set_xlim(minDate, maxDate) df = mpl.dates.DateFormatter('%b-%d', tz=pst) ax1.xaxis.set_major_formatter(df) ax1.tick_params(which='major', width=2.0, length=4.0) # , labelsize=10) xlocator = mpl.dates.DayLocator(tz=pst) ax1.xaxis.set_minor_locator(xlocator) plt.gcf().autofmt_xdate() z = np.polyfit(DateCombined, DailyAverageCombined, 2) # z = np.polynomial.chebyshev.chebfit(DateCombined, DailyAverageCombined, 2) p = np.poly1d(z) trendLine, = ax1.plot_date(DateCombined, p(DateCombined), 'k--', label='Trend Line') # ax1.legend(handles=[lineCombined, trendLine, lineTarg], loc='upper left') # , loc='lower right' 'best' ax1.legend(handles=[lineCombined, lineTarg, trendLine], loc='upper right') # , loc='lower right' 'best' plt.title('Average Daily Blood Glucose (Jardiance Trial)', loc='left') plt.title('William Trenker') # # sessionID = HTTPCookie.getSessionCookie(request) nowstr = System.getLastReadingDateStr() # nowstr = dateTimeStr(datetime.now(), "America/Vancouver") dbNow = f'({dbFileName}) last updated: {nowstr}' plt.title(dbNow, fontsize=10, loc='right') # ax1.set_xlabel( 'Date (2019 - 2020)') # Note that this won't work on plt or ax2 ax1.set_ylabel('Blood Glucose (mmol/L)') fig.set_size_inches(16, 8.5) # fig.tight_layout() img = BytesIO() fig.savefig(img) img.seek(0) return img
def editRatio(user): ratio = count(select(x for x in user.messages if x.edited)) / count(user.messages) return ratio