def make_post_doc(post, author): ''' Take a Post object and turn it into a Solr document. ''' author = post.author site = author.site doc = { 'content_txt_en': post.content, 'id': 'Post:%d' % post.id, 'language_s': post.language, 'last_update_tdt': isodate(post.last_update), 'post_date_tdt': isodate(post.upstream_created), 'post_id_i': post.id, 'profile_id_i': author.id, 'site_name_txt_en': author.site_name(), 'type_s': 'Post', 'username_s': author.username, } if post.latitude is not None and post.longitude is not None: doc['location_p'] = '{},{}'.format(post.latitude, post.longitude) if post.location is not None: doc['location_txt_en'] = post.location return doc
def get_posts(self, id_): ''' Return an array of posts by this profile. **Example Response** .. sourcecode:: json { "posts": [ { "content": "If your #Tor relay is stolen or you lose control of it, please report it so we can blacklist it: https://t.co/imVnrh1FbD @TorProject", "id": 4, "language": "en", "last_update": "2015-08-19T18:17:07", "location": [ null, null ], "upstream_created": "2014-11-07T16:24:05", "upstream_id": "530878388605423616" }, ... ], "username": "******" } :<header Content-Type: application/json :<header X-Auth: the client's auth token :query page: the page number to display (default: 1) :query rpp: the number of results per page (default: 10) :>header Content-Type: application/json :>json list posts Array of post objects. :>json str posts[n].content Text content of the post. :>json int posts[n].id Unique identifier for post. :>json str posts[n].language Language of post, e.g. 'en'. :>json str posts[n].last_update The date and time that this record was updated from the social media site. :>json str posts[n].location 2-element array of longitude and latitude. :>json str posts[n].upstream_created The date this was posted. :>json str posts[n].upstream_id The unique identifier assigned by the social media site. :>json str username Username of the requested profile :>json str site_name Site name associated with the requested profile :>json int total_count Total count of all posts by this profile, not just those displayed on this page :status 200: ok :status 400: invalid argument[s] :status 401: authentication required :status 404: user does not exist ''' page, results_per_page = get_paging_arguments(request.args) profile = g.db.query(Profile).filter(Profile.id == id_).first() if profile is None: raise NotFound('No profile exists for id={}.'.format(id_)) posts = list() post_query = g.db.query(Post) \ .filter(Post.author_id == id_) total_count = post_query.count() post_query = post_query.order_by(Post.upstream_created.desc()) \ .limit(results_per_page) \ .offset((page - 1) * results_per_page) for post in post_query: post_dict = { 'content': post.content, 'id': post.id, 'language': post.language, 'last_update': isodate(post.last_update), 'location': (post.longitude, post.latitude), 'upstream_created': isodate(post.upstream_created), 'upstream_id': post.upstream_id, } if len(post.attachments) > 0: attachment = post.attachments[0] post_dict['attachment'] = { 'mime': attachment.mime, 'name': attachment.name, 'url': url_for('FileView:get', id_=attachment.id) } posts.append(post_dict) return jsonify( posts=posts, site_name=profile.site_name(), total_count=total_count, username=profile.username )
def get_posts(self, id_): ''' Return an array of posts by this profile. **Example Response** .. sourcecode:: json { "posts": [ { "content": "If your #Tor relay is stolen or you lose control of it, please report it so we can blacklist it: https://t.co/imVnrh1FbD @TorProject", "id": 4, "language": "en", "last_update": "2015-08-19T18:17:07", "location": [ null, null ], "upstream_created": "2014-11-07T16:24:05", "upstream_id": "530878388605423616" }, ... ], "username": "******" } :<header Content-Type: application/json :<header X-Auth: the client's auth token :query page: the page number to display (default: 1) :query rpp: the number of results per page (default: 10) :>header Content-Type: application/json :>json list posts: List of post objects. :>json str posts[n].content: Text content of the post. :>json int posts[n].id: Unique identifier for post. :>json str posts[n].language: Language of post, e.g. 'en'. :>json str posts[n].last_update: The date and time that this record was updated from the social media site. :>json str posts[n].location: 2-element array of longitude and latitude. :>json str posts[n].upstream_created: The date this was posted. :>json str posts[n].upstream_id: The unique identifier assigned by the social media site. :>json str username: Username of the requested profile :>json str site_name: Site name associated with the requested profile :>json int total_count: Total count of all posts by this profile, not just those displayed on this page :status 200: ok :status 400: invalid argument[s] :status 401: authentication required :status 404: user does not exist ''' page, results_per_page = get_paging_arguments(request.args) profile = g.db.query(Profile).filter(Profile.id == id_).first() if profile is None: raise NotFound('No profile exists for id={}.'.format(id_)) posts = list() post_query = g.db.query(Post) \ .filter(Post.author_id == id_) total_count = post_query.count() post_query = post_query.order_by(Post.upstream_created.desc()) \ .limit(results_per_page) \ .offset((page - 1) * results_per_page) for post in post_query: post_dict = { 'content': post.content, 'id': post.id, 'language': post.language, 'last_update': isodate(post.last_update), 'location': (post.longitude, post.latitude), 'upstream_created': isodate(post.upstream_created), 'upstream_id': post.upstream_id, } if len(post.attachments) > 0: attachment = post.attachments[0] post_dict['attachment'] = { 'mime': attachment.mime, 'name': attachment.name, 'url': url_for('FileView:get', id_=attachment.id) } posts.append(post_dict) return jsonify( posts=posts, site_name=profile.site_name(), total_count=total_count, username=profile.username )