def temp_image(self, image): web.header("Content-Type", "image/png") web.header("Cache-Control", "no-cache") log.info("200: image/png (temporary)") return image
def GET(self, path=''): import os web.header('Content-type', 'image/vnd.microsoft.icon') return open(os.sep.join([ os.path.dirname(__file__), '..', 'ui', 'data', 'icons', 'bokken.ico' ])).read()
def GET(self): inputs = web.input() web.header("Content-Type", "application/json") try: ## STUB ## # TODO: fill in this stub result = [] result.append({username: "******", price: "1.20", bidtime: "05:14:23 PM"}) result.append({username: "******", price: "1.19", bidtime: "05:14:15 PM"}) result.append({username: "******", price: "1.18", bidtime: "05:14:10 PM"}) result.append({username: "******", price: "1.17", bidtime: "05:14:09 PM"}) result.append({username: "******", price: "1.16", bidtime: "05:14:02 PM"}) result.append({username: "******", price: "1.15", bidtime: "05:13:58 PM"}) result.append({username: "******", price: "1.14", bidtime: "05:13:49 PM"}) result.append({username: "******", price: "1.13", bidtime: "05:13:42 PM"}) result.append({username: "******", price: "1.12", bidtime: "05:13:36 PM"}) result = json.dumps({"result": result}) logging.debug("/auction_recent_bids response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auction_recent_bids response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(id=None, num_bids=None) web.header("Content-Type", "application/json") try: if not inputs.id: raise APIRequestException("No auction ID was supplied in the 'id' parameter.") if inputs.num_bids == None: raise APIRequestException("No number of bids was supplied in the 'num_bids' parameter.") # user validation user_name = user_controller.UserController.validate_cookie() if user_name is None: raise APIRequestException("Not logged in!") result = auction_controller.AuctionController.attach_autobidder( auction_id=int(inputs.id), user_name=user_name, num_bids=inputs.num_bids ) result = json.dumps({"result": result}) logging.debug("/auction_add_pending_bids_for_user response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auction_add_pending_bids_for_user response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(id=None) web.header('Content-Type', 'application/json') try: if not inputs.id: raise APIRequestException( "No auction ID in the 'id' parameter.") # user validation user_name = user_controller.UserController.validate_cookie() if user_name is None: raise APIRequestException("Not logged in.") result = auction_controller.AuctionController.cancel_autobidder( auction_id=int(inputs.id), user_name=user_name) result = json.dumps({'result': result}) logging.debug( "/auction_cancel_pending_bids_for_user response: {}".format( result)) return result except APIRequestException, exception: logging.info( "/auction_cancel_pending_bids_for_user response to bad request: {}" .format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(count=None) web.header('Content-Type', 'application/json') try: if not inputs.count: raise APIRequestException( "The number of auctions to list was not provided in the 'count' parameter." ) auctions = auction_controller.AuctionController.auctions_list_current( inputs.count) # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({'result': result}) logging.debug("/auctions_list_current response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/auctions_list_current response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})
def xml(self, data): web.header("Content-Type", "application/xml") output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" output += data ## should make this use a real library log.info("200: application/xml") return output
def html(self, html): web.header("Content-Type", "text/html") doc = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>" doc += html doc += "</body></html>" log.info("200: text/html") return doc
def GET(self, path=''): import os web.header('Content-type', 'image/vnd.microsoft.icon') return open( os.sep.join([ os.path.dirname(__file__), '..', 'ui', 'data', 'icons', 'bokken.ico' ])).read()
def GET(self): inputs = web.input() web.header("Content-Type", "application/json") try: result = user_controller.UserController.user_authenticate_cookie() result = json.dumps({"result": result}) logging.debug("/user_authenticate_cookie response: {}".format(result)) return result except APIRequestException, exception: logging.info("/user_authenticate_cookie response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input() web.header('Content-Type', 'application/json') try: this_user = user_controller.UserController.validate_cookie() result = user_controller.UserController.user_logout(this_user) result = json.dumps({'result': result}) logging.debug("/user_logout response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/user_logout response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input() web.header("Content-Type", "application/json") try: this_user = user_controller.UserController.validate_cookie() if not this_user: raise APIRequestException("Not logged in!") result = user_controller.UserController.user_info(this_user) result = json.dumps({"result": result}) logging.debug("/user_info response: {}".format(result)) return result except APIRequestException, exception: logging.info("/user_info response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(id=None) web.header("Content-Type", "application/json") try: if not inputs.id: raise APIRequestException("No auction ID was supplied in the 'id' parameter.") auction = auction_controller.AuctionController.auctions_status_by_ids(int(inputs.id)) result = generate_auction_dict(auction) result = json.dumps({"result": result}) logging.debug("/auction_detail response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auction_detail response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(assertion=None) web.header("Content-Type", "application/json") try: this_user = user_controller.UserController.persona_login(inputs.assertion) if this_user: result = {"result": True, "username": this_user.username} else: result = {"result": False, "error": "invalid credentials"} result = json.dumps(result) logging.debug("/persona_login response: {}".format(result)) return result except APIRequestException, exception: logging.info("/persona_login response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(email=None) web.header("Content-Type", "application/json") try: if not inputs.email: raise APIRequestException( 'required string parameter "email" was not passed to the server, or was an empty string' ) result = user_controller.UserController.user_email_exists(inputs.email) result = json.dumps({"result": result}) logging.debug("/user_email_exists response: {}".format(result)) return result except APIRequestException, exception: logging.info("/user_email_exists response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(username=None) web.header("Content-Type", "application/json") try: if inputs.username == None: raise APIRequestException("no user name in the 'username' parameter") if inputs.username == "": raise APIRequestException("username cannot be an empty string") result = user_controller.UserController.user_username_exists(inputs.username) result = json.dumps({"result": result}) logging.debug("/user_username_exists response: {}".format(result)) return result except APIRequestException, exception: logging.info("/user_username_exists response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(assertion=None) web.header('Content-Type', 'application/json') try: this_user = user_controller.UserController.persona_login( inputs.assertion) if this_user: result = {'result': True, 'username': this_user.username} else: result = {'result': False, 'error': 'invalid credentials'} result = json.dumps(result) logging.debug("/persona_login response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/persona_login response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(id=None) web.header("Content-Type", "application/json") try: if not inputs.id: raise APIRequestException("No auction ID was supplied in the 'id' parameter.") # user validation user = user_controller.UserController.validate_cookie() if user is None: raise APIRequestException("Not logged in!") result = auction_controller.AuctionController.auction_bid(inputs.id, user.username) result = json.dumps({"result": result}) logging.debug("/auction_bid response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auction_bid response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(email=None) web.header('Content-Type', 'application/json') try: if not inputs.email: raise APIRequestException( 'required string parameter "email" was not passed to the server, or was an empty string' ) result = user_controller.UserController.user_email_exists( inputs.email) result = json.dumps({'result': result}) logging.debug("/user_email_exists response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/user_email_exists response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(id=None) web.header('Content-Type', 'application/json') try: if not inputs.id: raise APIRequestException( "No auction ID was supplied in the 'id' parameter.") auction = auction_controller.AuctionController.auctions_status_by_ids( int(inputs.id)) result = generate_auction_dict(auction) result = json.dumps({'result': result}) logging.debug("/auction_detail response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/auction_detail response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(ids=None) web.header('Content-Type', 'application/json') auction_ids = inputs.ids try: if not auction_ids: raise APIRequestException( "No auction IDs were supplied in the 'ids' parameter.") # parse the string of IDs into a tuple of ints sids = auction_ids.split(',') if len(sids) > 40: raise APIRequestException( "Maximum of 40 auction statuses, reduce number of requested auction statuses." ) ids = [] for sid in sids: ids.append(int(sid)) ids = tuple(ids) # freeze the ID list auctions = auction_controller.AuctionController.auctions_status_by_ids( ids) # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({"result": result}) logging.debug("/auctions_status_by_id response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/auctions_status_by_id response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input() web.header("Content-Type", "application/json") try: # TODO: check that an administrative user issued this request auctions = auction_controller.AuctionController.auctions_list_all() # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({"result": result}) logging.debug("/auctions_list_all response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auctions_list_all response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(username=None) web.header('Content-Type', 'application/json') try: if inputs.username == None: raise APIRequestException( "no user name in the 'username' parameter") if inputs.username == "": raise APIRequestException('username cannot be an empty string') result = user_controller.UserController.user_username_exists( inputs.username) result = json.dumps({'result': result}) logging.debug("/user_username_exists response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/user_username_exists response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(count=None) web.header("Content-Type", "application/json") try: if not inputs.count: raise APIRequestException("The number of auctions to list was not provided in the 'count' parameter.") auctions = auction_controller.AuctionController.auctions_list_current(inputs.count) # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({"result": result}) logging.debug("/auctions_list_current response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auctions_list_current response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input() web.header('Content-Type', 'application/json') try: # TODO: check that an administrative user issued this request auctions = auction_controller.AuctionController.auctions_list_all() # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({'result': result}) logging.debug("/auctions_list_all response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/auctions_list_all response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})
def GET(self): inputs = web.input(ids=None) web.header("Content-Type", "application/json") auction_ids = inputs.ids try: if not auction_ids: raise APIRequestException("No auction IDs were supplied in the 'ids' parameter.") # parse the string of IDs into a tuple of ints sids = auction_ids.split(",") if len(sids) > 40: raise APIRequestException( "Maximum of 40 auction statuses, reduce number of requested auction statuses." ) ids = [] for sid in sids: ids.append(int(sid)) ids = tuple(ids) # freeze the ID list auctions = auction_controller.AuctionController.auctions_status_by_ids(ids) # Build the JSON payload result = [] for elem in auctions: result.append(generate_auction_dict(elem)) result = json.dumps({"result": result}) logging.debug("/auctions_status_by_id response: {}".format(result)) return result except APIRequestException, exception: logging.info("/auctions_status_by_id response to bad request: {}".format(result)) return json.dumps({"result": False, "error": unicode(exception)})
def image(self, image): web.header("Content-Type", "image/png") web.header("Expires","Thu, 15 Apr 2050 20:00:00 GMT") log.info("200: image/png") return image
def json(self, obj): web.header("Content-Type", "application/json") out = json.dumps(obj) return out.encode('utf-8')
def text(self, string): web.header("Content-Type", "text/plain") log.info("200: text/plain") return string
def csv(self, string, filename): web.header("Content-Type", "text/csv") web.header("Content-Disposition", "attachment; filename=%s" % filename) log.info("200: text/csv") return string
def json(self, data): output = json.dumps(data) web.header("Content-Type", "text/plain") log.info("200: text/plain (JSON)") return output
def render(self, template_name, template_values=None, suffix="html", content_type="text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict @param template_values: Values to include in the template. @type suffix: string @param suffix: Extension of template file. @type content_type: string @param content_type: HTTP header content type to output. @rtype: ? @returns: ? """ if template_values is None: template_values = {} # Set the user object in case it's been created since we initialized. self.setUserObject() # Hand all config values to the template. This method is deprecated # but around until all templates have been updated. config = Config.get_all() config['base_url'] = Config.base_url() for key in config: if type(config[key]) is dict: for param in config[key]: template_values["%s_%s" % (key, param)] = config[key][param] else: template_values[key] = config[key] # Give all config values as a dict in a config space. template_values['config'] = config # Send user data to template if self.user: template_values['user'] = self.user template_values['sqla_user'] = self.sqla_user # Add template data object if self.template_data: template_values['template_data'] = self.template_data # Create full URL from web.py values template_values['full_url'] = web.ctx.home + web.ctx.fullpath # Check for "flash"? if hasattr(self.session, 'flash') and self.session.flash is not None: template_values['flash'] = self.session.flash log.info('showing flash message: "' + self.session.flash + '"') self.session.flash = None self.session.invalidate() template_values['session_id'] = self.session.session_id # Put session values into template ?? keys = self.session.keys() for key in keys: template_values[key] = self.session[key] # Set up template and Jinja template_values['template_name'] = template_name renderer = render_jinja(os.path.dirname(__file__) + '/../templates/', extensions=[ 'jinja2.ext.i18n', 'jinja2.ext.with_', ]) renderer._lookup.filters.update(custom_filters.filters) # Install the translation translation = self.get_gettext_translation(self.get_language()) renderer._lookup.install_gettext_translations(translation) # Insert HTML for the language chooser curr_lang = self.get_language() all_langs = self.get_supported_languages() template_values['language'] = { "current": curr_lang, "list": all_langs.iteritems() } template_values['language_selector'] = self.choice_list( all_langs, curr_lang) # Set HTTP header web.header("Content-Type", content_type) # Debug data. log.info("%s: %s (%s)" % (status, content_type, template_name)) log.info("*** session = %s" % self.session) # Set status web.ctx.status = status # Return template and data. return (renderer[template_name + "." + suffix]( dict(d=template_values))).encode('utf-8')
def render(self, template_name, template_values=None, suffix="html", content_type = "text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict @param template_values: Values to include in the template. @type suffix: string @param suffix: Extension of template file. @type content_type: string @param content_type: HTTP header content type to output. @rtype: ? @returns: ? """ if template_values is None: template_values = {} # Set the user object in case it's been created since we initialized. self.setUserObject() # Hand all config values to the template. This method is deprecated # but around until all templates have been updated. config = Config.get_all() config['base_url'] = Config.base_url() for key in config: if type(config[key]) is dict: for param in config[key]: template_values["%s_%s" % (key, param)] = config[key][param] else: template_values[key] = config[key] # Give all config values as a dict in a config space. template_values['config'] = config # Send user data to template if self.user: template_values['user'] = self.user template_values['sqla_user'] = self.sqla_user # Add template data object if self.template_data: template_values['template_data'] = self.template_data # Create full URL from web.py values template_values['full_url'] = web.ctx.home + web.ctx.fullpath # Check for "flash"? if hasattr(self.session, 'flash') and self.session.flash is not None: template_values['flash'] = self.session.flash log.info('showing flash message: "' + self.session.flash + '"') self.session.flash = None self.session.invalidate() template_values['session_id'] = self.session.session_id # Put session values into template ?? keys = self.session.keys() for key in keys: template_values[key] = self.session[key] # Set up template and Jinja template_values['template_name'] = template_name renderer = render_jinja(os.path.dirname(__file__) + '/../templates/', extensions=['jinja2.ext.i18n', 'jinja2.ext.with_',]) renderer._lookup.filters.update(custom_filters.filters) # Install the translation translation = self.get_gettext_translation(self.get_language()) renderer._lookup.install_gettext_translations(translation) # Insert HTML for the language chooser curr_lang = self.get_language() all_langs = self.get_supported_languages() template_values['language'] = {"current": curr_lang, "list": all_langs.iteritems()} template_values['language_selector'] = self.choice_list( all_langs, curr_lang) # Set HTTP header web.header("Content-Type", content_type) # Debug data. log.info("%s: %s (%s)" % (status, content_type, template_name)) log.info("*** session = %s" % self.session) # Set status web.ctx.status = status # Return template and data. return (renderer[template_name + "." + suffix](dict(d=template_values))).encode('utf-8')
from framework.log import log #from framework.config import * from framework.config import Config #from framework.controller import * import framework.controller import framework.task_manager def reply(user, message): message = clean(message) try: message_id = framework.controller.Controller.get_db().insert("messages", user_id=user.id, message=message, sms=1, outgoing=1, status="queued") except Exception, e: log.error(e) framework.task_manager.Tasks().add(tube='sms', data={'user': user, 'message_id': message_id, 'message': message}, timeout=10) web.header("Content-Type", "text/plain") return '' def send(phone, message): log.info("Sending sms...") message = clean(message) settings = Config.get('twilio') account = twilio.Account(settings['sid'], settings['token']) callback = Config.base_url() if not callback: callback = Config.get('default_host')
def image(self, image): web.header("Content-Type", "image/png") web.header("Expires", "Thu, 15 Apr 2050 20:00:00 GMT") log.info("200: image/png") return image
def GET(self): web.header("Content-Type", "application/json") return json.dumps({"result": {"id": ""}})
def GET(self): web.header('Content-Type', 'application/json') return json.dumps({"result": {"id": ""}})
def GET(self): inputs = web.input() web.header('Content-Type', 'application/json') try: ## STUB ## # TODO: fill in this stub result = [] result.append({ username: '******', price: '1.20', bidtime: '05:14:23 PM' }) result.append({ username: '******', price: '1.19', bidtime: '05:14:15 PM' }) result.append({ username: '******', price: '1.18', bidtime: '05:14:10 PM' }) result.append({ username: '******', price: '1.17', bidtime: '05:14:09 PM' }) result.append({ username: '******', price: '1.16', bidtime: '05:14:02 PM' }) result.append({ username: '******', price: '1.15', bidtime: '05:13:58 PM' }) result.append({ username: '******', price: '1.14', bidtime: '05:13:49 PM' }) result.append({ username: '******', price: '1.13', bidtime: '05:13:42 PM' }) result.append({ username: '******', price: '1.12', bidtime: '05:13:36 PM' }) result = json.dumps({'result': result}) logging.debug("/auction_recent_bids response: {}".format(result)) return result except APIRequestException, exception: logging.info( "/auction_recent_bids response to bad request: {}".format( result)) return json.dumps({"result": False, "error": unicode(exception)})