Ejemplo n.º 1
0
 def current_user(self):
     if self.user:
         user = util.model_to_dict(self.user)
         if self.user.church_key:
             user['church'] = model.get_mini_church_info(
                 self.user.church_key)
         self.write_response(user)
     else:
         self.write_response({'status': 'no active user session'})
Ejemplo n.º 2
0
 def update_profile(self):
     # logging.info(self.user)
     try:
         data = self.request_data()
         user = model.update_user(self.user.key.id(), data)
         user = util.model_to_dict(user)
         if self.user.church_key:
             user['church'] = model.get_mini_church_info(
                 self.user.church_key)
         self.write_response(user)
     except Exception as e:
         logging.info(e)
         self.error_response('Update failed..')
Ejemplo n.º 3
0
 def filter_feed(item):
     if item.ref_key.kind() == 'Sermon':
         sermon = item.ref_key.get()
         if user.church_key and user.church_key == sermon.church_key:
             sermon_dict = util.model_to_dict(sermon)
             sermon_dict['kind'] = sermon.key.kind()
             sermon_dict['comments'] = get_comments(sermon.key.kind(),
                                                    sermon.key.id(),
                                                    page_size=5)
             sermon_dict['user'] = User.query(
                 User.key == sermon.created_by).get(projection=[
                     User.first_name, User.last_name, User.profile_photo,
                     User.title
                 ])
             return sermon_dict
Ejemplo n.º 4
0
def _process_note(note):
    logging.info(note)
    note_ = util.model_to_dict(note)
    if note.sermon_key:
        sermon = note.sermon_key.get()
        note_['sermon'] = {
            'id': sermon.key.id(),
            'title': sermon.title,
            'pastor': get_mini_user_info(sermon.created_by),
            'church': get_mini_church_info(sermon.church_key)
        }
    else:
        if note.church_key:
            note_['church'] = get_mini_church_info(note.church_key)
        if note.pastor_key:
            note_['pastor'] = get_mini_user_info(note.pastor_key)
    return note_
Ejemplo n.º 5
0
    def filter(sermon):
        """Filter sermon and add extra data."""
        if sermon.privacy == PRIVACY_MEMBER and (
                not user or user.church_key != sermon.church_key):
            return

        pastor = sermon.created_by.get()
        church = sermon.church_key.get()
        sermon_ = util.model_to_dict(sermon)
        sermon_['pastor'] = {
            'id': pastor.key.id(),
            'title': pastor.title,
            'first_name': pastor.first_name,
            'last_name': pastor.last_name
        }
        sermon_['church'] = {'id': church.key.id(), 'name': church.name}
        return sermon_
Ejemplo n.º 6
0
def get_comment_replies(comment_id, cursor=None, page_size=10):
    """Returns replies to a comment.

    Args:
        comment_id: The id of the comment for which we want the replies.
        cursor: An option value that specifies what point to start fetching the
        replies from.
        page_size: The number of items to return.

    Returns:
        A dict similar the response of the {get_comments} function.
    """
    try:

        q = Comment.query(Comment.reply_to == ndb.Key('Comment', int(
            comment_id))).order(-Comment.created_at)
        if cursor:
            comments, next_curs, more = q.fetch_page(page_size,
                                                     start_cursor=cursor)
        else:
            comments, next_curs, more = q.fetch_page(page_size)

        replies = []
        for c in comments:
            reply = util.model_to_dict(c)
            reply['user'] = User.query(User.key == c.created_by).get(
                projection=[
                    User.first_name, User.last_name, User.title,
                    User.profile_photo
                ])
            replies.append(reply)

        return {
            'comments': replies,
            'next': next_curs.url_safe() if more and next_curs else None
        }
    except Exception as e:
        print e
Ejemplo n.º 7
0
 def _user_response(self, user_object):
     user = util.model_to_dict(user_object)
     if user_object.church_key:
         user['church'] = model.get_mini_church_info(user_object.church_key)
     return user
Ejemplo n.º 8
0
def _process_sermon(sermon):
    sermon_ = util.model_to_dict(sermon)
    sermon_['pastor'] = get_mini_user_info(sermon.created_by)
    sermon_['church'] = get_mini_church_info(sermon.church_key)
    return sermon_
