Best Python code snippet using Kiwi_python
requests_wrapper.py
Source:requests_wrapper.py
...3import tempfile4from urllib import parse5from urllib.parse import urlencode6from django.conf import settings7def _get_absolute_url(relative_url, host):8 return parse.urljoin(host, relative_url)9def _debug_log(log):10 if settings.DEBUG:11 #if isinstance(log, bytes):12 # log = log.decode("utf-8")13 print(log)14def local_fail_json(err=0, description=''):15 return {'ok': False, 'reason': {'err': err, 'desc': description}}16def get(relative_url, local_args, args, host=None, format='json'):17 data = {}18 for i in args:19 val = local_args.get(i, None)20 if val is not None:21 #if isinstance(val, unicode):22 # val = val.encode('utf-8')23 data[i] = val24 compiled_url = '%s?%s' % (relative_url, urlencode(data))25 absolute_url = _get_absolute_url(compiled_url, host)26 req = requests.get(absolute_url)27 _debug_log(req.content)28 _debug_log(absolute_url)29 if req.status_code == 200:30 if format == 'json':31 return req.json()32 elif format == 'raw':33 return req.content34 return local_fail_json('20001', 'request error') 35def post(relative_url, locals, args, host=None, format='json'):36 data = {}37 for i in args:38 val = locals.get(i, None)39 if val is not None:40 #if isinstance(val, unicode):41 # val = val.encode('utf-8')42 data[i] = val43 absolute_url = _get_absolute_url(relative_url, host)44 req = requests.post(absolute_url, data=data)45 _debug_log(req.content)46 _debug_log(absolute_url)47 _debug_log(data)48 if req.status_code == 200:49 if format == 'json':50 return req.json()51 elif format == 'raw':52 return req.content53 return local_fail_json('20001', 'request error') 54def post_file(relative_url, locals, args, filename, content, host=None, format='json'):55 data = {}56 for i in args:57 val = locals.get(i, None)58 if val is not None:59 #if isinstance(val, unicode):60 # val = val.encode('utf-8')61 data[i] = val62 absolute_url = _get_absolute_url(relative_url, host)63 temp = tempfile.NamedTemporaryFile(delete=False)64 temp.write(content)65 temp.name = temp.name + "." + filename.split('.')[-1]66 temp.seek(0)67 files = {'filedata': temp}68 req = requests.post(absolute_url, data=data, files=files)69 _debug_log(req.content)70 _debug_log(absolute_url)71 _debug_log(data)72 temp.close()73 if req.status_code == 200:74 if format == 'json':75 return req.json()76 elif format == 'raw':...
admin.py
Source:admin.py
...18 prepopulated_fields = {"slug": ["title"]}19 search_fields = ["title", "slug"]20 list_filter = ["menu"]21 raw_id_fields = ("parent",)22 def _get_absolute_url(self, obj):23 try:24 url = obj.link.get_absolute_url()25 except AttributeError:26 url = None27 if url:28 return "<a href=\"%s\" target=\"public\">%s</a>" % (url, url)29 return "<p class=\"errornote\">Inactive or broken link</p>"30 _get_absolute_url.short_description = "Permalink"31 _get_absolute_url.allow_tags = True32admin.site.register(Menu, MenuAdmin)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!