I am currently developing a web app that lets users submit posts and replies, and they can have their own profile which lists all the submitted posts and replies. I am also using X-AppEngine to fetch the location of users and I'm storing the information into the database. Posts, replies, and user information are stored into 'Post', 'Reply', and 'User' entities respectively.

Temperature data of user's location is fetched using the public API and it is not stored in the database as the temperature needs to be updated every time someone visits the site. The get_temp function function simply returns temperature data.

The code is working as I expected but I am hoping to get some feedback for my code in terms of exception handling, efficiency, and cleanliness of the code. Thanks!

def city_temp(posts):
    city_info = {}
    for post in posts:
        temp = get_temp(post.city) 
        if temp:
            city_info[post.city] = temp
            city_info[post.city] = ""
    return city_info

class MainPage(Handler):        
    def get(self):
        posts = Post.query(ancestor=post_key()).order(-Post.created)
        replies = Reply.query(ancestor=reply_key()).order(-Reply.created)
        city_info = city_temp(posts)

        params = dict(posts=posts, replies=replies)

        if city_info:
            params['city_info'] = city_info

        if self.format =='html':
            self.render("front.html", **params)

        elif self.format == 'json':
            self.response.headers['Access-Control-Allow-Origin'] = '*'
            #as_dict() stores each post into JSON format
            return self.render_json([p.as_dict() for p in posts])

    def post(self):
        post_content = self.request.get('newpost')
        reply_content = self.request.get('reply_content')
        city = self.request.headers.get('X-AppEngine-City')
        coords = self.request.headers.get('X-AppEngine-CityLatLong')
        u_id = self.request.get('user_id')
        p_id = self.request.get('post_id')

        if city:
            #format_city is merely for formatting
            city = format_city(city)

        if self.user:
            #self.user information is retrieved from another data model
            user = self.user.name

        if u_id:    
            u_key = ndb.Key('User', int(u_id), parent=users_key())  

        if p_id:
            p_key = ndb.Key('Post', int(p_id), parent=post_key())

        if post_content and self.user:
            p = Post(parent=post_key(), content=post_content, user=user, user_key=u_key, city=city)
            if coords:
                lat, lon = coords.split(",")
                coords = ndb.GeoPt(lat, lon)
                p.coords = coords


        if reply_content and p_key:
            r = Reply(parent=reply_key(), content=reply_content, p_key=p_key, user_key=u_key, user=user)


        elif not self.user:


class Profile(Handler):
    def render_profile(self, profile_id):
        self.redirect('/profile/%s' % str(profile_id))

    def delete_entity(self, entity, e_id, e_parent):
        ndb.Key(entity, int(e_id), parent=e_parent).delete()

    def get(self, profile_id):
        key = ndb.Key('User', int(profile_id), parent=users_key())
        user = key.get()

        reply = Reply.query(Reply.user_key == key, ancestor=reply_key())
        posts = Post.query(Post.user_key == key, ancestor=post_key())

        params = dict(profile_user=user.name)

        if self.user:
            params['current_user'] = self.user.name

        if reply:
            params['reply'] = reply

        if posts:
            params['posts'] = posts

        self.render("profile.html", **params)

    def post(self, profile_id):
        key = ndb.Key('User', int(profile_id), parent=users_key())
        user = key.get()

        p_id = self.request.get('p_id')
        reply_id = self.request.get('reply_id')

        if p_id and self.request.get('delete_post'):
            self.delete_entity('Post', p_id, post_key())

        if reply_id and self.request.get('delete_reply'):
            self.delete_entity('Reply', reply_id, reply_key())


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.