def send_message(cls, log_line, event): message = { 'event': event, 'date': datetime_filter(log_line.timestamp, format='%b %d, %Y'), 'time': datetime_filter(log_line.timestamp, format='%I:%M%p'), 'username': log_line.username, 'chat': log_line.chat, 'death_message': log_line.death_message, 'achievement_message': log_line.achievement_message } client_ids = cls.get_client_ids(log_line.server_key) if client_ids: for client_id in client_ids: try: user = cls.get_user_key(client_id).get() if user is not None: timezone = user.timezone message['date'] = datetime_filter(log_line.timestamp, format='%b %d, %Y', timezone=timezone) message['time'] = datetime_filter(log_line.timestamp, format='%I:%M%p', timezone=timezone) message_json = json.dumps(message) channel.send_message(client_id, message_json) except: pass
async def push_blog(request, *, caption, summary, content, belong_to, subdivide): if not caption or not caption.strip(): raise APIError('caption') if not summary or not summary.strip(): raise APIError('summary') if not content or not content.strip(): raise APIError('content') if not belong_to or not belong_to.strip(): raise APIError('belong_to') if not subdivide or not subdivide.strip(): raise APIError('subdivide') user = None if request.__user__ is not None: user = request.__user__ else: raise APIError('user') blog = Blog(caption=caption, summary=summary, content=content, belong_to=belong_to, subdivide=subdivide, user_id=user.id, user_name=user.name, user_image=user.image) affected = await blog.save() if affected != 1: error = 'affected rows : %d' % affected else: error = 'none' blog.content = '*****' blog.create_at = datetime_filter(blog.create_at) return dict(blog=blog, error=error)
async def api_get_users(): users = await User.findAllOrMany(orderBy='create_at desc' ) #where="`name`='test'" for u in users: u.passwd = '******' u.create_at = datetime_filter(u.create_at) return dict(users=users)
async def api_get_blog_list(*, belong_to, page, num, subdivide): page = int(page) num = int(num) cnt = await Blog.findNumber('count(id)', where="belong_to='" + belong_to + "'") page_max = cnt // num + 1 if page > page_max: page = page_max top = (page - 1) * num limit = (int(top), int(num)) blogs = None if subdivide == 'none': blogs = await Blog.findColumn( ("`id`,`caption`", "`summary`", "`belong_to`", "`subdivide`", "`summary`", "`create_at`"), where='`belong_to`="' + belong_to + '"', orderBy='create_at desc', limit=limit) else: blogs = await Blog.findColumn( ("`id`,`caption`", "`summary`", "`belong_to`", "`subdivide`", "`summary`", "`create_at`"), where='`subdivide`="' + subdivide + '"', orderBy='create_at desc', limit=limit) for d in blogs: for k, v in d.items(): if k == 'create_at': d[k] = datetime_filter(v) return dict(blogs=blogs, max_page=page_max, page=page)
async def user_page(request): if request.__user__ is not None: user = request.__user__ user.create_at = datetime_filter(user.create_at) else: user = User(name='未登录', email="未登录", passwrd="******") return {'__template__': 'user_page.html', 'user': user}
async def get_blog(id, request): name, a_link_to = set_user(request) blog = await Blog.find(id) blog.create_at = datetime_filter(blog.create_at) return { '__template__': 'blog_page.html', 'blog': blog, 'name': name, 'href': a_link_to }
async def get_comment_list(*, blog_id, page, num): page = int(page) num = int(num) cnt = await Comment.findNumber('count(id)', where="blog_id='" + blog_id + "'") page_max = cnt // num + 1 if page > page_max: page = page_max top = (page - 1) * num limit = (int(top), int(num)) comments = await Comment.findAllOrMany(where='blog_id="' + blog_id + '"', orderBy='create_at asc', limit=limit) for d in comments: for k, v in d.items(): if k == 'create_at': d[k] = datetime_filter(v) return dict(comments=comments, max_page=page_max, page=page)
def update_status( self, status=None, last_ping=None, server_day=None, server_time=None, is_raining=None, is_thundering=None, num_overloads=None, ms_behind=None, skipped_ticks=None, address=None, timestamp=None, completed=None ): logging.info( "Updating Status (current: {0}, queued: {1}, new: {2}, completed: {3})".format( self.status, self.queued, status, completed ) ) changed = False now = datetime.datetime.utcnow() address = address or self.address timeout = now - datetime.timedelta(minutes=5) previous_status = self.status # Set queued datetime if status in [SERVER_QUEUED_START, SERVER_QUEUED_RESTART, SERVER_QUEUED_STOP]: self.queued = datetime.datetime.utcnow() self.idle = None changed = True elif status in [SERVER_LOADING, SERVER_LOADED, SERVER_SAVING]: if completed is not None: self.completed = completed self.queued = datetime.datetime.utcnow() self.idle = None changed = True elif status is not None and self.queued is not None: # Don't update status if status is not desired outcome of queued status, will go UNKNOWN eventually if something goes wrong # noqa if ( (previous_status in [SERVER_QUEUED_START, SERVER_LOADING, SERVER_LOADED] and status not in [SERVER_HAS_STARTED, SERVER_RUNNING]) or # noqa (previous_status == SERVER_QUEUED_RESTART and status not in [SERVER_HAS_STARTED, SERVER_RUNNING]) or # noqa (previous_status in [SERVER_QUEUED_STOP, SERVER_SAVING] and status is not SERVER_SAVED) ): status = previous_status else: self.completed = None self.queued = None self.idle = None changed = True # No status provided, check for timeout on running servers if status is None: status = previous_status if previous_status != SERVER_STOPPED: if self.queued is None: if self.last_ping is None or self.last_ping < timeout: self.idle = None status = SERVER_UNKNOWN elif self.queued < timeout: self.queued = None self.completed = None self.idle = None status = SERVER_UNKNOWN if status != previous_status: changed = True # Update address if status in [SERVER_HAS_STOPPED, SERVER_SAVED, SERVER_STOPPED]: address = None if address != self.address: self.address = address changed = True # Update server day/time if ( (server_day is not None and server_day != self.last_server_day) or (server_time is not None and server_time != self.last_server_time) ): self.timestamp = timestamp or datetime.datetime.utcnow() if server_day is not None: self.last_server_day = server_day if server_time is not None: self.last_server_time = server_time changed = True # Update server weather if is_raining is not None and is_raining != self.is_raining: self.is_raining = is_raining changed = True if is_thundering is not None and is_thundering != self.is_thundering: self.is_thundering = is_thundering changed = True # Update overloads if num_overloads is not None and num_overloads != self.num_overloads: self.num_overloads = num_overloads changed = True if ms_behind is not None and ms_behind != self.ms_behind: self.ms_behind = ms_behind changed = True if skipped_ticks is not None and skipped_ticks != self.skipped_ticks: self.skipped_ticks = skipped_ticks changed = True # Record pings every minute, even if nothing changed if last_ping is not None: if self.last_ping is None or self.last_ping < last_ping - datetime.timedelta(minutes=1): changed = True # Close all open play sessions if stopped if status in [SERVER_HAS_STOPPED, SERVER_SAVED]: PlaySession.close_all_current(self.key, now) # Put server changes if changed: logging.info( "Changed Status (previous: {0}, new: {1}, queued: {2})".format( self.status, status, self.queued ) ) if status == SERVER_HAS_STARTED: status = SERVER_RUNNING elif status in [SERVER_HAS_STOPPED, SERVER_SAVED]: status = SERVER_STOPPED logging.info( "Actual Changed Status (previous: {0}, new: {1}, queued: {2})".format( self.status, status, self.queued ) ) self.status = status if last_ping is not None: self.last_ping = last_ping self.put() # Email admins send_email = previous_status != status if status in [ SERVER_QUEUED_START, SERVER_QUEUED_RESTART, SERVER_QUEUED_STOP, SERVER_LOADING, SERVER_LOADED, SERVER_SAVING, SERVER_SAVED ]: send_email = False if send_email: for admin in User.query_admin(): subject = "{0} server status is {1}".format(self.name, status) body = 'The {0} server status is {1} as of {2}.\n\nThe last agent ping was on {3}'.format( self.name, status, datetime_filter(datetime.datetime.utcnow(), timezone=admin.timezone), datetime_filter(self.last_ping, timezone=admin.timezone) if self.last_ping else 'NEVER' ) User.send_admin_email(subject, body, admin=admin)
def update_status(self, status=None, last_ping=None, server_day=None, server_time=None, is_raining=None, is_thundering=None, num_overloads=None, ms_behind=None, skipped_ticks=None, address=None, timestamp=None, completed=None): logging.info( "Updating Status (current: {0}, queued: {1}, new: {2}, completed: {3})" .format(self.status, self.queued, status, completed)) changed = False now = datetime.datetime.utcnow() address = address or self.address timeout = now - datetime.timedelta(minutes=5) previous_status = self.status # Set queued datetime if status in [ SERVER_QUEUED_START, SERVER_QUEUED_RESTART, SERVER_QUEUED_STOP ]: self.queued = datetime.datetime.utcnow() self.idle = None changed = True elif status in [SERVER_LOADING, SERVER_LOADED, SERVER_SAVING]: if completed is not None: self.completed = completed self.queued = datetime.datetime.utcnow() self.idle = None changed = True elif status is not None and self.queued is not None: # Don't update status if status is not desired outcome of queued status, will go UNKNOWN eventually if something goes wrong # noqa if ((previous_status in [SERVER_QUEUED_START, SERVER_LOADING, SERVER_LOADED] and status not in [SERVER_HAS_STARTED, SERVER_RUNNING]) or # noqa (previous_status == SERVER_QUEUED_RESTART and status not in [SERVER_HAS_STARTED, SERVER_RUNNING]) or # noqa (previous_status in [SERVER_QUEUED_STOP, SERVER_SAVING] and status is not SERVER_SAVED)): status = previous_status else: self.completed = None self.queued = None self.idle = None changed = True # No status provided, check for timeout on running servers if status is None: status = previous_status if previous_status != SERVER_STOPPED: if self.queued is None: if self.last_ping is None or self.last_ping < timeout: self.idle = None status = SERVER_UNKNOWN elif self.queued < timeout: self.queued = None self.completed = None self.idle = None status = SERVER_UNKNOWN if status != previous_status: changed = True # Update address if status in [SERVER_HAS_STOPPED, SERVER_SAVED, SERVER_STOPPED]: address = None if address != self.address: self.address = address changed = True # Update server day/time if ((server_day is not None and server_day != self.last_server_day) or (server_time is not None and server_time != self.last_server_time)): self.timestamp = timestamp or datetime.datetime.utcnow() if server_day is not None: self.last_server_day = server_day if server_time is not None: self.last_server_time = server_time changed = True # Update server weather if is_raining is not None and is_raining != self.is_raining: self.is_raining = is_raining changed = True if is_thundering is not None and is_thundering != self.is_thundering: self.is_thundering = is_thundering changed = True # Update overloads if num_overloads is not None and num_overloads != self.num_overloads: self.num_overloads = num_overloads changed = True if ms_behind is not None and ms_behind != self.ms_behind: self.ms_behind = ms_behind changed = True if skipped_ticks is not None and skipped_ticks != self.skipped_ticks: self.skipped_ticks = skipped_ticks changed = True # Record pings every minute, even if nothing changed if last_ping is not None: if self.last_ping is None or self.last_ping < last_ping - datetime.timedelta( minutes=1): changed = True # Close all open play sessions if stopped if status in [SERVER_HAS_STOPPED, SERVER_SAVED]: PlaySession.close_all_current(self.key, now) # Put server changes if changed: logging.info( "Changed Status (previous: {0}, new: {1}, queued: {2})".format( self.status, status, self.queued)) if status == SERVER_HAS_STARTED: status = SERVER_RUNNING elif status in [SERVER_HAS_STOPPED, SERVER_SAVED]: status = SERVER_STOPPED logging.info( "Actual Changed Status (previous: {0}, new: {1}, queued: {2})". format(self.status, status, self.queued)) self.status = status if last_ping is not None: self.last_ping = last_ping self.put() # Email admins send_email = previous_status != status if status in [ SERVER_QUEUED_START, SERVER_QUEUED_RESTART, SERVER_QUEUED_STOP, SERVER_LOADING, SERVER_LOADED, SERVER_SAVING, SERVER_SAVED ]: send_email = False if send_email: for admin in User.query_admin(): subject = "{0} server status is {1}".format( self.name, status) body = 'The {0} server status is {1} as of {2}.\n\nThe last agent ping was on {3}'.format( self.name, status, datetime_filter(datetime.datetime.utcnow(), timezone=admin.timezone), datetime_filter(self.last_ping, timezone=admin.timezone) if self.last_ping else 'NEVER') User.send_admin_email(subject, body, admin=admin)
async def api_get_blogs(): blogs = await Blog.findAllOrMany(orderBy='create_at desc' ) #where="`name`='test'" for b in blogs: b.create_at = datetime_filter(b.create_at) return dict(blogs=blogs)
async def api_get_comments(): comments = await Comment.findAllOrMany(orderBy='create_at desc' ) #where="`name`='test'" for c in comments: c.create_at = datetime_filter(c.create_at) return dict(comments=comments)