def actuality(self): """ Return sophisticated value of post actuality """ cv = lambda x, y: float(x) / y if x < y else 1.0 # ceil part-value now_time = datetime.now() int_posted = (now_time - self.date_posted).total_seconds() int_updt = (now_time - self.date_updated).total_seconds() int_years = timedelta(days=365 * 5).total_seconds() comments_weight = cv(self.comments, 10) * 15 likes_weight = cv(self.likes, 30) * 15 views_weight = cv(self.views, 100) * 10 # the less time passed the higher value posted_weight = (1 - cv(int_posted, int_years)) * 40 updated_weight = (1 - cv(int_updt, int_years)) * 20 act = (comments_weight + likes_weight + views_weight + posted_weight + updated_weight) assert act < 100 if act < 10: app.log('Post %d is not relevant' % self.post_id, 'warning') return act
def actuality(self): """ Return sophisticated value of post actuality """ cv = lambda x, y: float(x)/y if x < y else 1.0 # ceil part-value now_time = datetime.now() int_posted = (now_time - self.date_posted).total_seconds() int_updt = (now_time - self.date_updated).total_seconds() int_years = timedelta(days=365*5).total_seconds() comments_weight = cv(self.comments, 10)*15 likes_weight = cv(self.likes, 30)*15 views_weight = cv(self.views, 100)*10 # the less time passed the higher value posted_weight = (1-cv(int_posted, int_years))*40 updated_weight = (1-cv(int_updt, int_years))*20 act = (comments_weight + likes_weight + views_weight + posted_weight + updated_weight) assert act < 100 if act < 10: app.log('Post %d is not relevant' % self.post_id, 'warning') return act
def _render_post_page(post): cu = app.current_user if post.deleted or post.draft: if cu is not None and \ (cu.user_id == post.user.user_id or cu.is_admin()): app.log('Showing post to its creator or to admin') else: abort(404) template = env.get_template('post/view.html') post.views += 1 post.save() return template.render(item=post)
def sp_add(): form = StaticPageForm(request.POST) template = env.get_template('static_page_admin.html') if form.validate_on_post(): app.log(form.page_url.data) new_page = StaticPage.create(title=form.title.data, url=form.page_url.data, text=form.text.data) app.flash('New page!') pages = StaticPage.select() return template.render(form=form, pages=pages)
def sp_add(): form = StaticPageForm( request.POST, model_class=StaticPage, url_prefix='sp', ) template = env.get_template('item_add.html') if form.validate_on_post(): app.log(form.page_url.data) StaticPage.create( title=form.title.data, url=form.page_url.data, text=form.text.data, ) app.flash('New page!') pages = StaticPage.select() return template.render(form=form, items=pages)
def main(): host = str('httpbin.org') port = int('443') server_name_indication = sys.argv[1] try: socket_tunnel = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_tunnel.settimeout(3) app.log('Connecting to {} port {}'.format(host, port)) socket_tunnel.connect((host, port)) app.log('Server name indication: {}'.format(server_name_indication)) socket_tunnel = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2).wrap_socket( socket_tunnel, server_hostname=server_name_indication, do_handshake_on_connect=True) certificate = ssl.DER_cert_to_PEM_cert( socket_tunnel.getpeercert(True)).splitlines() certificate = '\n'.join(certificate[:13] + certificate[-13:]) app.log('Certificate: \n\n{}\n'.format(certificate)) app.log('Connection established') app.log('Connected', color='[Y1]') except socket.timeout: app.log('Connection timeout', color='[R1]') except socket.error: app.log('Connection closed', color='[R1]') finally: socket_tunnel.close()