예제 #1
0
def search(request):
    r = request
    ses = r.session
    try:
        sc = request.registry.solr_conn
    except AttributeError:
        r.session['message'] = 'I could not find the search engine.'
        return {'code': 'ENOSOLR', 'success': False}
    search_term = r.params['term']
    q = sc.query()
    for term in search_term.split():
        q = q.query(term)
    res = q.execute()
    stories = []
    vds = []
    vote_dict = {}
    for r in res:
        stories.append(submission.get_story_by_id(r['id']))
        if 'users.id' in ses:
            vds.append(users.get_user_votes(ses['users.id'], "on_submission", r['id']))
    for vd in vds:
        if type(vd) == dict:
            vote_dict.update(vd)
    #queries.update_story_vote_tally(stories)
    return {'res': res, 'stories': stories, 'vote_dict': vote_dict}
예제 #2
0
def search(request):
    r = request
    ses = r.session
    try:
        sc = request.registry.solr_conn
    except AttributeError:
        r.session['message'] = 'I could not find the search engine.'
        return {'code': 'ENOSOLR', 'success': False}
    search_term = r.params['term']
    q = sc.query()
    for term in search_term.split():
        q = q.query(term)
    res = q.execute()
    stories = []
    vds = []
    vote_dict = {}
    for r in res:
        stories.append(submission.get_story_by_id(r['id']))
        if 'users.id' in ses:
            vds.append(
                users.get_user_votes(ses['users.id'], "on_submission",
                                     r['id']))
    for vd in vds:
        if type(vd) == dict:
            vote_dict.update(vd)
    #queries.update_story_vote_tally(stories)
    return {'res': res, 'stories': stories, 'vote_dict': vote_dict}
예제 #3
0
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,
    }
예제 #4
0
def follow(request):
    s = request.session
    p = request.session['safe_params']
    message = ''

    if 'logged_in' not in s:
        s['message'] = 'Sorry, you must be logged in to use the follow feature.'
        return {'success': False, 'code': 'ENOLOGIN'}

    if 'follow_id' in p and 'logged_in' in s:
        dbsession = DBSession()
        #@TODO: replace with model-wide method to get logged-in user object
        u = users.get_user_by_id(s['users.id'])
        to_follow = users.get_user_by_id(p['follow_id'])
        op = 'add'
        if 'op' in p:
            op = p['op']
        if to_follow not in u.follows and op == 'add':
            u.follows.append(to_follow)
            del (s['followed_users'])
            dbsession.add(u)
            message = 'Successfully following {0}'.format(
                to_follow.display_name())
        elif to_follow in u.follows and op == 'del':
            u.follows.remove(to_follow)
            del (s['followed_users'])
            dbsession.add(u)
            message = 'Successfully unfollowed {0}'.format(
                to_follow.display_name())
    elif 'logged_in' in s:
        u = users.get_user_by_id(s['users.id'])

    vds = []
    vote_dict = {}

    if u:
        for i in u.follows:
            for story in i.submissions:
                #@FIXME: this is probably quite slow
                vds.append(
                    users.get_user_votes(u.id, "on_submission", story.id))
        for vd in vds:
            if type(vd) == dict:
                vote_dict.update(vd)

    s['message'] = message
    return {'follows': u.follows, 'vote_dict': vote_dict}
예제 #5
0
def save(request):
    s = request.session
    p = request.session['safe_params']
    u = None
    op = 'add'
    vote_dict = {}

    if 'story_id' in p and 'logged_in' in s:
        dbsession = DBSession()
        u = users.get_user_by_id(s['users.id'])
        to_save = submission.get_story_by_id(p['story_id'])
        if 'op' in p:
            op = p['op']
        if op == 'add':
            if to_save not in u.saved:
                u.saved.append(to_save)
                dbsession.add(u)
            s['message'] = 'Successfully saved {0}'.format(to_save.title)
        elif op == 'del':
            if to_save in u.saved:
                u.saved.remove(to_save)
                dbsession.add(u)
            s['message'] = 'Successfully unsaved {0}'.format(to_save.title)
    elif 'logged_in' in s:
        u = users.get_user_by_id(s['users.id'])

    if u:
        vds = []
        for i in u.saved:
            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 {
        'saved': u.saved,
        'vote_dict': vote_dict,
    }
예제 #6
0
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,
    }
