Example #1
0
    def post(self, lid):

        # the precondtion the max 3 louds.
        loud_count = Loud.query.get_louds().filter(Loud.user.has(User.phone==self.current_user.phone)).count()
        if loud_count >= 3:
            raise HTTPError(412)

        data = self.get_data()
        self.wrap_mars_addr(data)

        loud = Loud()
        loud.user_id = self.current_user.id

        if self.current_user.is_admin:
            # admin's loud
            data['grade'] = 0

        loud.from_dict(data)

        if loud.save():
            self.set_status(201)
            self.set_header('Location', loud.get_link())
            msg = self.message("Created Success.")
        else:
            self.set_status(400)
            msg = self.message("content,lat,lon,address fields are required.")

        # addtional operation add the position
        #self.current_user.last_lat = data['lat']
        #self.current_user.last_lon = data['lon']
        #self.current_user.save()

        self.render_json(msg)
Example #2
0
    def post(self, lid):

        loud_data = self.get_data()
        # if virtual help will 
        if loud_data['loudcate'] == 'virtual':
            loud_data['expired'] += datetime.timedelta(days=365)
        http_client = tornado.httpclient.AsyncHTTPClient()

        mars_location_uri = "%s/e2m/%f,%f" % (options.geo_uri, loud_data['lat'], loud_data['lon'])

        # first request for mars location
        location_rsp = yield gen.Task(http_client.fetch, mars_location_uri)

        loud_data['flat'], loud_data['flon'] = loud_data['lat'], loud_data['lon']
        if not location_rsp.error:
            try:
                geo = self.dejson(location_rsp.body)
                loud_data['flat'], loud_data['flon'] = geo['lat'], geo['lon']
            except (ValueError, TypeError):
                pass

        mars_addr_uri = "%s/m2addr/%f,%f" % (options.geo_uri, loud_data['flat'], loud_data['flon'])

        # second reuest for address for mars location
        addr_rsp = yield gen.Task(http_client.fetch, mars_addr_uri)

        if not addr_rsp.error and addr_rsp.body:
            policital, loud_data['address'] = addr_rsp.body.split('#')

        loud = Loud()
        loud.user_id = self.current_user.id

        #if self.current_user.is_admin:
        #    # admin's loud loud category is 'sys'
        #    loud_data['loudcate'] = 'sys'

        loud.from_dict(loud_data)

        # update the user last location
        self.current_user.lat, self.current_user.lon = loud_data['lat'], loud_data['lon']

        if loud.save():
            self.set_status(201)
            self.set_header('Location', loud.get_link())

            # send to sns
            if loud_data['weibo'] or loud_data['renren'] or loud_data['douban']:
                mqurl = "http://localhost:8888/"

                auth_query = Auth.query.filter(Auth.user_id==self.current_user.id)
                if loud_data['weibo']:
                    try:
                        weibo_auth = auth_query.filter(Auth.site_label==Auth.WEIBO).one()
                    except (NoResultFound, MultipleResultsFound):
                        pass
                    else:
                        sns_data = loud.loud2dict4sns(weibo_auth)
                        sns_data['token'] = weibo_auth.access_token
                        sns_data['secret'] = weibo_auth.access_secret
                        sns_data['label'] = Auth.WEIBO
                        rsp = yield gen.Task(http_client.fetch,
                                mqurl,
                                method='POST',
                                body='queue=snspost&value=%s' % self.json(sns_data),
                                )

                if loud_data['renren']:
                    try:
                        renren_auth = auth_query.filter(Auth.site_label==Auth.RENREN).one()
                    except (NoResultFound, MultipleResultsFound):
                        pass
                    else:
                        sns_data = loud.loud2dict4sns(renren_auth)
                        sns_data['token'] = renren_auth.access_token
                        sns_data['secret'] = renren_auth.access_secret
                        sns_data['label'] = Auth.RENREN
                        rsp = yield gen.Task(http_client.fetch,
                                mqurl,
                                method='POST',
                                body='queue=snspost&value=%s' % self.json(sns_data),
                                )

                if loud_data['douban']:
                    try:
                        douban_auth = auth_query.filter(Auth.site_label==Auth.DOUBAN).one()
                    except (NoResultFound, MultipleResultsFound):
                        pass
                    else:
                        sns_data = loud.loud2dict4sns(douban_auth)
                        sns_data['token'] = douban_auth.access_token
                        sns_data['secret'] = douban_auth.access_secret
                        sns_data['label'] = Auth.DOUBAN
                        rsp = yield gen.Task(http_client.fetch,
                                mqurl,
                                method='POST',
                                body='queue=snspost&value=%s' % self.json(sns_data),
                                )

        else:
            raise HTTPError(500, "Save data error.")

        self.finish()