def get(dtype: Type[T], key: str, default: Optional[T] = None) -> Optional[T]: if (row := db.get(Settings, key)) is None: if default is None: return None row = Settings.create(key, default)
def test_tuple_throughput(self): db = self.db db.open('test_tuple_throughput') db.delete('tuple') result = db.put('tuple', (2,3,4,5,6,)) self.assertTrue(result) result = db.get('tuple') expected = (2, 3, 4, 5, 6) self.assertTupleEqual(result, expected) result = db.append('tuple', 3) expected = b'!:A1D!:2, 3, 4, 5, 6' self.assertEqual(result, expected) result = db.append('tuple', 3) expected = b'!:A1D!:2, 3, 4, 5, 6,3' self.assertEqual(result, expected) result = db.append('tuple', 3) expected = b'!:A1D!:2, 3, 4, 5, 6,3,3' self.assertEqual(result, expected) result = db.append('tuple', 3) expected = b'!:A1D!:2, 3, 4, 5, 6,3,3,3' self.assertEqual(result, expected) result = db.get('tuple') expected = (2, 3, 4, 5, 6, 3, 3, 3, 3,) self.assertTupleEqual(result, expected)
def is_from_authorized_sender(self): """ Determine if the request sender is authorized to send us webhooks. Returns: bool: The request is from an authorized sender. """ manual_flag = int(self.request.args.get('phab', '0')) gitlab = self.request.headers.get('X-Gitlab-Event', '') cnchi = self.request.args.get('cnchi', False) cnchi_version = self.request.headers.get('X-Cnchi-Installer', False) if manual_flag and manual_flag > 0: if self.request.args.get('token', '') == db.get('ANTBS_MANUAL_TOKEN'): self.is_manual = True self.is_authorized = True self.manual_trans_index = manual_flag elif cnchi and cnchi_version and db.get('CNCHI_TOKEN_NEW') == cnchi: self.is_cnchi = cnchi_version self.is_authorized = True elif gitlab and 'Push Hook' == gitlab: self.is_gitlab = True self.is_authorized = True self.changes = [['numix-icon-theme-square']] else: if not db.exists('GITHUB_HOOK_IP_BLOCKS'): # Store the IP address blocks that github uses for webhook requests. hook_blocks = requests.get('https://api.github.com/meta').text db.setex('GITHUB_HOOK_IP_BLOCKS', 42300, hook_blocks) hook_blocks = json.loads(hook_blocks)['hooks'] else: hook_blocks = json.loads( db.get('GITHUB_HOOK_IP_BLOCKS'))['hooks'] for block in hook_blocks: ip = ipaddress.ip_address(self.request.remote_addr) if ipaddress.ip_address(ip) in ipaddress.ip_network(block): # the remote_addr is within the network range of github self.is_github = True self.is_authorized = True break if self.request.headers.get('X-GitHub-Event') == "ping": self.result = json.dumps({'msg': 'Hi!'}) elif self.request.headers.get('X-GitHub-Event') != "push": self.result = json.dumps({'msg': "wrong event type"}) return self.is_authorized
def is_from_authorized_sender(self): """ Determine if the request sender is authorized to send us webhooks. Returns: bool: The request is from an authorized sender. """ manual_flag = int(self.request.args.get('phab', '0')) gitlab = self.request.headers.get('X-Gitlab-Event', '') cnchi = self.request.args.get('cnchi', False) cnchi_version = self.request.headers.get('X-Cnchi-Installer', False) if manual_flag and manual_flag > 0: if self.request.args.get('token', '') == db.get('ANTBS_MANUAL_TOKEN'): self.is_manual = True self.is_authorized = True self.manual_trans_index = manual_flag elif cnchi and cnchi_version and db.get('CNCHI_TOKEN_NEW') == cnchi: self.is_cnchi = cnchi_version self.is_authorized = True elif gitlab and 'Push Hook' == gitlab: self.is_gitlab = True self.is_authorized = True self.changes = [['numix-icon-theme-square']] else: if not db.exists('GITHUB_HOOK_IP_BLOCKS'): # Store the IP address blocks that github uses for webhook requests. hook_blocks = requests.get('https://api.github.com/meta').text db.setex('GITHUB_HOOK_IP_BLOCKS', 42300, hook_blocks) hook_blocks = json.loads(hook_blocks)['hooks'] else: hook_blocks = json.loads(db.get('GITHUB_HOOK_IP_BLOCKS'))['hooks'] for block in hook_blocks: ip = ipaddress.ip_address(self.request.remote_addr) if ipaddress.ip_address(ip) in ipaddress.ip_network(block): # the remote_addr is within the network range of github self.is_github = True self.is_authorized = True break if self.request.headers.get('X-GitHub-Event') == "ping": self.result = json.dumps({'msg': 'Hi!'}) elif self.request.headers.get('X-GitHub-Event') != "push": self.result = json.dumps({'msg': "wrong event type"}) return self.is_authorized
def get(self): user = database.users.get_current_user() li = database.get_current_li() partner = db.get(db.Key.from_path('TrustedPartner', int(cgi.escape(self.request.get('partner_id'))))) if user and li and partner and li.verify_xsrf_token(self): #grab all their items items = db.GqlQuery("SELECT * FROM Item WHERE created_by_id=:1", user.user_id()) item_array = [] for i in items: item_hash = {'price': i.price, 'rating': i.rating, 'description': i.description, 'seller': {'username': li.nickname, 'id': li.user_id},'title': i.title} item_array.append(item_hash) #now generate the JSON hash = {'email': li.email, 'google_user_id': li.user_id, 'name': li.nickname, 'bio': li.desc, 'items': item_array} url = partner.base_url + "/webservices/user_import" try: final = {'user_data': json.dumps(hash), 'auth_token': partner.foreign_auth_token} database.logging.info(final) result = urlfetch.fetch(url=url, method=urlfetch.POST, payload=urllib.urlencode(final), headers={'Content-Type': 'application/x-www-form-urlencoded'}) database.logging.info(result.content); item_contents = json.loads(result.content) except Exception, e: item_contents = None if item_contents['success']: for i in items: i.delete() li.delete() self.redirect('/') return
def detail(key): if request.method == 'GET': val = db.get(key) if val is None: return Renderer('base.html',msg='cannot find: '+key).render(404) return Renderer('detail.html', key=key, val=val).render() if request.method == 'POST': method = request.args.get('method', request.form.get('method')) if method: g.method = method.upper() else: return Renderer('base.html',msg='not supported: POST').render(405) if request.method == 'PUT' or g.method == 'PUT': val = None if request.mimetype == 'application/json': d = request.get_json(force=True) val = d['val'] if request.mimetype == 'application/x-www-form-urlencoded': val = request.form['val'] db.put(key,val) return Renderer('base.html',key=key,val=val).render() if request.method == 'DELETE' or g.method == 'DELETE': if key in db: db.rm(key) return Renderer('base.html',msg='deleted: '+key).render() return Renderer('base.html',msg='not found: '+key).render(404)
def __call__(self, args): for eid in args.task_id: record = db.get(eid=eid) message = "Are you sure you want to delete T{} - {}?".format( eid, record['name']) if self.prompt_confirmation(message): db.remove(eids=[eid]) print("Deleted task T{} - {}".format(eid, record['name']))
def get(self): user = database.users.get_current_user() if user and database.get_current_li().is_admin and database.get_current_li().verify_xsrf_token(self): item_feedback = db.get(db.Key.from_path('LoginInformation', int(cgi.escape(self.request.get('created_by'))), 'ItemFeedback', int(cgi.escape(self.request.get('feedback_id'))))) db.delete(item_feedback) self.redirect(self.request.referer) else: self.redirect('/')
def deactivate(ban_id: int, unban_mod: int = None, unban_reason: str = None): row: Ban = db.get(Ban, ban_id) row.active = False row.deactivation_timestamp = datetime.utcnow() row.unban_mod = unban_mod row.unban_reason = unban_reason
def get(self): user = database.users.get_current_user() if user and database.get_current_li().is_admin and database.get_current_li().verify_xsrf_token(self): feedback_id = cgi.escape(self.request.get('feedback_id')) f = db.get(db.Key.from_path('UserFeedback', int(feedback_id))) db.delete(f) self.redirect(self.request.referer) else: self.redirect('/')
def __call__(self, args): for eid in args.task_id: record = db.get(eid=eid) if args.uncheck: print("Unchecked task T{} - {}".format(eid, record['name'])) db.update({'completed': False}, eids=[eid]) else: print("Completed task T{} - {}".format(eid, record['name'])) db.update({'completed': True}, eids=[eid])
def get(self): user = database.users.get_current_user() current_li = database.get_current_li() collection = db.get(db.Key.from_path('ItemCollection', int(cgi.escape(self.request.get('collection_id'))))) if user and current_li and (collection.created_by_id == user.user_id() or current_li.is_admin): db.delete(collection) self.redirect(self.request.referer) else: self.redirect('/')
def get(self): user = database.users.get_current_user() current_li = database.get_current_li() item = db.get(db.Key.from_path('Item', int(cgi.escape(self.request.get('item_id'))))) if user and current_li and current_li.verify_xsrf_token(self) and item.created_by_id == user.user_id(): item.sold = True item.put() self.redirect(self.request.referer) else: self.redirect('/')
def post(self): if not(authenticate(self.request.get('auth_token'))): render_error(self, "Invalid auth token.") return #fill out the thread first thread = None database.logging.info("Destination : " + self.request.get('destination_conversation_id')) try: if self.request.get('destination_conversation_id'): thread = db.get(db.Key.from_path('Thread', int(cgi.escape(self.request.get('destination_conversation_id'))))) except Exception, e: database.logging.info("invalid destination conversation id: " + self.request.get("destination_conversation_id"))
def get_build_history_chart_data(pkg_obj=None): if pkg_obj is None: builds = status.completed + status.failed chart_data = db.get('antbs:misc:charts:home:heatmap') or False else: builds = pkg_obj.builds chart_data = pkg_obj.heat_map if chart_data and '_' != chart_data: chart_data = json.loads(chart_data) all_builds = sum([ int(num) for num in [chart_data[key]['builds'] for key in chart_data] ]) if len(pkg_obj.builds) > all_builds: chart_data = '[]' timestamps = [] if not chart_data or chart_data in ['[]', '_']: chart_data = dict() builds = [b for b in builds if b] for bld in builds: bld_obj = get_build_object(bnum=bld) if not bld_obj.end_str: continue dt = datetime.strptime(bld_obj.end_str, "%m/%d/%Y %I:%M%p") if datetime_older_than_days(dt): continue key = dt.strftime("%m-%d-%Y") if not chart_data.get(key, False): # chart_data[key] = dict(month=dt.month, day=dt.day, year=dt.year, builds=1, # timestamp=key) chart_data[key] = dict(date=key, builds=1) else: if chart_data[key]['builds'] > 35: continue chart_data[key]['builds'] += 1 if pkg_obj is None: db.setex('antbs:misc:charts:home:heatmap', 10800, json.dumps(chart_data)) else: pkg_obj.heatmap = json.dumps(chart_data) elif isinstance(chart_data, str): chart_data = json.loads(chart_data) for key in chart_data: timestamps.append(chart_data[key]) return json.dumps(timestamps)
def get(self): user = database.users.get_current_user() current_li = database.get_current_li() if user and current_li: item = db.get(db.Key.from_path('Item', int(cgi.escape(self.request.get('item_id'))))) if item.created_by_id == current_li.user_id: token = database.get_current_li().create_xsrf_token() database.render_template(self, 'items/edit_item.html', {'item': item, 'xsrf_token' : token}) else: self.redirect('/') else: self.redirect('/')
def get(self): user = database.users.get_current_user() current_li = database.get_current_li() collection = db.get(db.Key.from_path('ItemCollection', int(cgi.escape(self.request.get('collection_id'))))) if user and current_li and collection.created_by_id == user.user_id(): if len(collection.get_items()) > 0: database.render_template(self, '/items/view_collection.html', {'items': collection.get_items()}) else: db.delete(collection) self.redirect(self.request.referer) else: self.redirect('/')
def get(self): auth_token = cgi.escape(self.request.get('auth_token')) if authenticate(auth_token): item_id = cgi.escape(self.request.get('item_id')) try: item = db.get(db.Key.from_path('Item', int(item_id))) self.response.out.write(json.dumps(item_to_dictionary(item, self))) except ValueError: render_error(self, "item_id does not exist") except AttributeError: render_error(self, "item_id does not exist") else: render_error(self, "authentication failure")
def task_id(arg): if arg[0].upper() != 'T': raise argparse.ArgumentTypeError(arg + ": task IDs must begin with T") try: eid = int(arg[1:]) except ValueError: raise argparse.ArgumentTypeError(arg + ": Task ID # must be an int") if db.get(eid=eid) is None: raise argparse.ArgumentTypeError(arg + ": Task does not exist") else: return eid
def get(self): user = database.users.get_current_user() if user and database.get_current_li().verify_xsrf_token(self): item = db.get(db.Key.from_path('Item', int(cgi.escape(self.request.get('item_id'))))) feedback = db.GqlQuery("SELECT * FROM ItemFeedback WHERE item_id = :1", str(item.key().id())) #make sure the person owns this item or they're an admin if (item.created_by_id == user.user_id()) or (database.get_current_li().is_admin): database.logging.info("Deleting item with id %s by user_id %s", item.key().id(), user.user_id()) database.db.delete(item) for f in feedback: db.delete(f) self.redirect(self.request.referer) else: self.redirect('/')
def get_build_history_chart_data(pkg_obj=None): if pkg_obj is None: builds = status.completed + status.failed chart_data = db.get('antbs:misc:charts:home:heatmap') or False else: builds = pkg_obj.builds chart_data = pkg_obj.heat_map if chart_data and '_' != chart_data: chart_data = json.loads(chart_data) all_builds = sum([int(num) for num in [chart_data[key]['builds'] for key in chart_data]]) if len(pkg_obj.builds) > all_builds: chart_data = '[]' timestamps = [] if not chart_data or chart_data in ['[]', '_']: chart_data = dict() builds = [b for b in builds if b] for bld in builds: bld_obj = get_build_object(bnum=bld) if not bld_obj.end_str: continue dt = datetime.strptime(bld_obj.end_str, "%m/%d/%Y %I:%M%p") if datetime_older_than_days(dt): continue key = dt.strftime("%m-%d-%Y") if not chart_data.get(key, False): # chart_data[key] = dict(month=dt.month, day=dt.day, year=dt.year, builds=1, # timestamp=key) chart_data[key] = dict(date=key, builds=1) else: if chart_data[key]['builds'] > 20: continue chart_data[key]['builds'] += 1 if pkg_obj is None: db.setex('antbs:misc:charts:home:heatmap', 10800, json.dumps(chart_data)) else: pkg_obj.heatmap = json.dumps(chart_data) elif isinstance(chart_data, str): chart_data = json.loads(chart_data) for key in chart_data: timestamps.append(chart_data[key]) return json.dumps(timestamps)
def test_destroy(self): db = self.db db.open('test_wipe') db.put('doggy', (1,2,3)) self.assertTupleEqual(db.get('doggy'), (1,2,3)) db.close() db.open('test_wipe') self.assertTupleEqual(db.get('doggy'), (1,2,3)) db.wipe() db.open('test_wipe') self.assertEqual(db.get('doggy'), None) db.put('doggy', (1,2,3)) db.close() db.open('test_wipe') self.assertTupleEqual(db.get('doggy'), (1,2,3)) db.start = Mock(side_effect=db.start) db.wipe(True) db.start.assert_called() self.assertEqual(db.get('doggy'), None)
def get(self): current_li = database.get_current_li() item = db.get(db.Key.from_path('Item', int(self.request.get('item_id')))) li = db.GqlQuery("SELECT * FROM LoginInformation WHERE user_id = :1", item.created_by_id).get() token = "" if database.users.get_current_user(): token = database.get_current_li().create_xsrf_token() feedback = db.GqlQuery("SELECT * FROM ItemFeedback WHERE item_id = :1 ORDER BY created_at DESC", str(item.key().id())) buyer = database.get_user(item.highest_bid_id) rating = None if current_li: f = database.db.GqlQuery("SELECT * FROM UserFeedback WHERE for_user_id = :1 AND created_by_id = :2", li.user_id, current_li.user_id) if f.count() > 0: rating = int(f.get().rating) database.render_template(self, 'items/view_item.html', {'item': item, 'li': li, 'feedback': feedback, 'buyer': buyer, 'rating':rating, 'xsrf_token' : token})
def test_tuple_put_append_get(self): db = self.db db.open('test_tuples') key = 'poppy' ta = (10,11,12,) tb = (1,2,3) db.delete(key) db.put(key, ta) db.append(key, tb) c_list = db.collect(key, convert=False) self.assertEqual(len(c_list), 1) result = db.get(key) expected = ta + tb self.assertTupleEqual(result, expected)
def get(self): user = database.users.get_current_user() li = database.get_current_li() partner = db.get(db.Key.from_path('TrustedPartner', int(cgi.escape(self.request.get('partner_id'))))) if user and li and partner and li.verify_xsrf_token(self): #grab all their items url = partner.base_url + "/webservices/add_user_rating" try: final = {'target_user_id': self.request.get('target_user_id'), 'user_name': li.nickname, 'user_id': li.user_id, 'rating': int(self.request.get('rating')), 'feedback': 'Rating', 'auth_token': partner.foreign_auth_token} database.logging.info(final) result = urlfetch.fetch(url=url, method=urlfetch.POST, payload=urllib.urlencode(final), headers={'Content-Type': 'application/x-www-form-urlencoded'}) database.logging.info(result.content); item_contents = json.loads(result.content) except Exception, e: item_contents = None
def test_get_last(self): db = self.db db.open('test_delete') db.wipe() db.put('a', 'b') db.put('a', 'e') db.put('b', 'c') db.put('c', 'd') db.put('c', 'e') self.assertEqual(db.get('a'), 'b') self.assertEqual(db.get('a', last=True), 'e') self.assertEqual(db.get('c'), 'd') self.assertEqual(db.get('c', last=True), 'e') self.assertEqual(db.get('b'), 'c') self.assertEqual(db.get('b', last=True), 'c') self.assertNotEqual(db.get('b', last=True), 'a') self.assertNotEqual(db.get('b'), 'a')
def post(self): user = database.users.get_current_user() current_li = database.get_current_li() if user and current_li and current_li.verify_xsrf_token(self): bid = float(cgi.escape(self.request.get('bid'))) item_id = int(cgi.escape(self.request.get('item_id'))) item = db.get(db.Key.from_path('Item', item_id)) if item.highest_bid: if(bid > float(item.highest_bid) and item.bidding_enabled): item.highest_bid = '%.2f' % bid item.highest_bid_id = user.user_id() item.put() else: item.highest_bid = '%.2f' % bid item.highest_bid_id = user.user_id() item.put() self.redirect(self.request.referer) else: self.redirect('/')
def post(self): user = database.users.get_current_user() current_li = database.get_current_li() if user and current_li and current_li.verify_xsrf_token(self): item = db.get(db.Key.from_path('Item', int(cgi.escape(self.request.get('item_id'))))) if item.created_by_id == current_li.user_id: item.title = cgi.escape(database.quick_sanitize(self.request.get('title'))) item.description = cgi.escape(database.sanitizeHTML(self.request.get('description'))) item.bidding_enabled = bool(self.request.get('bidding_enabled')) if (len(item.description) > 40): item.summary = item.description[:40] + "..." else: item.summary = item.description item.price = float('%.2f' % float(cgi.escape(self.request.get('price')))) item.is_active = not bool(self.request.get('show_item')) item.sponsored = bool(self.request.get('sponsored')) if self.request.get('photo'): item.image = database.db.Blob(database.images.resize(self.request.get('photo'), 512, 512)) database.logging.info("Item #%s changed to:\nTitle: %s\nDescription: %s\nPrice: %f", item.key().id(), item.title, item.description, item.price) item.put() self.redirect('/items/my_items') else: self.redirect('/')
def get(self): image_id = cgi.escape(self.request.get('avatar_id')) item_id = cgi.escape(self.request.get('item_id')) if image_id: li = database.db.GqlQuery("SELECT * FROM LoginInformation WHERE user_id = :1", image_id).get() if li.avatar: self.response.headers['Content-Type'] = 'image/png' self.response.out.write(li.avatar) else: self.error(404) elif item_id: item = db.get(db.Key.from_path('Item', int(self.request.get('item_id')))) if item.image: self.response.headers['Content_type'] = 'image/png' self.response.out.write(item.image) else: self.error(404) else: li = database.db.GqlQuery("SELECT * FROM LoginInformation WHERE user_id = :1", database.users.get_current_user().user_id()).get() if li.avatar: self.response.headers['Content-Type'] = 'image/png' self.response.out.write(li.avatar) else: self.error(404)
def deactivate(mute_id: int, unmute_mod: int = None, reason: str = None): row: Mute = db.get(Mute, mute_id) row.active = False row.deactivation_timestamp = datetime.utcnow() row.unmute_mod = unmute_mod row.unmute_reason = reason
def post(post_id: str) -> bool: if db.get(RedditPost, post_id) is not None: return False RedditPost.create(post_id) return True
from utils import batch_sign from database import ( get_pkg_object, db, status ) logger = status.logger TESTING_DIR = os.path.join(status.ISO_DIR, 'testing') RELEASE_DIR = os.path.join(status.ISO_DIR, 'release') SCRIPTS_DIR = os.path.join(status.APP_DIR, 'scripts') PASSWORD = status.gpg_password GPG_KEY = status.gpg_key API_KEY = db.get(status.ANTERGOS_API_DB_KEY_NAME) class ISOUtility: def __init__(self, pkg_obj): self.version = pkg_obj.pkgver self.pkgname = pkg_obj.pkgname self.file_name = self.get_file_name(pkg_obj) self.file_path = os.path.join(TESTING_DIR, self.file_name) self.md5sums_path = os.path.join(TESTING_DIR, 'MD5SUMS-{}'.format(self.version)) self.mirror_url = 'http://mirrors.antergos.com/iso/release/{0}'.format(self.file_name) self.files = [ self.file_path, '{0}.sig'.format(self.file_path), '{0}.torrent'.format(self.file_path) ]
def update(guild_id: int, code: str): row = db.get(AllowedInvite, guild_id) row.code = code
def get(permission: str) -> int: if (row := db.get(PermissionModel, permission)) is None: from util import ADMINISTRATOR row = PermissionModel.create(permission, ADMINISTRATOR)
def set(dtype: Type[T], key: str, value: T) -> "Settings": if (row := db.get(Settings, key)) is None: return Settings.create(key, value)
def set(permission: str, level: int) -> "PermissionModel": if (row := db.get(PermissionModel, permission)) is None: return PermissionModel.create(permission, level)
def change_owner(channel_id: int, owner: int): row: DynamicVoiceChannel = db.get(DynamicVoiceChannel, channel_id) row.owner = owner
def exists(channel_id: int) -> bool: return db.get(LogExclude, channel_id) is not None
def remove(channel_id: int): db.delete(db.get(LogExclude, channel_id))
import subprocess import requests from requests_toolbelt.adapters.source import SourceAddressAdapter from utils import batch_sign from database import (get_pkg_object, db, status) logger = status.logger TESTING_DIR = os.path.join(status.ISO_DIR, 'testing') RELEASE_DIR = os.path.join(status.ISO_DIR, 'release') SCRIPTS_DIR = os.path.join(status.APP_DIR, 'scripts') PASSWORD = status.gpg_password GPG_KEY = status.gpg_key API_KEY = db.get(status.ANTERGOS_API_DB_KEY_NAME) class ISOUtility: def __init__(self, pkg_obj): self.version = pkg_obj.pkgver self.pkgname = pkg_obj.pkgname self.file_name = self.get_file_name(pkg_obj) self.file_path = os.path.join(TESTING_DIR, self.file_name) self.md5sums_path = os.path.join(TESTING_DIR, 'MD5SUMS-{}'.format(self.version)) self.mirror_url = 'http://mirrors.antergos.com/iso/release/{0}'.format( self.file_name) self.files = [ self.file_path, '{0}.sig'.format(self.file_path), '{0}.torrent'.format(self.file_path)