예제 #7
0
def list(request):
    from raggregate.queries import user_preference as up
    s = request.session
    p = request.session['safe_post']
    r = request
    qs = s['safe_get']
    s['message'] = "Post a story."
    dbsession = DBSession()
    stories = None

    filtered_section = None
    section_found = False
    sections = section_queries.get_sections()
    direct_link = False

    if s.get('users.id', None):
        direct_link = True if up.get_user_prefs(s['users.id']).get(
            'link_to_story', 'off') == 'on' else False

    if r.params and 'op' in r.params:
        sub_id = r.params['sub_id']
        if r.params['op'] == 'del' or r.params['op'] == 'hide':
            try:
                story_to_del = submission.get_story_by_id(sub_id)
            except sqlalchemy.orm.exc.NoResultFound:
                story_to_del = None
            if story_to_del:
                if users.is_user_allowed_admin_action(
                        s['users.id'],
                        str(story_to_del.id),
                ):
                    if r.params['op'] == 'del':
                        story_to_del.description = "[deleted]"
                        story_to_del.url = "#"
                        story_to_del.title = "[deleted]"

                    story_to_del.deleted = True
                    dbsession.add(story_to_del)
                    dbsession.flush()
                else:
                    print("Illegal deletion attempted on {0}".format(
                        story_to_del.submitter.id))

    if 'sort.default_order' in r.registry.settings:
        sort = r.registry.settings['sort.default_order']
    else:
        # default to new sort order if server-specific setting doesn't exist
        # this should only be the case on old clones; do NOT remove default_order
        # from the ini just because you want new by default.
        sort = 'new'
    page_num = 1
    per_page = 30
    next_page = None
    prev_page = None

    # only pass through approved sort options
    if 'sort' in qs:
        if qs['sort'] == 'top':
            sort = 'top'
        if qs['sort'] == 'hot':
            sort = 'hot'
        if qs['sort'] == 'contro':
            sort = 'contro'
        if qs['sort'] == 'new':
            sort = 'new'

    if 'page_num' in qs:
        try:
            page_num = int(qs['page_num'])
        except:
            page_num = 1

    if 'section' in qs and qs['section'] == 'all':
        section = 'all'
    else:
        section = None

    if 'section' in qs and qs['section'] != 'all' and qs['section'] != '':
        section = qs['section']
        try:
            section = section_queries.get_section_by_name(section)
            section_found = True
        except sqlalchemy.orm.exc.NoResultFound:
            try:
                section = section_queries.get_section_by_id(section)
                section_found = True
            except:
                from pyramid_tm import transaction
                transaction.abort()
                pass

        # reset section variable to None if we couldn't the named section
        if section_found == False:
            section = None
        else:
            #if we did find something, set filtered_section so that we can
            #reference the filtered section in the template.
            filtered_section = section

    if 'subscribe' in qs and isinstance(section, Section) and 'logged_in' in s:
        if qs['subscribe'] == 'y':
            sub_way = True
        elif qs['subscribe'] == 'n':
            sub_way = False

        sub = sub_queries.create_subscription(s['users.id'], section.id,
                                              sub_way)
        s['message'] = 'Subscription to section {0} updated'.format(
            section.name)


#   @FIXME: make per_page configurable in a safe location
#   it is probably unwise to allow this to be set in the query string
#   because then a malicious user could say per_page = 10000000000
#   and easily launch a DoS via that mechanism.
#   if 'per_page' in qs:
#       per_page = qs['per_page']

    stories = submission.get_story_list(page_num=page_num,
                                        per_page=per_page,
                                        sort=sort,
                                        request=request,
                                        section=section)
    max_stories = stories['max_stories']
    stories = stories['stories']

    # this should be split into its own def under queries.py
    # as it is currently used in at least one other place
    if max_stories > (page_num * per_page):
        next_page = page_num + 1

    if page_num > 1:
        prev_page = page_num - 1

    vote_dict = {}
    subscribed_to_list = []
    if 'logged_in' in s:
        vote_dict = users.get_user_votes(s['users.id'], "on_all_submissions")
        subscribed_to_list = sub_queries.get_subscribed_by_user_id(
            s['users.id'])
    for story in stories:
        #@TODO: Remember to not tally on every load once a real site deploys
        story.tally_votes()
        story.tally_comments()

    print "\n\nsubscribed list: {0}\n\n".format(subscribed_to_list)

    # Get message of the day
    motd = motd_queries.get_random_message()

    return {
        'stories': stories,
        'success': True,
        'code': 0,
        'vote_dict': vote_dict,
        'max_stories': max_stories,
        'prev_page': prev_page,
        'next_page': next_page,
        'sections': sections,
        'filtered_section': section,
        'motd': motd,
        'subscribed_to_list': subscribed_to_list,
        'direct_link': direct_link
    }
