def post(self, slug): # user must be logged in msg = {} if not self.current_user: msg = {'error': 'You must be logged in to bump.', 'redirect': True} else: post = postsdb.get_post_by_slug(slug) if post: can_vote = True for u in post['voted_users']: if u['username'] == self.current_user: can_vote = False if not can_vote: msg = {'error': 'You have already upvoted this post.'} else: user = userdb.get_user_by_screen_name(self.current_user) # Increment the vote count post['votes'] += 1 post['voted_users'].append(user['user']) postsdb.save_post(post) msg = {'votes': post['votes']} # send email notification to post author author = userdb.get_user_by_screen_name(post['user']['username']) if 'email_address' in author.keys(): subject = "[#usvconversation] @%s just bumped up your post: %s" % (self.current_user, post['title']) text = "Woo!\n\n%s" % template_helpers.post_permalink(post) logging.info('sent email to %s' % author['email_address']) self.send_email('*****@*****.**', author['email_address'], subject, text) self.api_response(msg)
def get(self, slug): # user must be logged in msg = {} if not self.current_user: msg = {'error': 'You must be logged in to bump.', 'redirect': True} else: post = postsdb.get_post_by_slug(slug) if post: can_vote = True for u in post['voted_users']: if u['username'] == self.current_user: can_vote = False if not can_vote: msg = {'error': 'You have already upvoted this post.'} else: user = userdb.get_user_by_screen_name(self.current_user) # Increment the vote count post['votes'] += 1 post['voted_users'].append(user['user']) postsdb.save_post(post) msg = {'votes': post['votes']} # send email notification to post author author = userdb.get_user_by_screen_name(post['user']['username']) if 'email_address' in author.keys(): subject = "[#usvconversation] @%s just bumped up your post: %s" % (self.current_user, post['title']) text = "Woo!\n\n%s" % template_helpers.post_permalink(post) logging.info('sent email to %s' % author['email_address']) self.send_email('*****@*****.**', author['email_address'], subject, text) self.api_response(msg)
def post(self, slug): # user must be logged in msg = {} if not self.current_user: msg = {'error': 'You must be logged in to bump.', 'redirect': True} else: post = postsdb.get_post_by_slug(slug) if post: can_vote = True for u in post['voted_users']: if u['username'] == self.current_user: can_vote = False if not can_vote: msg = {'error': 'You have already upvoted this post.'} else: user_info = userdb.get_user_by_screen_name(self.current_user) # Increment the vote count post.votes += 1 post.update(push__voted_users=user_info.user) post.save() msg = {'votes': post.votes} # send email notification to post author author = userdb.get_user_by_screen_name(post.user.username) if author.email_address: subject = "[#theconversation] @%s just bumped up your post: %s" % (self.current_user, post.title) text = "Woo!\n\n%s" % post.permalink() logging.info('sent email to %s' % author.email_address) self.send_email(settings.get('system_email_address'), author.email_address, subject, text) self.api_response(msg)
def get(self, slug): # user must be logged in msg = {} if not self.current_user: msg = {"error": "You must be logged in to bump.", "redirect": True} else: post = postsdb.get_post_by_slug(slug) if post: can_vote = True for u in post["voted_users"]: if u["username"] == self.current_user: can_vote = False if not can_vote: msg = {"error": "You have already upvoted this post."} else: user = userdb.get_user_by_screen_name(self.current_user) # Increment the vote count post["votes"] += 1 post["voted_users"].append(user["user"]) postsdb.save_post(post) msg = {"votes": post["votes"]} # send email notification to post author author = userdb.get_user_by_screen_name(post["user"]["username"]) if "email_address" in author.keys(): subject = "[#usvconversation] @%s just bumped up your post: %s" % ( self.current_user, post["title"], ) text = "Woo!\n\nhttp://%s" % template_helpers.post_permalink(post) logging.info("sent email to %s" % author["email_address"]) self.send_email("*****@*****.**", author["email_address"], subject, text) self.api_response(msg)
def post(self, post_slug): post = postsdb.get_post_by_slug(post_slug) post_owner = userdb.get_user_by_screen_name(post.user.screen_name) user_info = userdb.get_user_by_screen_name(self.current_user) firebase_id = self.get_argument('firebase_id', None) parent_comment = None if self.get_argument('parent_comment_id', '') != "": parent_comment = commentsdb.get_comment_by_id(self.get_argument('parent_comment_id', '')) comment = commentsdb.add_comment(post, user_info, self.get_argument('comment_body_text', ''), parent_comment, firebase_id) if comment.status == "published": #self.set_secure_cookie("flash", "Comment added!") #self.redirect(post.permalink()) self.write('success') else: self.write('error')
def get(self, username=None): if username is None and self.current_user: username = self.current_user if username != self.current_user: raise tornado.web.HTTPError(401) if self.request.path.find("/user/settings") >= 0: self.redirect('/user/%s/settings' % username) msg = self.get_argument("msg", None) user = userdb.get_user_by_screen_name(self.current_user) if not user: raise tornado.web.HTTPError(404) user['wants_daily_email'] = user.get('wants_daily_email', False) user['wants_email_alerts'] = user.get('wants_email_alerts', True) #self.render('user/settings.html', user=user, msg=msg) self.render('user/profile.html', user=user, screen_name=self.current_user, posts=None, section="settings", page=None, per_page=None, tags=None, tag=None, msg=msg)
def get(self, slug): post = postsdb.get_post_by_slug(slug) if not post: raise tornado.web.HTTPError(404) topics = topicsdb.get_all() comments = commentsdb.get_comments_by_post(post) current_user_can_edit = False current_user = userdb.get_user_by_screen_name(self.current_user) if current_user: current_user_can_edit = (current_user.role == "staff" or post.user == current_user.user) # remove dupes from voted_users voted_users = [] for i in post.voted_users: if i not in voted_users: voted_users.append(i) post.voted_users = voted_users self.vars.update({ 'post': post, 'comments': comments, 'topics': topics, 'current_user_can_edit': current_user_can_edit }) self.render('post/view_post.html', **self.vars)
def get(self, screen_name, section="shares"): user = userdb.get_user_by_screen_name(screen_name) if not user: raise tornado.web.HTTPError(404) view = "profile" #section = self.get_argument('section', 'shares') tag = self.get_argument('tag', '') per_page = int(self.get_argument('per_page', 10)) page = int(self.get_argument('page',1)) if section == 'mentions': # get the @ mention list for this user posts = mentionsdb.get_mentions_by_user(screen_name.lower(), per_page, page) elif section =='bumps': posts = postsdb.get_posts_by_bumps(screen_name, per_page, page) else: if tag == '': posts = postsdb.get_posts_by_screen_name(screen_name, per_page, page) else: posts = postsdb.get_posts_by_screen_name_and_tag(screen_name, tag, per_page, page) # also get the list of tags this user has put in tags = tagsdb.get_user_tags(screen_name) self.render('user/profile.html', user=user, screen_name=screen_name, posts=posts, section=section, page=page, per_page=per_page, tags=tags, tag=tag, msg=None, view=view)
def get(self): comment = self.get_argument('comment', '') post_slug = self.get_argument('post', '') post = postsdb.get_post_by_slug(post_slug) if post: # increment the comment count for this post post['comment_count'] += 1 postsdb.save_post(post) # determine if anyone is subscribed to this post (for email alerts) if len(post['subscribed']) > 0: # attempt to get the current user's email (we don't need to alert them) author_email = '' if self.current_user: author = userdb.get_user_by_screen_name(self.current_user) if author and 'email_address' in author.keys() and author['email_address'].strip() != '': author_email = author['email_address'] # get the message details from Disqus message = disqus.get_post_details(comment) if message['message'].strip() != '': # send the emails with details logging.info(message) subject = 'New message on: %s' % post['title'] text = 'The following comment was just added to your share on usv.com.\n\n%s\n\nYou can engaged in the conversation, and manage your alert settings for this share, at http://www.usv.com/posts/%s' % (message['message'], post['slug']) for email in post['subscribed']: # attempt to send to each of these subscribed people (don't send to author) if email.lower() != author_email.lower() and email.strip() != '': self.send_email('*****@*****.**', email, subject, text) self.api_response('OK')
def get(self): code = self.get_argument('code','') req_host = self.request.headers['host'] api_key = settings.get('disqus_public_key') api_secret = settings.get('disqus_secret_key') link = 'https://disqus.com/api/oauth/2.0/access_token/' data = { 'grant_type':'authorization_code', 'client_id':api_key, 'client_secret':api_secret, 'redirect_uri': 'http://%s/disqus' % req_host, 'code' : code } try: account = userdb.get_user_by_screen_name(self.current_user) if account: response = urllib2.urlopen(link, urllib.urlencode(data)) # access_token should look like access_token=111122828977539|98f28d8b5b8ed787b585e69b.1-537252399|1bKwe6ghzXyS9vPDyeB9b1fHLRc user_data = json.loads(response.read()) # refresh the user token details account['disqus_username'] = user_data['username'] account['disqus_user_id'] = user_data['user_id'] account['disqus_access_token'] = user_data['access_token'] account['disqus_expires_in'] = user_data['expires_in'] account['disqus_refresh_token'] = user_data['refresh_token'] account['disqus_token_type'] = user_data['token_type'] userdb.save_user(account) except: # trouble logging in data = {} self.redirect('/your_account?section=social_accounts')
def get(self, screen_name): if self.current_user in settings.get('staff'): user = userdb.get_user_by_screen_name(screen_name) if user: user['user']['is_blacklisted'] = False userdb.save_user(user) self.redirect('/')
def get(self): comment = self.get_argument("comment", "") post_slug = self.get_argument("post", "") post = postsdb.get_post_by_slug(post_slug) if post: # increment the comment count for this post post["comment_count"] += 1 postsdb.save_post(post) # determine if anyone is subscribed to this post (for email alerts) if len(post["subscribed"]) > 0: # attempt to get the current user's email (we don't need to alert them) author_email = "" if self.current_user: author = userdb.get_user_by_screen_name(self.current_user) if author and "email_address" in author.keys() and author["email_address"].strip() != "": author_email = author["email_address"] # get the message details from Disqus message = disqus.get_post_details(comment) if message["message"].strip() != "": # send the emails with details logging.info(message) subject = "New message on: %s" % post["title"] text = ( "The following comment was just added to your share on usv.com.\n\n%s\n\nYou can engaged in the conversation, and manage your alert settings for this share, at http://www.usv.com/posts/%s" % (message["message"], post["slug"]) ) for email in post["subscribed"]: # attempt to send to each of these subscribed people (don't send to author) if email.lower() != author_email.lower() and email.strip() != "": self.send_email("*****@*****.**", email, subject, text) self.api_response("OK")
def get(self): # remove twitter from this account account = userdb.get_user_by_screen_name(self.current_user) if account: del account['disqus'] userdb.save_user(account) self.redirect('/user/%s/settings?msg=updated' % self.current_user)
def get(self, username): if self.current_user_role() in settings.get('admin_roles'): user = userdb.get_user_by_screen_name(username) if user: user['user']['is_blacklisted'] = False userdb.save_user(user) self.set_secure_cookie('flash_success', "<strong>" + username + "</strong> was unbanned" ) self.redirect('/admin/users')
def post(self): next_page = self.get_argument('next', '') next_page += "&finished=true" close_popup = self.get_argument('close_popup', '') email = self.get_argument('email', '') subscribe_to = self.get_argument('subscribe_to', '') error = '' status = '' slug = '' if close_popup != '': status = 'close_popup' # get the current user's email value user = userdb.get_user_by_screen_name(self.current_user) if user: # Clear the existing email address if email == '': if subscribe_to == '': user['email_address'] = '' self.set_secure_cookie('email_address', '') userdb.save_user(user) error = 'Your email address has been cleared.' else: # make sure someone else isn't already using this email existing = userdb.get_user_by_email(email) if existing and existing['user']['id_str'] != user['user']['id_str']: error = 'This email address is already in use.' else: # OK to save as user's email user['email_address'] = email userdb.save_user(user) self.set_secure_cookie('email_address', email) if subscribe_to != '': post = postsdb.get_post_by_slug(subscribe_to) if post: slug = post['slug'] # Attempt to create the post's thread thread_id = 0 try: # Attempt to create the thread. thread_details = disqus.create_thread(post, user['disqus_access_token']) thread_id = thread_details['response']['id'] except: try: # trouble creating the thread, try to just get the thread via the slug thread_details = disqus.get_thread_details(slug) thread_id = thread_details['response']['id'] except: thread_id = 0 if thread_id != 0: # Subscribe a user to the thread specified in response disqus.subscribe_to_thread(thread_id, user['disqus_access_token']) self.redirect("/user/%s/settings?msg=updated" % user['user']['screen_name'])
def get(self): oauth_verifier = self.get_argument('oauth_verifier', '') consumer_key = settings.get('twitter_consumer_key') consumer_secret = settings.get('twitter_consumer_secret') auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True) auth.set_request_token(self.get_secure_cookie('request_token_key'), self.get_secure_cookie('request_token_secret')) auth.get_access_token(oauth_verifier) screen_name = auth.get_username() bounce_to = self.get_secure_cookie('twitter_auth_redirect') or '/' access_token = { 'secret': auth.access_token.secret, 'user_id': '', 'screen_name': '', 'key': auth.access_token.key } # check if we have this user already or not in the system user = userdb.get_user_by_screen_name(screen_name) if user: # set the cookies based on account details self.set_secure_cookie("user_id_str", user['user']['id_str']) self.set_secure_cookie("username", user['user']['screen_name']) if 'email_address' not in user or ('email_address' in user and user['email_address'] == ''): bounce_to = '/user/%s/settings?1' % screen_name else: # need to create the account (so get more details from Twitter) auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True) api = tweepy.API(auth) user = api.get_user(screen_name) access_token['user_id'] = user.id access_token['screen_name'] = user.screen_name user_data = { 'auth_type': 'twitter', 'id_str': user.id_str, 'username': user.screen_name, 'fullname': user.name, 'screen_name': user.screen_name, 'profile_image_url': user.profile_image_url, 'profile_image_url_https': user.profile_image_url_https, } # now save to mongo userdb.create_new_user(user_data, access_token) # and set our cookies self.set_secure_cookie("user_id_str", user.id_str) self.set_secure_cookie("username", user.screen_name) bounce_to = '/user/%s/settings?msg=twitter-thanks' % screen_name # let's save the screen_name to a cookie as well so we can use it for restricted bounces if need be self.set_secure_cookie('screen_name', screen_name, expires_days=30) # bounce to account self.redirect(bounce_to)
def render(self, template, **kwargs): # add any variables we want available to all templates kwargs['user_obj'] = None kwargs['user_obj'] = userdb.get_user_by_screen_name(self.current_user) kwargs['current_user_can'] = self.current_user_can kwargs['settings'] = settings super(BaseHandler, self).render(template, **kwargs)
def current_user_role(self): """ Returns the current user's role """ result = "" u = userdb.get_user_by_screen_name(self.current_user) if u and 'role' in u.keys(): result = u['role'] return result
def post(self): domain_dict = {} domain_dict['name'] = self.get_argument('name', '') domain_dict['description'] = self.get_argument('description', '') domain_dict['status'] = self.get_argument('status', '') new_domain = Domain(**domain_dict) new_domain.user_info = userdb.get_user_by_screen_name(self.current_user) new_domain.save() self.set_secure_cookie('flash', 'domain added!') self.redirect(new_domain.permalink())
def get(self): oauth_verifier = self.get_argument("oauth_verifier", "") consumer_key = settings.get("twitter_consumer_key") consumer_secret = settings.get("twitter_consumer_secret") auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True) auth.set_request_token( self.get_secure_cookie("request_token_key"), self.get_secure_cookie("request_token_secret") ) auth.get_access_token(oauth_verifier) screen_name = auth.get_username() bounce_to = "/" access_token = { "secret": auth.access_token.secret, "user_id": "", "screen_name": "", "key": auth.access_token.key, } # check if we have this user already or not in the system user = userdb.get_user_by_screen_name(screen_name) if user: # set the cookies based on account details self.set_secure_cookie("user_id_str", user["user"]["id_str"]) self.set_secure_cookie("username", user["user"]["screen_name"]) if "email_address" not in user or ("email_address" in user and user["email_address"] == ""): bounce_to = "/user/%s/settings?1" % screen_name else: # need to create the account (so get more details from Twitter) auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True) api = tweepy.API(auth) user = api.get_user(screen_name) access_token["user_id"] = user.id access_token["screen_name"] = user.screen_name user_data = { "auth_type": "twitter", "id_str": user.id_str, "username": user.screen_name, "fullname": user.name, "screen_name": user.screen_name, "profile_image_url": user.profile_image_url, "profile_image_url_https": user.profile_image_url_https, } # now save to mongo userdb.create_new_user(user_data, access_token) # and set our cookies self.set_secure_cookie("user_id_str", user.id_str) self.set_secure_cookie("username", user.screen_name) bounce_to = "/user/%s/settings?msg=twitter-thanks" % screen_name # let's save the screen_name to a cookie as well so we can use it for restricted bounces if need be self.set_secure_cookie("screen_name", screen_name, expires_days=30) # bounce to account self.redirect(bounce_to)
def get(self, username): if username != self.current_user: raise tornado.web.HTTPError(401) msg = self.get_argument("msg", None) user = userdb.get_user_by_screen_name(self.current_user) if not user: raise tornado.web.HTTPError(404) #self.render('user/settings.html', user=user, msg=msg) self.render('user/profile.html', user=user, screen_name=self.current_user, posts=None, section="settings", page=None, per_page=None, tags=None, tag=None, msg=msg)
def get(self): oauth_verifier = self.get_argument('oauth_verifier', '') consumer_key = settings.get('twitter_consumer_key') consumer_secret = settings.get('twitter_consumer_secret') auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_request_token(self.get_secure_cookie('request_token_key'), self.get_secure_cookie('request_token_secret')) auth.get_access_token(oauth_verifier) screen_name = auth.get_username() bounce_to = '/' access_token = { 'secret': auth.access_token.secret, 'user_id': '', 'screen_name': '', 'key': auth.access_token.key } # check if we have this user already or not in the system user = userdb.get_user_by_screen_name(screen_name) if user: # set the cookies based on account details self.set_secure_cookie("user_id_str", user['user']['id_str']) self.set_secure_cookie("username", user['user']['screen_name']) if 'email_address' not in user or ('email_address' in user and user['email_address'] == ''): bounce_to = '/user/%s/settings?1' % screen_name else: # need to create the account (so get more details from Twitter) auth = tweepy.OAuthHandler(consumer_key, consumer_secret) api = tweepy.API(auth) user = api.get_user(screen_name) access_token['user_id'] = user.id access_token['screen_name'] = user.screen_name user_data = { 'auth_type': 'twitter', 'id_str': user.id_str, 'username': user.screen_name, 'fullname': user.name, 'screen_name': user.screen_name, 'profile_image_url': user.profile_image_url, 'profile_image_url_https': user.profile_image_url_https, } # now save to mongo userdb.create_new_user(user_data, access_token) # and set our cookies self.set_secure_cookie("user_id_str", user.id_str) self.set_secure_cookie("username", user.screen_name) bounce_to = '/user/%s/settings?msg=twitter-thanks' % screen_name # let's save the screen_name to a cookie as well so we can use it for restricted bounces if need be self.set_secure_cookie('screen_name', screen_name, expires_days=30) # bounce to account self.redirect(bounce_to)
def render(self, template, **kwargs): # add any variables we want available to all templates kwargs['user_obj'] = None kwargs['user_obj'] = userdb.get_user_by_screen_name(self.current_user) kwargs['current_user_can'] = self.current_user_can kwargs['settings'] = settings kwargs['body_location_class'] = "" if self.request.path == "/": kwargs['body_location_class'] = "home" super(BaseHandler, self).render(template, **kwargs)
def get(self): # remove twitter from this account account = userdb.get_user_by_screen_name(self.current_user) if account: del account['disqus_username'] del account['disqus_user_id'] del account['disqus_access_token'] del account['disqus_expires_in'] del account['disqus_refresh_token'] del account['disqus_token_type'] userdb.save_user(account) self.redirect('/user/%s/settings?msg=updated' % self.current_user)
def get(self): next_page = self.get_argument('next', '') subscribe_to = self.get_argument('subscribe_to', '') error = '' email = '' status = 'enter_email' # get the current user's email value user = userdb.get_user_by_screen_name(self.current_user) if user: email = user['email_address'] self.render('user/email_subscribe.html', email=email, error=error, next_page=next_page, subscribe_to=subscribe_to, status=status)
def current_user_can(self, capability): """ Tests whether a user can do a certain thing. """ result = False u = userdb.get_user_by_screen_name(self.current_user) if u and 'role' in u.keys(): try: if capability in settings.get('%s_capabilities' % u['role']): result = True except: result = False return result
def get(self): # remove twitter from this account account = userdb.get_user_by_screen_name(self.current_user) if account: del account['disqus_username'] del account['disqus_user_id'] del account['disqus_access_token'] del account['disqus_expires_in'] del account['disqus_refresh_token'] del account['disqus_token_type'] userdb.save_user(account) self.redirect('/your_account?section=social_accounts')
def get(self): code = self.get_argument('code', '') req_host = self.request.headers['host'] api_key = settings.get('disqus_public_key') api_secret = settings.get('disqus_secret_key') link = 'https://disqus.com/api/oauth/2.0/access_token/' data = { 'grant_type': 'authorization_code', 'client_id': api_key, 'client_secret': api_secret, 'redirect_uri': 'http://%s/disqus' % req_host, 'code': code } try: account = userdb.get_user_by_screen_name(self.current_user) if account: response = urllib2.urlopen(link, urllib.urlencode(data)) # access_token should look like access_token=111122828977539|98f28d8b5b8ed787b585e69b.1-537252399|1bKwe6ghzXyS9vPDyeB9b1fHLRc user_data = json.loads(response.read()) # refresh the user token details disqus_obj = {} disqus_obj['username'] = user_data['username'] disqus_obj['user_id'] = user_data['user_id'] disqus_obj['access_token'] = user_data['access_token'] disqus_obj['expires_in'] = user_data['expires_in'] disqus_obj['refresh_token'] = user_data['refresh_token'] disqus_obj['token_type'] = user_data['token_type'] disqus_obj['token_startdate'] = datetime.now() account['disqus'] = disqus_obj if 'disqus_username' in account.keys(): del account['disqus_username'] if 'disqus_user_id' in account.keys(): del account['disqus_user_id'] if 'disqus_access_token' in account.keys(): del account['disqus_access_token'] if 'disqus_expires_in' in account.keys(): del account['disqus_expires_in'] if 'disqus_refresh_token' in account.keys(): del account['disqus_refresh_token'] if 'disqus_token_type' in account.keys(): del account['disqus_token_type'] userdb.save_user(account) # subscribe user to all previous threads they've written disqus.subscribe_to_all_your_threads(self.current_user) except Exception, e: logging.info(e) # trouble logging in data = {}
def render(self, template, **kwargs): # add any variables we want available to all templates kwargs['user_obj'] = None kwargs['user_obj'] = userdb.get_user_by_screen_name(self.current_user) kwargs['current_user_can'] = self.current_user_can kwargs['settings'] = settings kwargs['body_location_class'] = "" kwargs['current_path'] = self.request.uri #kwargs['request_path'] = self.request if self.request.path == "/": kwargs['body_location_class'] = "home" super(BaseHandler, self).render(template, **kwargs)
def get(self): code = self.get_argument('code','') req_host = self.request.headers['host'] api_key = settings.get('disqus_public_key') api_secret = settings.get('disqus_secret_key') link = 'https://disqus.com/api/oauth/2.0/access_token/' data = { 'grant_type':'authorization_code', 'client_id':api_key, 'client_secret':api_secret, 'redirect_uri': 'http://%s/disqus' % req_host, 'code' : code } try: account = userdb.get_user_by_screen_name(self.current_user) if account: response = urllib2.urlopen(link, urllib.urlencode(data)) # access_token should look like access_token=111122828977539|98f28d8b5b8ed787b585e69b.1-537252399|1bKwe6ghzXyS9vPDyeB9b1fHLRc user_data = json.loads(response.read()) # refresh the user token details disqus_obj = {} disqus_obj['username'] = user_data['username'] disqus_obj['user_id'] = user_data['user_id'] disqus_obj['access_token'] = user_data['access_token'] disqus_obj['expires_in'] = user_data['expires_in'] disqus_obj['refresh_token'] = user_data['refresh_token'] disqus_obj['token_type'] = user_data['token_type'] disqus_obj['token_startdate'] = datetime.now() account['disqus'] = disqus_obj if 'disqus_username' in account.keys(): del account['disqus_username'] if 'disqus_user_id' in account.keys(): del account['disqus_user_id'] if 'disqus_access_token' in account.keys(): del account['disqus_access_token'] if 'disqus_expires_in' in account.keys(): del account['disqus_expires_in'] if 'disqus_refresh_token' in account.keys(): del account['disqus_refresh_token'] if 'disqus_token_type' in account.keys(): del account['disqus_token_type'] userdb.save_user(account) # subscribe user to all previous threads they've written disqus.subscribe_to_all_your_threads(self.current_user) except Exception, e: logging.info(e) # trouble logging in data = {}
def render(self, template, **kwargs): # add any variables we want available to all templates kwargs['user_obj'] = None kwargs['user_obj'] = userdb.get_user_by_screen_name(self.current_user) kwargs['current_user_can'] = self.current_user_can kwargs['settings'] = settings kwargs['body_location_class'] = "" kwargs['current_path'] = self.request.uri kwargs['is_blacklisted'] = self.is_blacklisted #kwargs['request_path'] = self.request if self.request.path == "/": kwargs['body_location_class'] = "home" elif self.request.path == "/contact": kwargs['body_location_class'] = "contact-page" super(BaseHandler, self).render(template, **kwargs)
def render(self, template, **kwargs): # add any variables we want available to all templates kwargs['user_obj'] = None kwargs['user_obj'] = userdb.get_user_by_screen_name(self.current_user) kwargs['current_user_can'] = self.current_user_can kwargs['settings'] = settings kwargs['body_location_class'] = "" kwargs['current_path'] = self.request.uri user_info = kwargs['user_obj'] #kwargs['request_path'] = self.request if user_info and 'date_created' not in user_info.keys(): user_info['date_created'] = datetime.now() - timedelta(days=180) userdb.save_user(user_info) if self.request.path == "/": kwargs['body_location_class'] = "home" super(BaseHandler, self).render(template, **kwargs)
def get(self, slug): post = postsdb.get_post_by_slug(slug) if not post: raise tornado.web.HTTPError(404) tag_posts = [] all_keeper_posts = [] if 'tags' in post.keys() and len(post['tags']) > 0: for t in post['tags']: posts = postsdb.get_related_posts_by_tag(t) tag_keeper_posts = [] for p in posts: if p['slug'] != slug and p not in all_keeper_posts: tag_keeper_posts.append(p) all_keeper_posts.append(p) obj = {'tag': t, 'posts': tag_keeper_posts} tag_posts.append(obj) msg = self.get_argument('msg', None) user = None if self.current_user: user = userdb.get_user_by_screen_name(self.current_user) # remove dupes from voted_users voted_users = [] for i in post['voted_users']: if i not in voted_users: voted_users.append(i) post['voted_users'] = voted_users hot_posts_past_week = postsdb.get_hot_posts_past_week() featured_posts = {} view = "single" self.render('post/view_post.html', user_obj=user, post=post, msg=msg, tag_posts=tag_posts, hot_posts_past_week=hot_posts_past_week, featured_posts=featured_posts, view=view)
def subscribe_to_all_your_threads(username): account = userdb.get_user_by_screen_name(username) # todo: get disqus_user_id # temp: nick's ID if 'disqus' not in account: print 'ERROR: no disqus user ID' return print 'subscribing to disqus threads for user %s' % username #make sure all your threads are registered w disqus posts = postsdb.get_posts_by_screen_name(username, per_page=25, page=1) for post in posts: print template_helpers.post_permalink(post) if 'disqus_thread_id_str' not in post.keys() or post.get( 'disqus_thread_id_str') == "": thread_details = create_thread(post, account['disqus']['access_token']) try: thread_id = thread_details['response']['id'] except: thread = get_thread_details(post) thread_id = thread['response']['id'] post['disqus_thread_id_str'] = thread_id postsdb.save_post(post) subscribe_to_thread( post.get('disqus_thread_id_str'), account['disqus']['access_token']) ''' threads = get_all_threads(account['disqus']['user_id'])['response'] my_threads = [] for thread in threads: if 'link' not in thread or thread['link'] is None: continue if thread['link'].find('http://localhost') >= 0: continue my_threads.append(thread) if 'disqus' in account: for thread in my_threads: subscribe_to_thread(thread['id'], account['disqus']['access_token']) print 'subscribed to thread: %s' % thread['title'] return ''' return
def get(self, slug): post = postsdb.get_post_by_slug(slug) if not post: raise tornado.web.HTTPError(404) msg = self.get_argument("msg", None) user = None if self.current_user: user = userdb.get_user_by_screen_name(self.current_user) # remove dupes from voted_users voted_users = [] for i in post["voted_users"]: if i not in voted_users: voted_users.append(i) post["voted_users"] = voted_users self.render("post/view_post.html", user_obj=user, post=post, msg=msg)
def get(self, username=None): if username is None and self.current_user: username = self.current_user if username != self.current_user: raise tornado.web.HTTPError(401) if self.request.path.find("/user/settings") >= 0: self.redirect('/user/%s/settings' % username) msg = self.get_argument("msg", None) user = userdb.get_user_by_screen_name(self.current_user) if not user: raise tornado.web.HTTPError(404) user['wants_daily_email'] = user.wants_daily_email user['wants_email_alerts'] = user.wants_email_alerts #self.render('user/settings.html', user=user, msg=msg) self.render('user/profile.html', user=user, screen_name=self.current_user, posts=None, section="settings", page=None, per_page=None, tags=None, tag=None, msg=msg)
def subscribe_to_all_your_threads(username): account = userdb.get_user_by_screen_name(username) # todo: get disqus_user_id # temp: nick's ID if 'disqus' not in account: print 'ERROR: no disqus user ID' return print 'subscribing to disqus threads for user %s' % username #make sure all your threads are registered w disqus posts = postsdb.get_posts_by_screen_name(username, per_page=25, page=1) for post in posts: print template_helpers.post_permalink(post) if 'disqus_thread_id_str' not in post.keys() or post.get('disqus_thread_id_str') == "": thread_details = create_thread(post, account['disqus']['access_token']) try: thread_id = thread_details['response']['id'] except: thread = get_thread_details(post) thread_id = thread['response']['id'] post['disqus_thread_id_str'] = thread_id postsdb.save_post(post) subscribe_to_thread(post.get('disqus_thread_id_str'), account['disqus']['access_token']) ''' threads = get_all_threads(account['disqus']['user_id'])['response'] my_threads = [] for thread in threads: if 'link' not in thread or thread['link'] is None: continue if thread['link'].find('http://localhost') >= 0: continue my_threads.append(thread) if 'disqus' in account: for thread in my_threads: subscribe_to_thread(thread['id'], account['disqus']['access_token']) print 'subscribed to thread: %s' % thread['title'] return ''' return
def get(self, slug): # user must be logged in msg = {} if not self.current_user: msg = {'error': 'You must be logged in to bump.', 'redirect': True} else: post = postsdb.get_post_by_slug(slug) if post: can_vote = True for u in post['voted_users']: if u['username'] == self.current_user: can_unbump = True if not can_unbump: msg = {'error': "You can't unbump this post!"} else: user = userdb.get_user_by_screen_name(self.current_user) post['votes'] -= 1 post['voted_users'].remove(user['user']) postsdb.save_post(post) msg = {'votes': post['votes']} self.api_response(msg)
def get(self, screen_name, section="shares"): user = userdb.get_user_by_screen_name(screen_name) if not user: raise tornado.web.HTTPError(404) view = "profile" #section = self.get_argument('section', 'shares') tag = self.get_argument('tag', '') per_page = int(self.get_argument('per_page', 10)) page = int(self.get_argument('page', 1)) if section == 'mentions': # get the @ mention list for this user posts = mentionsdb.get_mentions_by_user(screen_name.lower(), per_page, page) elif section == 'bumps': posts = postsdb.get_posts_by_bumps(screen_name, per_page, page) else: if tag == '': posts = postsdb.get_posts_by_screen_name( screen_name, per_page, page) else: posts = postsdb.get_posts_by_screen_name_and_tag( screen_name, tag, per_page, page) # also get the list of tags this user has put in tags = tagsdb.get_user_tags(screen_name) self.render('user/profile.html', user=user, screen_name=screen_name, posts=posts, section=section, page=page, per_page=per_page, tags=tags, tag=tag, msg=None, view=view)
def post(self): sort_by = self.get_argument('sort_by', 'hot') page = abs(int(self.get_argument('page', '1'))) per_page = abs(int(self.get_argument('per_page', '9'))) is_blacklisted = False msg = 'success' if self.current_user: is_blacklisted = self.is_blacklisted(self.current_user) post = {} post['slug'] = self.get_argument('slug', None) post['title'] = self.get_argument('title', '') post['url'] = self.get_argument('url', '') post['body_raw'] = self.get_argument('body_raw', '') post['tags'] = self.get_argument('tags', '').split(',') post['featured'] = self.get_argument('featured', '') post['has_hackpad'] = self.get_argument('has_hackpad', '') post['slug'] = self.get_argument('slug', '') post['sort_score'] = 0 post['daily_sort_score'] = 0 if post['has_hackpad'] != '': post['has_hackpad'] = True else: post['has_hackpad'] = False deleted = self.get_argument('deleted', '') if deleted != '': post['deleted'] = True post['date_deleted'] = datetime.datetime.now() bypass_dup_check = self.get_argument('bypass_dup_check', '') is_edit = False if post['slug']: bypass_dup_check = "true" is_edit = True dups = [] # make sure user isn't blacklisted if not self.is_blacklisted(self.current_user): # check if there is an existing URL if post['url'] != '': url = urlparse(post['url']) netloc = url.netloc.split('.') if netloc[0] == 'www': del netloc[0] path = url.path if path and path[-1] == '/': path = path[:-1] url = '%s%s' % ('.'.join(netloc), path) post['normalized_url'] = url long_url = post['url'] if long_url.find('goo.gl') > -1: long_url = google.expand_url(post['url']) if long_url.find('bit.ly') > -1 or long_url.find( 'bitly.com') > -1: long_url = bitly.expand_url(post['url'].replace( 'http://bitly.com', '').replace('http://bit.ly', '')) post['domain'] = urlparse(long_url).netloc ok_to_post = True dups = postsdb.get_posts_by_normalized_url( post.get('normalized_url', ""), 1) if post['url'] != '' and len( dups) > 0 and bypass_dup_check != "true": ## ## If there are dupes, kick them back to the post add form ## return (self.render('post/new_post.html', post=post, dups=dups)) # Handle tags post['tags'] = [t.strip().lower() for t in post['tags']] post['tags'] = [t for t in post['tags'] if t] userdb.add_tags_to_user(self.current_user, post['tags']) for tag in post['tags']: tagsdb.save_tag(tag) # format the content as needed post['body_html'] = sanitize.html_sanitize( post['body_raw'], media=self.current_user_can('post_rich_media')) post['body_text'] = sanitize.html_to_text(post['body_html']) post['body_truncated'] = sanitize.truncate(post['body_text'], 500) # determine if this should be a featured post or not if self.current_user_can( 'feature_posts') and post['featured'] != '': post['featured'] = True post['date_featured'] = datetime.datetime.now() else: post['featured'] = False post['date_featured'] = None user = userdb.get_user_by_screen_name(self.current_user) if not post['slug']: # No slug -- this is a new post. # initiate fields that are new post['disqus_shortname'] = settings.get('disqus_short_code') post['muted'] = False post['comment_count'] = 0 post['disqus_thread_id_str'] = '' post['sort_score'] = 0.0 post['downvotes'] = 0 post['hackpad_url'] = '' post['date_created'] = datetime.datetime.now() post['user_id_str'] = user['user']['id_str'] post['username'] = self.current_user post['user'] = user['user'] post['votes'] = 1 post['voted_users'] = [user['user']] #save it post['slug'] = postsdb.insert_post(post) msg = 'success' else: # this is an existing post. # attempt to edit the post (make sure they are the author) saved_post = postsdb.get_post_by_slug(post['slug']) if saved_post and self.current_user == saved_post['user'][ 'screen_name']: # looks good - let's update the saved_post values to new values for key in post.keys(): saved_post[key] = post[key] # finally let's save the updates postsdb.save_post(saved_post) msg = 'success' # log any @ mentions in the post mentions = re.findall(r'@([^\s]+)', post['body_raw']) for mention in mentions: mentionsdb.add_mention(mention.lower(), post['slug']) # Send email to USVers if OP is staff if self.current_user in settings.get('staff'): subject = 'USV.com: %s posted "%s"' % (self.current_user, post['title']) if 'url' in post and post[ 'url']: # post.url is the link to external content (if any) post_link = 'External Link: %s \n\n' % post['url'] else: post_link = '' post_url = "http://%s/posts/%s" % (settings.get('base_url'), post['slug']) text = '"%s" ( %s ) posted by %s. \n\n %s %s' % ( post['title'].encode('ascii', errors='ignore'), post_url, self.current_user, post_link, post.get('body_text', "")) # now attempt to actually send the emails for u in settings.get('staff'): if u != self.current_user: acc = userdb.get_user_by_screen_name(u) if acc: self.send_email('*****@*****.**', acc['email_address'], subject, text) # Subscribe to Disqus # Attempt to create the post's thread acc = userdb.get_user_by_screen_name(self.current_user) thread_id = 0 try: # Attempt to create the thread. thread_details = disqus.create_thread( post, acc['disqus']['access_token']) thread_id = thread_details['response']['id'] except: try: # trouble creating the thread, try to just get the thread via the slug thread_details = disqus.get_thread_details(post) thread_id = thread_details['response']['id'] except: thread_id = 0 if thread_id != 0: # Subscribe a user to the thread specified in response disqus.subscribe_to_thread(thread_id, acc['disqus']['access_token']) # update the thread with the disqus_thread_id_str saved_post = postsdb.get_post_by_slug(post['slug']) saved_post['disqus_thread_id_str'] = thread_id postsdb.save_post(saved_post) if is_edit: self.redirect('/posts/%s?msg=updated' % post['slug']) else: self.redirect('/?msg=success&slug=%s' % post['slug'])
def is_blacklisted(self, screen_name): u = userdb.get_user_by_screen_name(screen_name) if u and 'user' in u.keys() and 'is_blacklisted' in u['user'].keys( ) and u['user']['is_blacklisted']: return True return False
def post(self): next_page = self.get_argument('next', '') next_page += "&finished=true" close_popup = self.get_argument('close_popup', '') email = self.get_argument('email', '') subscribe_to = self.get_argument('subscribe_to', '') error = '' status = '' slug = '' if close_popup != '': status = 'close_popup' # get the current user's email value user = userdb.get_user_by_screen_name(self.current_user) if user: # Clear the existing email address if email == '': if subscribe_to == '': user['email_address'] = '' self.set_secure_cookie('email_address', '') userdb.save_user(user) error = 'Your email address has been cleared.' else: # make sure someone else isn't already using this email existing = userdb.get_user_by_email(email) if existing and existing['user']['id_str'] != user['user'][ 'id_str']: error = 'This email address is already in use.' else: # OK to save as user's email user['email_address'] = email userdb.save_user(user) self.set_secure_cookie('email_address', email) if subscribe_to != '': post = postsdb.get_post_by_slug(subscribe_to) if post: slug = post['slug'] # Attempt to create the post's thread thread_id = 0 try: # Attempt to create the thread. thread_details = disqus.create_thread( post, user['disqus_access_token']) thread_id = thread_details['response']['id'] except: try: # trouble creating the thread, try to just get the thread via the slug thread_details = disqus.get_thread_details( post) thread_id = thread_details['response']['id'] except: thread_id = 0 if thread_id != 0: # Subscribe a user to the thread specified in response disqus.subscribe_to_thread( thread_id, user['disqus_access_token']) #save email prefs user['wants_daily_email'] = self.get_argument('wants_daily_email', False) if user['wants_daily_email'] == "on": user['wants_daily_email'] = True user['wants_email_alerts'] = self.get_argument('wants_email_alerts', False) if user['wants_email_alerts'] == "on": user['wants_email_alerts'] = True userdb.save_user(user) self.redirect("/user/%s/settings?msg=updated" % user['user']['screen_name'])