def find_by_id(id): # @FIXME: make these exceptions specific to the case # where they can run successfully but do not find # the thing we are looking for. This should be really # easy as I think we just need NoResultFound. from raggregate.queries import submission from raggregate.queries import users from raggregate.queries import epistle as epistle_queries try: return submission.get_story_by_id(id) except: pass if users.get_user_by_id(id): return users.get_user_by_id(id) try: return submission.get_comment_by_id(id) except: pass try: return epistle_queries.get_epistle_by_id(id) except: raise
def notify(request): from raggregate.queries import notify as notify_queries s = request.session p = request.session['safe_params'] u = None op = 'add' vote_dict = {} notifyd = notify_queries.get_notify_by_user_id(s['users.id']) notifyd_ids = [ str(i.target_id) for i in notify_queries.get_notify_by_user_id(s['users.id']) ] if 'target_id' in p and 'logged_in' in s: dbsession = DBSession() uid = s['users.id'] to_notify = p['target_id'] if 'op' in p: op = p['op'] if op == 'add': if to_notify not in notifyd_ids: notify_queries.create_notify(uid, to_notify, s['users.id']) s['message'] = 'Successfully notified' elif op == 'del': if to_notify in notifyd_ids: notify_queries.delete_notify(user_id=uid, target_id=to_notify) s['message'] = 'Successfully de-notified' elif 'logged_in' in s: u = users.get_user_by_id(s['users.id']) # the template expects a set of stories to render notifyd_stories = [ submission.get_story_by_id(i.target_id) for i in notifyd if i.target_type == 'submission' ] notifyd_comments = [ submission.get_comment_by_id(i.target_id) for i in notifyd if i.target_type == 'comment' ] if u: vds = [] for i in notifyd_stories: vds.append( users.get_user_votes(s['users.id'], "on_submission", i.id)) for vd in vds: if type(vd) == dict: vote_dict.update(vd) return { 'notifyd_stories': notifyd_stories, 'notifyd_comments': notifyd_comments, 'vote_dict': vote_dict, }
def full(request): message = '' #@TODO: Change this to use slugs instead of literal guids sub_id = request.matchdict['sub_id'] sub_id = submission.get_story_id_from_slug(sub_id) dbsession = DBSession() p = request.session['safe_post'] prm = request.session['safe_params'] s = request.session logged_in = False if 'logged_in' in s: #return {'message': 'Sorry, please log in first.', 'story': {}, 'comments': {}, 'success': False, 'code': 'ENOLOGIN'} logged_in = True # record the comment if 'op' in prm and prm['op'] == 'del' and logged_in: if 'comment_id' in prm: c = submission.get_comment_by_id(prm['comment_id']) if users.is_user_allowed_admin_action( s['users.id'], str(c.id), ): c.deleted = True dbsession.add(c) s['message'] = 'Comment deleted.' if 'op' in prm and prm['op'] == 'edit' and logged_in: if 'comment_id' in prm: c = submission.get_comment_by_id(prm['comment_id']) if users.is_user_allowed_admin_action( s['users.id'], str(c.id), ): c.body = prm['body'] dbsession.add(c) s['message'] = 'Comment updated.' else: if 'description-textarea' in request.session['safe_post'] and logged_in: sub = submission.get_story_by_id(sub_id) if users.is_user_allowed_admin_action(s['users.id'], str(sub.id)): sub.description = prm['description-textarea'] dbsession.add(sub) s['message'] = 'Description updated.' if 'body' in request.session['safe_post'] and logged_in: if p['parent_type'] == 'story': in_reply_to = submission.get_story_by_id( p['comment_parent']).submitter.id elif p['parent_type'] == 'comment': c = submission.get_comment_by_id(p['comment_parent']) in_reply_to = c.user_id c = Comment(sub_id, s['users.id'], p['comment_parent'], prm['body'], in_reply_to=in_reply_to) dbsession.add(c) dbsession.flush() # if enabled default, subscribe user to own comment. # @TODO: make a preference for users to toggle this if general.check_notify_default(s['users.id'], request): notify_queries.create_notify(s['users.id'], c.id, s['users.id']) v = Vote(sub_id, s['users.id'], 1, "comment", c.id) v.direction = 1 dbsession.add(v) notify_queries.fire_to_listeners(p['comment_parent'], s['users.id'], c.id, request) s['message'] = 'Comment added.' #@TODO: Stop using SA queries in views, move them to individual models story = submission.get_story_by_id(sub_id) story.tally_votes() story_vote_dict = {} comment_vote_dict = {} if logged_in: # see queries.py; these two should not be separate. #@FIXME story_vote_dict = users.get_user_votes(s['users.id'], "on_submission", sub_id) comment_vote_dict = users.get_user_votes(s['users.id'], "on_submissions_comments", sub_id) page_num = 1 per_page = 30 if 'sort.comment_default_order' in request.registry.settings: sort = request.registry.settings['sort.comment_default_order'] else: # do NOT change the hardcoded default, change in the ini as above sort = 'top' next_page = None prev_page = None if 'comment_sort' in prm: sort = prm['comment_sort'] if 'page_num' in prm: try: page_num = int(prm['page_num']) except: page_num = 1 # comments returns a dict; see queries.py if 'comment_perma' not in prm: comments = submission.get_comments(sub_id, organize_parentage=True, page_num=page_num, per_page=per_page, sort=sort) else: comments = submission.get_comments(sub_id, organize_parentage=True, page_num=page_num, per_page=per_page, sort=sort, target='comment', target_id=prm['comment_perma']) for c in comments['comments']: #@TODO: Don't do this on every load on a real deployment c.tally_votes() if c.deleted: c.body = '[deleted]' if page_num > 1: prev_page = page_num - 1 if comments['max_comments'] > (page_num * per_page): next_page = page_num + 1 return { 'story': story, 'comments': comments, 'success': True, 'code': 0, 'story_vote_dict': story_vote_dict, 'comment_vote_dict': comment_vote_dict, 'next_page': next_page, 'prev_page': prev_page, 'render_type': story.render_type, }
def full(request): message = '' #@TODO: Change this to use slugs instead of literal guids sub_id = request.matchdict['sub_id'] sub_id = submission.get_story_id_from_slug(sub_id) dbsession = DBSession() p = request.session['safe_post'] prm = request.session['safe_params'] s = request.session logged_in = False if 'logged_in' in s: #return {'message': 'Sorry, please log in first.', 'story': {}, 'comments': {}, 'success': False, 'code': 'ENOLOGIN'} logged_in = True # record the comment if 'op' in prm and prm['op'] == 'del' and logged_in: if 'comment_id' in prm: c = submission.get_comment_by_id(prm['comment_id']) if users.is_user_allowed_admin_action(s['users.id'], str(c.id), ): c.deleted = True dbsession.add(c) s['message'] = 'Comment deleted.' if 'op' in prm and prm['op'] == 'edit' and logged_in: if 'comment_id' in prm: c = submission.get_comment_by_id(prm['comment_id']) if users.is_user_allowed_admin_action(s['users.id'], str(c.id), ): c.body = prm['body'] dbsession.add(c) s['message'] = 'Comment updated.' else: if 'description-textarea' in request.session['safe_post'] and logged_in: sub = submission.get_story_by_id(sub_id) if users.is_user_allowed_admin_action(s['users.id'], str(sub.id)): sub.description = prm['description-textarea'] dbsession.add(sub) s['message'] = 'Description updated.' if 'body' in request.session['safe_post'] and logged_in: if p['parent_type'] == 'story': in_reply_to = submission.get_story_by_id(p['comment_parent']).submitter.id elif p['parent_type'] == 'comment': c = submission.get_comment_by_id(p['comment_parent']) in_reply_to = c.user_id c = Comment(sub_id, s['users.id'], p['comment_parent'], prm['body'], in_reply_to = in_reply_to) dbsession.add(c) dbsession.flush() # if enabled default, subscribe user to own comment. # @TODO: make a preference for users to toggle this if general.check_notify_default(s['users.id'], request): notify_queries.create_notify(s['users.id'], c.id, s['users.id']) v = Vote(sub_id, s['users.id'], 1, "comment", c.id) v.direction = 1 dbsession.add(v) notify_queries.fire_to_listeners(p['comment_parent'], s['users.id'], c.id, request) s['message'] = 'Comment added.' #@TODO: Stop using SA queries in views, move them to individual models story = submission.get_story_by_id(sub_id) story.tally_votes() story_vote_dict = {} comment_vote_dict = {} if logged_in: # see queries.py; these two should not be separate. #@FIXME story_vote_dict = users.get_user_votes(s['users.id'], "on_submission", sub_id) comment_vote_dict = users.get_user_votes(s['users.id'], "on_submissions_comments", sub_id) page_num = 1 per_page = 30 if 'sort.comment_default_order' in request.registry.settings: sort = request.registry.settings['sort.comment_default_order'] else: # do NOT change the hardcoded default, change in the ini as above sort = 'top' next_page = None prev_page = None if 'comment_sort' in prm: sort = prm['comment_sort'] if 'page_num' in prm: try: page_num = int(prm['page_num']) except: page_num = 1 # comments returns a dict; see queries.py if 'comment_perma' not in prm: comments = submission.get_comments(sub_id, organize_parentage=True, page_num = page_num, per_page = per_page, sort = sort) else: comments = submission.get_comments(sub_id, organize_parentage=True, page_num = page_num, per_page = per_page, sort = sort, target = 'comment', target_id = prm['comment_perma']) for c in comments['comments']: #@TODO: Don't do this on every load on a real deployment c.tally_votes() if c.deleted: c.body = '[deleted]' if page_num > 1: prev_page = page_num - 1 if comments['max_comments'] > (page_num * per_page): next_page = page_num + 1 return {'story': story, 'comments': comments, 'success': True, 'code': 0, 'story_vote_dict': story_vote_dict, 'comment_vote_dict': comment_vote_dict, 'next_page': next_page, 'prev_page': prev_page, 'render_type': story.render_type, }