예제 #8
0
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, }
예제 #9
0
def list(request):
    from raggregate.queries import user_preference as up
    s = request.session
    p = request.session['safe_post']
    r = request
    qs = s['safe_get']
    s['message'] = "Post a story."
    dbsession = DBSession()
    stories = None

    filtered_section = None
    section_found = False
    sections = section_queries.get_sections()
    direct_link = False

    if s.get('users.id', None):
        direct_link = True if up.get_user_prefs(s['users.id']).get('link_to_story', 'off') == 'on' else False

    if r.params and 'op' in r.params:
        sub_id = r.params['sub_id']
        if r.params['op'] == 'del' or r.params['op'] == 'hide':
            try:
                story_to_del = submission.get_story_by_id(sub_id)
            except sqlalchemy.orm.exc.NoResultFound:
                story_to_del = None
            if story_to_del:
                if users.is_user_allowed_admin_action(s['users.id'], str(story_to_del.id), ):
                    if r.params['op'] == 'del':
                        story_to_del.description = "[deleted]"
                        story_to_del.url = "#"
                        story_to_del.title = "[deleted]"

                    story_to_del.deleted = True
                    dbsession.add(story_to_del)
                    dbsession.flush()
                else:
                    print("Illegal deletion attempted on {0}".format(story_to_del.submitter.id))

    if 'sort.default_order' in r.registry.settings:
        sort = r.registry.settings['sort.default_order']
    else:
        # default to new sort order if server-specific setting doesn't exist
        # this should only be the case on old clones; do NOT remove default_order
        # from the ini just because you want new by default.
        sort = 'new'
    page_num = 1
    per_page = 30
    next_page = None
    prev_page = None

    # only pass through approved sort options
    if 'sort' in qs:
        if qs['sort'] == 'top':
            sort = 'top'
        if qs['sort'] == 'hot':
            sort = 'hot'
        if qs['sort'] == 'contro':
            sort = 'contro'
        if qs['sort'] == 'new':
            sort = 'new'

    if 'page_num' in qs:
        try:
            page_num = int(qs['page_num'])
        except:
            page_num = 1

    if 'section' in qs and qs['section'] == 'all':
        section = 'all'
    else:
        section = None

    if 'section' in qs and qs['section'] != 'all' and qs['section'] != '':
        section = qs['section']
        try:
            section = section_queries.get_section_by_name(section)
            section_found = True
        except sqlalchemy.orm.exc.NoResultFound:
            try:
                section = section_queries.get_section_by_id(section)
                section_found = True
            except:
                from pyramid_tm import transaction
                transaction.abort()
                pass

        # reset section variable to None if we couldn't the named section
        if section_found == False:
            section = None
        else:
            #if we did find something, set filtered_section so that we can
            #reference the filtered section in the template.
            filtered_section = section

    if 'subscribe' in qs and isinstance(section, Section) and 'logged_in' in s:
        if qs['subscribe'] == 'y':
            sub_way = True
        elif qs['subscribe'] == 'n':
            sub_way = False

        sub = sub_queries.create_subscription(s['users.id'], section.id, sub_way)
        s['message'] = 'Subscription to section {0} updated'.format(section.name)

#   @FIXME: make per_page configurable in a safe location
#   it is probably unwise to allow this to be set in the query string
#   because then a malicious user could say per_page = 10000000000
#   and easily launch a DoS via that mechanism.
#   if 'per_page' in qs:
#       per_page = qs['per_page']

    stories = submission.get_story_list(page_num = page_num, per_page = per_page, sort = sort, request = request, section = section)
    max_stories = stories['max_stories']
    stories = stories['stories']

    # this should be split into its own def under queries.py
    # as it is currently used in at least one other place
    if max_stories > (page_num * per_page):
        next_page = page_num + 1

    if page_num > 1:
        prev_page = page_num - 1

    vote_dict = {}
    subscribed_to_list = []
    if 'logged_in' in s:
        vote_dict = users.get_user_votes(s['users.id'], "on_all_submissions")
        subscribed_to_list = sub_queries.get_subscribed_by_user_id(s['users.id'])
    for story in stories:
        #@TODO: Remember to not tally on every load once a real site deploys
        story.tally_votes()
        story.tally_comments()

    print "\n\nsubscribed list: {0}\n\n".format(subscribed_to_list)

    # Get message of the day
    motd = motd_queries.get_random_message()

    return {'stories': stories, 'success': True, 'code': 0, 'vote_dict': vote_dict,
            'max_stories': max_stories, 'prev_page': prev_page, 'next_page': next_page,
            'sections': sections,
            'filtered_section': section, 'motd': motd,
            'subscribed_to_list': subscribed_to_list,
            'direct_link': direct_link}