Ejemplo n.º 9
0
def get_comments(type, id, cursor=None, page_size=20):
    """ Returns comments associated with the object type with the input id.

    If a cursor is provided, start querying from the cursor. We also include the
    replies with the result, the replies also paged, so a cursor is returned
    along with the replies for the caller to load more replies.

    Args:
        type: A string representing the Kind of object.
        id: The object id.
        cursor: An optional value that when specified indicates the starting
        point of the fetch.
        page_size: The number of items to return.

    Returns:
        Returns a dict with a key (comments) mapped to the list of comments and
        a key (next) with the cursor value.
        The field comments contains an array of comment objects represent as
        dict. Each comment has the user field with info of the creator,
        as well as field replies that contains dict with the replies to the
        comment.
        Example
        {
          "comments": [
            {
              "like_count": 1,
              "reply_to": null,
              "user": {
                "title": "Pastor",
                "last_name": "Anjorin Jnr",
                "first_name": "Tola",
                "id": 5629499534213120
              },
              ...
               "likes_key": [
                5629499534213120
              ],
              "comment": "This is comment",
              "created_at": 1452982257257,
              "replies": {
                "comments": [
                  {
                    ...
                    "user": {
                      "title": "",
                      "last_name": "Anjorin",
                      "first_name": "Ebby",
                      "id": 5733953138851840
                    },
                    "comment": "reply...",
                  }
                ],
                "next": null
              }
            }
          ],
          "next": null
        }
    """
    q = Comment.query(Comment.ref_key == ndb.Key(type, int(id)),
                      Comment.reply_to == None).order(-Comment.created_at)
    if cursor:
        comments, next_curs, more = q.fetch_page(page_size,
                                                 start_cursor=cursor)
    else:
        comments, next_curs, more = q.fetch_page(page_size)

    data = []
    for c in comments:
        comment = util.model_to_dict(c)
        comment['ref_kind'] = c.ref_key.kind()
        comment['replies'] = get_comment_replies(c.key.id())
        comment['user'] = User.query(User.key == c.created_by).get(projection=[
            User.first_name, User.last_name, User.profile_photo, User.title
        ])
        data.append(comment)

    return {
        'comments': data,
        'next': next_curs.url_safe() if more and next_curs else None
    }
Ejemplo n.º 10
0
    def test_get_feed(self):
        # create some sample feeds.
        u = model.User(first_name='foo', last_name='bar')
        u.key = u.put()
        c = model.Church(name='my church')
        c.key = c.put()
        s1 = model.Sermon(title='sermon 1', church_key=c.key, created_by=u.key)
        s1.key = s1.put()

        s2 = model.Sermon(title='sermon 2', church_key=c.key, created_by=u.key)
        s2.key = s2.put()

        s3 = model.Sermon(title='sermon 3', church_key=c.key, created_by=u.key)
        s3.key = s3.put()

        s = model.Sermon(title='sermon excl',
                         church_key=ndb.Key('Church', '999'))
        s.key = s.put()

        model.Feed(ref_key=s1.key).put()
        model.Feed(ref_key=s2.key).put()
        model.Feed(ref_key=s3.key).put()

        m = model.User(first_name='john', last_name='doe', church_key=c.key)
        m.key = m.put()

        _user = model.User.query(model.User.key == u.key).get(
            projection=[model.User.first_name, model.User.last_name,
                        model.User.title])
        # load first feed
        initial = model.get_feed(m.key.id(), page_size=1)
        data = util.model_to_dict(s3)
        data['user'] = _user
        data['kind'] = 'Sermon'
        data['comments'] = {'comments': [], 'next': None}
        self.assertEquals([data], initial['feeds'])

        # scroll to load more
        more = model.get_feed(m.key.id(), cursor=initial['next'], page_size=1)
        data = util.model_to_dict(s2)
        data['user'] = _user
        data['kind'] = 'Sermon'
        data['comments'] = {'comments': [], 'next': None}
        self.assertEquals([data], more['feeds'])

        # new feed since last fetch
        s4 = model.Sermon(title='sermon 4', church_key=c.key, created_by=u.key)
        s4.key = s4.put()

        model.Feed(ref_key=s4.key).put()

        new = model.get_feed(m.key.id(), last_time=initial['ts'], page_size=1)
        data = util.model_to_dict(s4)
        data['user'] = _user
        data['kind'] = 'Sermon'
        data['comments'] = {'comments': [], 'next': None}
        self.assertEquals([data], new['feeds'])

        # scroll to load more
        more = model.get_feed(m.key.id(), cursor=more['next'], page_size=1)
        data = util.model_to_dict(s1)
        data['user'] = _user
        data['kind'] = 'Sermon'
        data['comments'] = {'comments': [], 'next': None}
        self.assertEquals([data], more['feeds'])
        self.assertIsNone(more['next'])