def to_network(cidr): try: return IPNetwork(cidr) except ValueError as e: raise BadRequest('Value error: {}'.format(e)) except netaddr_core.AddrFormatError as e: raise BadRequest('Format error: {}'.format(e))
def get_argument(self, key, default=NoArgument): args = self.args.get(key, None) if not args: if default == NoArgument: raise BadRequest() else: return default else: if len(args) == 1: return unicode(args[0], "utf8") else: raise BadRequest()
def render_GET(self, request): if len(request.path.split("/")) != 3: raise BadRequest() name = request.path.split("/")[2] if not name: raise BadRequest() file_path = "page/%s" % name if not os.path.isfile(file_path): raise PageNotFound() with open(file_path) as f: context = { "content": markdown_convert_file(f), } return render_template("page.html", request, context)
def render_POST(self, request): board_id = request.get_argument_int("id") query = request.dbsession.query(Board).filter(Board.uid == board_id) result = query.all() if not result: raise BadRequest() board = result[0] name = request.get_argument("name") repr = request.get_argument("repr") repr_order = request.get_argument_int("repr_order", None) classification = request.get_argument("classification") or None description = request.get_argument("description") or None write_group_uid = request.get_argument_int("write_group_uid") comment_group_uid = request.get_argument_int("comment_group_uid") enabled = request.get_argument("enabled") == "True" board.name = name board.repr = repr board.repr_order = repr_order board.classification = classification board.description = description board.write_group_uid = write_group_uid board.comment_group_uid = comment_group_uid board.enabled = enabled request.dbsession.commit() request.redirect("/admin/board") return "redirected"
def acquire(self, network=None, vpc=None, network_or_vpc=None, zone=None, domain=None, vm=None, until_completion=None): data = { 'zoneid': Zones().get_uuid(zone, or_any=True), 'domainid': Domains().get_uuid(domain, or_any=True) } if network: data['networkid'] = Networks().get_uuid(network) elif vpc: data['vpcid'] = VPCs().get_uuid(vpc) elif network_or_vpc: network = Networks().get(network_or_vpc, none_on_not_found=True) if network: if network.get('vpcid'): self.echo('Network is part of vpc: acquiring to vpc.') data['vpcid'] = network['vpcid'] data['networkid'] = network['id'] else: data['vpcid'] = VPCs().get_uuid(network_or_vpc) else: raise BadRequest('Must specify network or vpc.') ip = self.request_entity( self.ACQUIRE, data=data, asynchronous=True, until_completion=until_completion)['ipaddress'] return self.associate(ip, vm) if vm else ip
def create(self, name, display=None, offering='DefaultIsolatedNetworkOfferingWithSourceNatService', cidr=None, vpc=None, acl=None, zone=None, **kwargs): network = { 'name': name, 'displaytext': display or name, 'networkofferingid': NetworkOfferings().get_uuid(offering), 'zoneid': Zones().get_uuid(zone, or_any=True) } if cidr: cidr = self.to_network(cidr) network['gateway'] = str(cidr.ip) network['netmask'] = str(cidr.netmask) if vpc: network['vpcid'] = VPCs().get_uuid(vpc) if not cidr: raise BadRequest('VPC tiers require a CIDR.') if acl: network['aclid'] = NetworkACLLists().get_uuid(acl) network.update(**kwargs) return self.create_entity(network) # (networks-create is synchronous!)
def get_argument_int(self, key, default=NoArgument): try: value = self.get_argument(key, default) return int(value) except ValueError: if default != NoArgument: return default else: raise BadRequest()
def render_GET(self, request): user_id = request.get_argument("user_id") query = request.dbsession.query(User).filter(User.uid == user_id) result = query.all() if not result: raise BadRequest() user = result[0] context = {"user": user} return render_template("profile_popup.html", request, context)
def render_POST(self, request): reply_id = request.get_argument("id") reply = get_reply(request, reply_id) if is_author(request, reply): content = request.get_argument("content") if content.strip(): edit_reply(request, reply, content) request.dbsession.commit() return "reply edit success" else: raise BadRequest() else: raise Unauthorized()
def render_POST(self, request): user_id = request.get_argument_int("user_id") query = request.dbsession.query(User).filter(User.uid == user_id) result = query.all() if not result: raise BadRequest() user = result[0] query = request.dbsession.query(Group).filter(Group.name == "anybody") anybody = query.one() anybody.users.append(user) request.dbsession.commit() request.redirect("/admin/approve") return "redirect"
def render_GET(self, request): board_id = request.get_argument_int("id") query = request.dbsession.query(Board).filter(Board.uid == board_id) result = query.all() if not result: raise BadRequest() board = result[0] groups = request.dbsession.query(Group).all() context = { "board": board, "groups": groups, } return render_template("admin_board_edit.html", request, context)
def render_GET(self, request): chat_id = request.get_argument("id", None) page = request.get_argument("page", None) if not chat_id and not page: raise BadRequest() if chat_id: chat_id = yuzuki_convert_int(chat_id) chats = get_chat_newer_than(request, chat_id) else: page = yuzuki_convert_int(page) chats = get_chat_page(request, page) data = [chat.to_dict() for chat in chats] data = sorted(data, key=lambda c: c["uid"]) request.setNoCache() return json.dumps(data)
def render_POST(self, request): article_id = request.get_argument("id") article = get_article(request, article_id) if is_author(request, article): subject = request.get_argument("subject") content = request.get_argument("content") # no empty subject if subject.strip(): edit_article(request, article, subject, content) request.dbsession.commit() request.redirect("/article/view?id=%s" % article.uid) return "article edit success" else: raise BadRequest() else: raise Unauthorized()
def render_POST(self, request): board_name = request.get_argument("name") board = get_board(request, board_name) if not can_write(request, board): raise Unauthorized() subject = request.get_argument("subject") content = request.get_argument("content") # no empty subject if subject.strip(): article = create_article(request, board, subject, content) request.dbsession.add(article) request.dbsession.commit() request.redirect("/article/view?id=%s" % article.uid) return "article posted" else: raise BadRequest()
def render_POST(self, request): article_id = request.get_argument("article_id") article = get_article(request, article_id) if not can_comment(request, article.board): raise Unauthorized() content = request.get_argument("content") # no empty reply if content.strip(): reply = create_reply(request, article, content) request.dbsession.add(reply) request.dbsession.commit() page = request.get_argument("page", None) redirect = "/article/view?id=%s" % article.uid if page: redirect += "&page=%s" % page request.redirect(redirect) return "success" else: raise BadRequest()
def create(self, keyword, name=None, enable=True, skip_if_exists=False, user_data=True, until_completion=None): if keyword == 'nuage': proposed_name = 'NuageVpcOffering' user_data_provider = 'VpcVirtualRouter' elif keyword == 'nuage_configdrive': proposed_name = 'NuageVpcOfferingWithConfigDrive' user_data_provider = 'ConfigDrive' else: raise BadRequest('Bad keyword {}.'.format(keyword)) if not user_data: proposed_name += 'Basic' name = name or proposed_name if skip_if_exists: vpc_off = VPCOfferings().get(name, none_on_not_found=True) if vpc_off is not None: return vpc_off supported_services = { 'connectivity': 'NuageVsp', 'dhcp': 'NuageVsp', 'sourcenat': 'NuageVsp', 'staticnat': 'NuageVsp', 'networkacl': 'NuageVsp' } if user_data: supported_services['userdata'] = user_data_provider offering = {'name': name, 'displaytext': name, 'ispersistent': True} return self.create_helper(offering, supported_services, enable, asynchronous=True, until_completion=until_completion)
def render_POST(self, request): article_id = request.get_argument("article_id") article = get_article(request, article_id) if not can_comment(request, article.board): raise Unauthorized() content = request.get_argument("content") # no empty reply if content.strip(): reply = create_reply(request, article, content) request.dbsession.add(reply) request.dbsession.commit() page = request.get_argument("page", None) redirect = "/article/view?id=%s" % article.uid if page: redirect += "&page=%s" % page post_messages_to_subscribers(request, article.subscribing_users, u"구독하고 있는 글에 새 댓글이 등록되었습니다.", reply.user, article.subject, content, redirect) request.redirect(redirect) return "success" else: raise BadRequest()
def render_POST(self, request): article_id = request.get_argument("id") article = get_article(request, article_id) if is_author(request, article): subject = request.get_argument("subject") content = request.get_argument("content") # no empty subject if subject.strip(): edit_article(request, article, subject, content) request.dbsession.commit() redirect_url = "/article/view?id=%s" % article.uid request.redirect(redirect_url) post_messages_to_subscribers(request, article.subscribing_users, u"구독하고 있는 글이 수정되었습니다.", article.user, article.subject, article.compiled_content, redirect_url) return "article edit success" else: raise BadRequest() else: raise Unauthorized()
def no_such_entity(self, entity): raise BadRequest('{} {} could not be found.'.format( self.entity, entity))
def show(self, entity): raise BadRequest('No show implemented for {}.'.format(self.entity))
def render_GET(self, request): query_string = request.get_argument("query") search_type = request.get_argument("type", "content") target = request.get_argument("target", "article") if search_type not in ["user", "content"] or\ target not in ["article", "reply"]: raise BadRequest() board_name = request.get_argument("board", None) board = get_board(request, board_name) if board_name else None page = request.get_argument_int("page", 1) if search_type == "content": query_words = ["%" + word + "%" for word in query_string.split(" ")] expr_list = list() if target == "article": for word in query_words: expr_list.append(Article.subject.like(word)) expr_list.append(Article.content.like(word)) query = request.dbsession.query(Article)\ .filter(or_(*expr_list))\ .options(subqueryload(Article.user))\ .options(subqueryload(Article.board)) else: for word in query_words: expr_list.append(Reply.content.like(word)) query = request.dbsession.query(Reply)\ .filter(or_(*expr_list))\ .options(subqueryload(Reply.user)) else: query = request.dbsession.query(User)\ .filter(User.nickname == query_string) result = query.all() target_user = result[0] if result else None if target == "article": query = request.dbsession.query(Article)\ .filter(Article.user == target_user)\ .options(subqueryload(Article.board))\ .options(subqueryload(Article.user)) else: query = request.dbsession.query(Reply)\ .filter(Reply.user == target_user)\ .options(subqueryload(Reply.article) .subqueryload(Article.board)) item_per_page = ARTICLE_PER_PAGE start_idx = item_per_page * (page - 1) end_idx = item_per_page * page items = query[start_idx:end_idx] total_item_count = query.count() page_total = total_item_count / item_per_page if total_item_count % item_per_page != 0: page_total += 1 render_page = "search_article.html"\ if target == "article" else "search_reply.html" context = { "query": query_string, "type": search_type, "target": target, "board": board, "page": page, "page_total": page_total, "items": items, } return render_template(render_page, request, context)
def server_error(job_result): assert job_result and isinstance(job_result, dict) raise BadRequest('Server error: {} {}'.format( job_result.get('errorcode', 'N/A'), job_result.get('errortext', 'N/A')))
def deep_delete(self, entity): raise BadRequest('No deep delete implemented for {}.'.format( self.entity))
def create(self, keyword, name=None, for_vpc=False, enable=True, user_data=True, skip_if_exists=False): shared = False if keyword == 'isolated': proposed_name = ('VpcTierOffering' if for_vpc else 'IsolatedNetOffering') connectivity = 'VpcVirtualRouter' if for_vpc else 'VirtualRouter' user_data_provider = connectivity elif keyword == 'isolated_configdrive': proposed_name = ('VpcTierOfferingWithConfigDrive' if for_vpc else 'IsolatedNetOfferingWithConfigDrive') connectivity = 'VpcVirtualRouter' if for_vpc else 'VirtualRouter' user_data_provider = 'ConfigDrive' elif keyword == 'shared': if for_vpc: raise BadRequest('Shared network cannot be a VPC tier.') proposed_name = 'SharedNetOffering' connectivity = 'VirtualRouter' user_data_provider = connectivity shared = True elif keyword == 'shared_configdrive': if for_vpc: raise BadRequest('Shared network cannot be a VPC tier.') proposed_name = 'SharedNetOfferingWithConfigDrive' connectivity = 'VirtualRouter' user_data_provider = 'ConfigDrive' shared = True elif keyword == 'nuage_isolated': proposed_name = ('NuageVpcTierOffering' if for_vpc else 'NuageIsolatedNetOffering') user_data_provider = ('VpcVirtualRouter' if for_vpc else 'VirtualRouter') connectivity = 'NuageVsp' elif keyword == 'nuage_isolated_configdrive': proposed_name = ('NuageVpcTierOfferingWithConfigDrive' if for_vpc else 'NuageIsolatedNetOfferingWithConfigDrive') user_data_provider = 'ConfigDrive' connectivity = 'NuageVsp' elif keyword == 'nuage_shared': if for_vpc: raise BadRequest('Shared network cannot be a VPC tier.') proposed_name = 'NuageSharedNetOffering' user_data_provider = 'VirtualRouter' shared = True connectivity = 'NuageVsp' elif keyword == 'nuage_shared_configdrive': if for_vpc: raise BadRequest('Shared network cannot be a VPC tier.') proposed_name = 'NuageSharedNetOfferingWithConfigDrive' user_data_provider = 'ConfigDrive' shared = True connectivity = 'NuageVsp' else: raise BadRequest('Bad keyword {}.'.format(keyword)) if not user_data: proposed_name += 'Basic' name = name or proposed_name if skip_if_exists: net_off = NetworkOfferings().get(name, none_on_not_found=True) if net_off is not None: return net_off supported_services = { 'dhcp': connectivity, } if user_data: supported_services['userdata'] = user_data_provider if connectivity == 'NuageVsp': supported_services['connectivity'] = connectivity if not shared: # supported_services['dns'] = 'VirtualRouter' supported_services['sourcenat'] = connectivity supported_services['staticnat'] = connectivity if for_vpc: supported_services['networkacl'] = connectivity else: supported_services['firewall'] = connectivity offering = { 'name': name, 'displaytext': name, 'forvpc': for_vpc, 'traffictype': 'guest', 'guestiptype': 'shared' if shared else 'isolated' } if for_vpc: offering['ispersistent'] = True # most logical choice offering['conservemode'] = False elif shared: offering['specifyvlan'] = False # Nuage has foreseen ability to # set this to False offering['specifyipranges'] = True # must be True for Shared off. offering['ispersistent'] = False # must be False for Shared off. if connectivity == 'NuageVsp': offering['servicecapabilitylist[0].service'] = \ 'Connectivity' offering['servicecapabilitylist[0].provider'] = connectivity offering['servicecapabilitylist[0].capabilitytype'] = \ 'PublicAccess' offering['servicecapabilitylist[0].capabilityvalue'] = 'True' # else: # isolated net # offering['ispersistent'] = False # most logical choice return self.create_helper(offering, supported_services, enable)
def request(self, cmd, args, exit_on_bad_request=False, truncate_error=True): if not self.session: self.session = requests.Session() self.session.mount('https://', SSLAdapter(ssl.PROTOCOL_TLSv1)) http_cmd = 'POST' if cmd == 'login' else 'GET' args = self.prep_args(cmd, args) resp = None def sign_request(params, secret_key): self.trace('Signing request') request = zip(params.keys(), params.values()) request.sort(key=lambda x: x[0].lower()) hash_str = '&'.join([ '='.join([ r[0].lower(), urllib.quote_plus(str(r[1]), safe='*').lower().replace( '+', '%20').replace('%3A', ':') ]) for r in request ]) return base64.encodestring( hmac.new(secret_key, hash_str, hashlib.sha1).digest()).strip() if self.api_key: assert self.secret_key args['apikey'] = self.api_key args['signature'] = sign_request(args, self.secret_key) elif cmd != 'login': self.password_login() args['sessionkey'] = self.session_key try: resp = self.http_request(http_cmd, args) result = resp.text if resp.status_code == 200: # success error = None elif resp.status_code == 401: # auth issue error = '401 Authentication error' elif resp.status_code == 405: error = ('Method not allowed, unauthorized access on URL: %s' % self.url) elif resp.status_code == 531: error = ('Error authenticating at %s using username: %s,' 'password: %s, domain: %s' % (self.url, self.username, self.password, self.domain)) else: error_msg = resp.headers.get('X-Description') if truncate_error: error_msg = error_msg.split(': {')[0] error = '{0}: {1}'.format(resp.status_code, error_msg) except requests.exceptions.ConnectionError as e: raise BadRequest('Connection refused by server: %s' % e) except Exception as pokemon: result = None error = pokemon.message if error is not None: self.debug('Error: {}'.format(error)) if result: try: response = json.loads(result, "utf-8") self.trace('RESPONSE: %s: %s' % (resp.status_code, response)) except ValueError as e: self.error('Received: {}'.format(result)) response = None error = e else: response = None if response and isinstance(response, dict): m = list(v for v in response.keys() if 'response' in v.lower()) if not m: error = 'Invalid response received: %s' % response else: response = response[filter(lambda x: 'response' in x, response.keys())[0]] if error: if exit_on_bad_request: self.error(error) else: raise BadRequest(error) else: return response
def getChildWithDefault(self, path, request): if len(request.path.split("/")) != 3: raise BadRequest() return self
def no_unique_entity(self, entity): raise BadRequest('{} {} is not unique, please use id.'.format( self.entity, entity))
def yuzuki_convert_int(num_str): try: value = int(num_str) return value except ValueError: raise BadRequest()
def run_app(data_dir): chan_fp = os.path.join(data_dir, 'channels.json') uploads_fp = os.path.join(data_dir, 'uploads.json') data = jsonparse.load_json(chan_fp) full_video_list = [] # probably needs to be changed full_uploads_dict = {} for key in data.keys(): chan = Channel(key) grid_data = youtubescraper.scraper(chan.ytlink) vids = jsonparse.parser(grid_data) if chan.searchterms: sub_dict = {} m_elems = [] for term in chan.searchterms: term_vids = chan.search_titles(vids, term) m_type = term_vids.pop(0) if term_vids: sub_dict[term] = term_vids[0]['id'] elem = message.channel_entry(term_vids, m_type) m_elems.append(elem) else: sub_dict[term] = chan.check_last_upload(term) if m_elems: m = message.s_concatenate(m_elems) else: m = None else: sub_dict = {} all_vids = chan.search_titles(vids) m_type = all_vids.pop(0) m_elems = [] if all_vids: sub_dict[chan.empty_search] = all_vids[0]['id'] elem = message.channel_entry(all_vids, m_type) m_elems.append(elem) else: sub_dict[chan.empty_search] = chan.check_last_upload( chan.empty_search) if m_elems: m = m_elems.pop() else: m = None full_uploads_dict[chan.name] = sub_dict if m: com = message.channel_header(chan.name) + m full_video_list.append(com) if full_video_list: message_full = message.s_concatenate(full_video_list) try: res = telegram.telegram_bot_sendtext(message_full) if BadRequest.check_for_err(res): raise BadRequest(res) else: jsonparse.save_json(full_uploads_dict, uploads_fp) except BadRequest as e: print('\nERROR -- {}'.format(e)) # DEBUG print(' -- message contents:') # DEBUG print(message_full) # DEBUG