Beispiel #1
0
 def test_get_user_by_name(self):
     u = users.create_user(username = '******', password='******')
     res = users.get_user_by_name(u.name)
     self.assertEqual(u.id, res.id)
Beispiel #2
0
def epistle(request):
    message = ''
    dbsession = DBSession()
    s = request.session
    p = request.session['safe_post']

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

    if p and 'recipient' in p:
        if p['recipient'] == '' and p['recipient-name'] == '':
            s['message'] = "No recipient provided."
            return {'code': 'ENORECP', 'success': False}
        if p['recipient'] == '':
            # look up recipient-name
            try:
                recp = users.get_user_by_name(p['recipient-name'])
            except sqlalchemy.orm.exc.NoResultFound:
                #@TODO: discuss facebook name sending implications
                s['message'] = "Could not find that user."
                return {'code': 'ENORECP', 'success': False}
        else:
            try:
                recp = users.get_user_by_id(p['recipient'])
            except:
                s['message'] = "Could not find that user."
                return {'code': 'ENORECP', 'success': False}

        if p['subject'] == '':
            subject = None
        else:
            subject = p['subject']

        if 'parent_id' not in p or p['parent_id'] == '':
            parent_id = None
            parent_type = 'epistle'
        else:
            parent_id = p['parent_id']
            parent_obj = queries.find_by_id(parent_id)
            if isinstance(parent_obj, Comment):
                parent_type = 'comment'
                c = Comment(parent_obj.submission_id, s['users.id'], parent_obj.id, p['body'], in_reply_to = parent_obj.user_id)
                dbsession.add(c)
            else:
                parent_type = 'reply'

        if parent_type != 'comment':
            ep = Epistle(recp.id, s['users.id'], p['body'], parent=parent_id, parent_type=parent_type, subject=subject)
            dbsession.add(ep)
        message = 'Message sent.'

    box = request.matchdict['box']

    if box == 'in':
        comments = queries.get_unread_comments_by_user_id(s['users.id'])
    elif box == 'comments':
        comments = queries.get_read_comments_by_user_id(s['users.id'])
    else:
        comments = []

    if box != 'comments':
        ep = queries.get_epistle_roots(id=s['users.id'], target=box)
        epistle_children = {}

        for e in ep:
            e_id = str(e.id)
            epistle_children[e_id] = queries.get_epistle_children(e.id)

        flat_eps = []
        [flat_eps.append(e) for e in _unwrap_list(ep)]
        [flat_eps.append(e) for e in _unwrap_list(epistle_children.values())]

        for e in flat_eps:
            if str(e.recipient) == s['users.id']:
                queries.mark_epistle_read(e)
            e = _assign_epistle_parent(e)

        for c in comments:
            queries.mark_comment_read(c)
    else:
        ep = {}
        epistle_children = {}

    return {'epistles': {'roots': ep, 'children': epistle_children}, 'comments': comments, 'success': True, 'code': 0,}