Example #1
0
def user_preferences(request):
    from raggregate.queries import user_preference as up
    from webob.multidict import MultiDict

    user_id = request.session.get('users.id', None)
    prefs = {}

    if request.POST:
        prefs['link_to_story'] = request.POST.get(
            'prop-link-directly-to-story', 'off')
        prefs['reg_for_notifications'] = request.POST.get(
            'prop-auto-reg-for-notifications', 'off')
        up.set_user_prefs(user_id, prefs)
    else:
        prefs = up.get_user_prefs(user_id)

    return prefs
Example #2
0
def user_info(request):
    import hashlib
    import os
    from raggregate.queries import user_preference as up

    r = request
    ses = request.session
    p = ses['safe_post']

    edit_mode = False
    user_id = None

    if 'user_id' in r.params:
        user_id = r.params['user_id']

    if 'logged_in' in ses and 'user_id' not in r.params:
        user_id = ses['users.id']

    if 'logged_in' in ses and (user_id == str(ses['users.id'])
                               or users.get_user_by_id(
                                   ses['users.id']).is_user_admin()):
        edit_mode = True

    u = users.get_user_by_id(user_id)
    params = up.get_user_prefs(user_id)

    if p and edit_mode:
        dbsession = DBSession()
        u.about_me = p['about_me']
        if p['email'] == "":
            u.email = None
        else:
            u.email = p['email']
        if r.POST['picture'] != '':
            orig_filename = r.POST['picture'].filename
            up_dir = r.registry.settings['user.picture_upload_directory']

            u.picture = users.add_user_picture(orig_filename,
                                               str(u.id)[:7], up_dir,
                                               r.POST['picture'].file)

        dbsession.add(u)

    response = {'edit_mode': edit_mode, 'u': u}
    response.update(params)
    return response
Example #3
0
def check_notify_default(user_id, request):
    """
    Check to see if the user is setup to receive notifications by default.

    Arguments:
        user_id - The ID of the user you want to find notification settings for
        request - The HTTP request object
    """
    from raggregate.queries import user_preference as up

    prefs = up.get_user_prefs(user_id)
    if 'reg_for_notifications' in prefs and prefs['reg_for_notifications'] == 'on':
        return True
    elif 'reg_for_notifications' not in prefs:
        if 'register_notify_by_default' in request.registry.settings \
        and request.registry.settings['register_notify_by_default'] == 'true':
            return True
    else:
        return False
Example #4
0
def check_notify_default(user_id, request):
    """
    Check to see if the user is setup to receive notifications by default.

    Arguments:
        user_id - The ID of the user you want to find notification settings for
        request - The HTTP request object
    """
    from raggregate.queries import user_preference as up

    prefs = up.get_user_prefs(user_id)
    if 'reg_for_notifications' in prefs and prefs[
            'reg_for_notifications'] == 'on':
        return True
    elif 'reg_for_notifications' not in prefs:
        if 'register_notify_by_default' in request.registry.settings \
        and request.registry.settings['register_notify_by_default'] == 'true':
            return True
    else:
        return False
Example #5
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
    }
Example #6
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}