def export_as_csv(modeladmin, request, queryset): """ Generic csv export admin action. based on http://djangosnippets.org/snippets/2369/ """ opts = modeladmin.model._meta field_names = set([field.name for field in opts.fields]) many_to_many_field_names = set([many_to_many_field.name for many_to_many_field in opts.many_to_many]) if fields: fieldset = set(fields) field_names = field_names & fieldset elif exclude: excludeset = set(exclude) field_names = field_names - excludeset response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_') writer = csv.writer(response) if header: writer.writerow(list(chain(field_names, many_to_many_field_names))) for obj in queryset: row = [] for field in field_names: row.append(unicode(getattr(obj, field))) for field in many_to_many_field_names: row.append(unicode(getattr(obj, field).all())) writer.writerow(row) return response
def parse_tester(self, svn_name, ext_spaces): path = os.path.join("setuptools", "tests", "svn_data", svn_name + "_ext_list.xml") example_base = svn_name + "_example" data = _read_utf8_file(path) if ext_spaces: folder2 = "third party2" folder3 = "third party3" else: folder2 = "third_party2" folder3 = "third_party3" expected = set( [ os.sep.join((example_base, folder2)), os.sep.join((example_base, folder3)), # folder is third_party大介 os.sep.join((example_base, unicode("third_party") + unichr(0x5927) + unichr(0x4ECB))), os.sep.join((example_base, "folder", folder2)), os.sep.join((example_base, "folder", folder3)), os.sep.join((example_base, "folder", unicode("third_party") + unichr(0x5927) + unichr(0x4ECB))), ] ) expected = set(os.path.normpath(x) for x in expected) dir_base = os.sep.join(("C:", "development", "svn_example")) self.assertEqual(set(x for x in svn_utils.parse_externals_xml(data, dir_base)), expected)
def parse_tester(self, svn_name, ext_spaces): path = os.path.join('setuptools', 'tests', 'svn_data', svn_name + '_ext_list.xml') example_base = svn_name + '_example' data = _read_utf8_file(path) if ext_spaces: folder2 = 'third party2' folder3 = 'third party3' else: folder2 = 'third_party2' folder3 = 'third_party3' expected = set([ os.sep.join((example_base, folder2)), os.sep.join((example_base, folder3)), # folder is third_party大介 os.sep.join((example_base, unicode('third_party') + unichr(0x5927) + unichr(0x4ecb))), os.sep.join((example_base, 'folder', folder2)), os.sep.join((example_base, 'folder', folder3)), os.sep.join((example_base, 'folder', unicode('third_party') + unichr(0x5927) + unichr(0x4ecb))), ]) expected = set(os.path.normpath(x) for x in expected) dir_base = os.sep.join(('C:', 'development', 'svn_example')) self.assertEqual(set(x for x in svn_utils.parse_externals_xml(data, dir_base)), expected)
def parse_tester(self, svn_name, ext_spaces): path = os.path.join('setuptools', 'tests', 'svn_data', svn_name + '_ext_list.xml') example_base = svn_name + '_example' data = _read_utf8_file(path) if ext_spaces: folder2 = 'third party2' folder3 = 'third party3' else: folder2 = 'third_party2' folder3 = 'third_party3' expected = set([ os.sep.join((example_base, folder2)), os.sep.join((example_base, folder3)), # folder is third_party大介 os.sep.join( (example_base, unicode('third_party') + unichr(0x5927) + unichr(0x4ecb))), os.sep.join((example_base, 'folder', folder2)), os.sep.join((example_base, 'folder', folder3)), os.sep.join( (example_base, 'folder', unicode('third_party') + unichr(0x5927) + unichr(0x4ecb))), ]) expected = set(os.path.normpath(x) for x in expected) dir_base = os.sep.join(('C:', 'development', 'svn_example')) self.assertEqual( set(x for x in svn_utils.parse_externals_xml(data, dir_base)), expected)
def test_update_invalid_rentable(self): print("*****************************************************") print("Test the creation of an invalid rentable using a POST") print("*****************************************************") # Create Rentable url = reverse('rentableList') data = {'store': None, 'type': unicode('WaveRunner'), 'isRented': True, 'dateRented': unicode('2015-03-11T23:29:56.947000Z'), 'dateDue': unicode('2015-03-11T23:29:56.947000Z'), 'dateReturned': unicode('2015-03-11T23:29:56.947000Z'),} response = self.client.post(url, data, format='json') # Edit rentable url = reverse('rentable', args="1") data = {'store': '', 'type': '', 'isRented': '', 'dateRented': '', 'dateDue': '', 'dateReturned': '',} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) print("Expected Status Code %s" % (status.HTTP_400_BAD_REQUEST)) print("Returned Status Code %s" % (response.status_code))
def export_as_csv(modeladmin, request, queryset): """ Generic csv export admin action. based on http://djangosnippets.org/snippets/2369/ """ opts = modeladmin.model._meta field_names = set([field.name for field in opts.fields]) many_to_many_field_names = set([ many_to_many_field.name for many_to_many_field in opts.many_to_many ]) if fields: fieldset = set(fields) field_names = field_names & fieldset elif exclude: excludeset = set(exclude) field_names = field_names - excludeset response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename=%s.csv' % unicode( opts).replace('.', '_') writer = csv.writer(response) if header: writer.writerow(list(chain(field_names, many_to_many_field_names))) for obj in queryset: row = [] for field in field_names: row.append(unicode(getattr(obj, field))) for field in many_to_many_field_names: row.append(unicode(getattr(obj, field).all())) writer.writerow(row) return response
def field_hidden_representation(self, field_name, value): field = self.get_field(field_name) if isinstance(field, django.db.models.fields.related.ForeignKey): return unicode(value.id) elif isinstance(field, django.db.models.fields.DateField): return value.strftime('%Y%m%d') else: return unicode(value)
def render_instance(self, inst, cellindex): if inst is None: return u'' fieldvalue = getattr(inst, self.fieldname) field = self.get_field() if isinstance(field, django.db.models.fields.DecimalField): format = u"%." + unicode(field.decimal_places) + "f" return format % fieldvalue else: return unicode(fieldvalue)
def test_get_store_detail(self): print("***************************************************") print("Test using a GET to get a store by a specific pk") print("***************************************************") createUrl = reverse('storeList') data = {'name': unicode('Hooters'), 'address': unicode('123 FunLane'), 'phoneNum': unicode('850-342-8543')} postResponse = self.client.post(createUrl, data, format='json') url = reverse('store', args="1") getResponse = self.client.get(url, format='json') print("Expected: %s" % postResponse.data) print("Returned: %s" % getResponse.data) self.assertEqual(getResponse.data, postResponse.data)
def test_create_store(self): print("********************************************") print("Test the creation of a store using a POST") print("********************************************") url = reverse('storeList') data = {'name': unicode('Hooters'), 'address': unicode('123 Fun Lane'), 'phoneNum': unicode('850-374-9283')} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) print("Expected Status Code %s" % (status.HTTP_201_CREATED)) print("Returned Status Code %s" % (response.status_code)) self.assertEqual(response.data, data) print(response.data)
def test_store_list(self): url = reverse('storeList') print("***********************************") print("Test using a GET to get all objects") print("***********************************") for x in range(0, 5): data = {'name': unicode('Hooters'), 'address': unicode('123 Fun Lane'), 'phoneNum': unicode('850-657-9384')} self.client.post(url, data, format='json') getResponse = self.client.get(url, format='json') print("Number of returned objects") print("Expected 5") print("Returned %s" % (len(getResponse.data))) self.assertEqual(len(getResponse.data), 5)
def hidden_inputs(self): rs = [] # fixed fixed = [(field, value) for field, value in self.data.fixed_fields] for name, value in fixed: fieldval = unicode(value) if self.is_foreignkey(name): fieldval = unicode(value.id) rs.append('<input type="hidden" name="%s_fixed_%s" value="%s">' % (self.prefix, name, fieldval)) return mark_safe(u"\n".join(rs))
def contact_form(request): if request.user.is_authenticated(): if request.POST: form = ContactForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): for team in request.user.team_set.all(): team.user.remove(request.user) for team in form.cleaned_data['teams']: team.user.add(request.user) form.save() else: if request.is_ajax(): errors_dict = {} if form.errors: for error in form.errors: e = form.errors[error] errors_dict[error] = unicode(e) return HttpResponseBadRequest(json.dumps(errors_dict)) else: form = ContactForm( instance=request.user, initial={'teams': [team.id for team in request.user.team_set.all()]}) return render(request, "contact_form.html", {'form': form}) raise PermissionDenied
def upload_to_sandbox(path): print('File \'%s\' uploading started...' % path.replace('api/test/../../', '')) start_time = str(int(time.time())) session = botocore.session.get_session() session.set_credentials(access_key="AKIAIA5IPHVWBGQSSBBA", secret_key="uhsuCK/U8hBWYkMHPoB9dGFJHxjTFK4y63cx3D/y") client = session.create_client('s3', region_name='us-west-2') f = open(path, 'rb') key = '/'.join(unicode(uuid4()).split('-') + [os.path.basename(path)]) response = client.put_object( Body=f.read(), Bucket='bkstg-sandbox', Key=key, ContentType=mimetypes.guess_type(path)[0] ) end_time = str(int(time.time())) print('File was uploaded in %s seconds.\n' % (int(end_time) - int(start_time))) return key # if __name__ == '__main__': # Upload.upload_to_sandbox(path=TestData.image_path)
def test_get_rental_detail(self): print("***************************************************") print("Test using a GET to get a rental by a specific pk") print("***************************************************") createUrl = reverse('rentalList') data = {'cusName': unicode('Jack Daniels'), 'cusPhoneNum': unicode('850-342-9485'), 'cusEmail': unicode('*****@*****.**'), 'price': 23.54, 'rentable': None} postResponse = self.client.post(createUrl, data, format='json') url = reverse('rental', args="1") getResponse = self.client.get(url, format='json') print("Expected: %s" % postResponse.data) print("Returned: %s" % getResponse.data) self.assertEqual(getResponse.data, postResponse.data)
def test_create_rental(self): print("********************************************") print("Test the creation of a rental using a POST") print("********************************************") url = reverse('rentalList') data = {'cusName': unicode('Jack Daniels'), 'cusPhoneNum': unicode('850-342-9485'), 'cusEmail': unicode('*****@*****.**'), 'price': 23.54, 'rentable': None} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) print("Expected Status Code %s" % status.HTTP_201_CREATED) print("Returned Status Code %s" % response.status_code) self.assertEqual(response.data, data) print(response.data)
def save_cells(self, cix, inputs): instance_id = inputs.values()[0][0] assert all([v[0] == instance_id for v in inputs.values()]) # Validate all first valuedict = {} had_errors = False for cellix, inputdata in inputs.iteritems(): instance_id, valuestr = inputdata try: value = self.validate_cell(cellix, valuestr) default = self.default_for_cell(cellix) valuedict[cellix] = (value, default) except exceptions.ValidationError as err: self.cell_errors[cellix] = CellError(err, cellix, valuestr) had_errors = True except Exception as err: logger.error("Error: save_cells: validate_cell %s %s => %s " % (str(cellix), valuestr, unicode(err))) raise err if had_errors: return self.data.save_many(instance_id, valuedict)
def get_hotel_details(self): hoteldetais_json = '' try: # 获取酒店详情 hoteldetails_tags = self.soup.find_all(find_hoteldetails_tag) hoteldetails_p_list = hoteldetails_tags[0].find_all('p') hoteldetais = {} for p in hoteldetails_p_list: key = unicode(p.b.string) value = unicode(p.b.next_sibling.next_sibling.next_sibling.string) hoteldetais[key] = value hoteldetais_json = json.dumps(hoteldetais, ensure_ascii=False) # print(hoteldetais_json) except BaseException as e: print('酒店详情信息解析失败') finally: return hoteldetais_json
def test_get_rentable_detail(self): print("***************************************************") print("Test using a GET to get a rentable by a specific pk") print("***************************************************") createUrl = reverse('rentableList') data = {'store': None, 'type': unicode('WaveRunner'), 'isRented': True, 'dateRented': unicode('2015-03-11T23:29:56.947000Z'), 'dateDue': unicode('2015-03-11T23:29:56.947000Z'), 'dateReturned': unicode('2015-03-11T23:29:56.947000Z')} postResponse = self.client.post(createUrl, data, format='json') url = reverse('rentable', args="1") getResponse = self.client.get(url, format='json') print("Expected: %s" % postResponse.data) print("Returned: %s" % getResponse.data) self.assertEqual(getResponse.data, postResponse.data)
def test_rental_list(self): url = reverse('rentalList') print("***********************************") print("Test using a GET to get all objects") print("***********************************") for x in range(0, 5): data = {'cusName': unicode('Jack Daniels'), 'cusPhoneNum': unicode('850-342-9485'), 'cusEmail': unicode('*****@*****.**'), 'price': 23.54, 'rentable': None} self.client.post(url, data, format='json') getResponse = self.client.get(url, format='json') print("Number of returned objects") print("Expected 5") print("Returned %s" % (len(getResponse.data))) self.assertEqual(len(getResponse.data), 5)
def test_create_rentable(self): print("********************************************") print("Test the creation of a rentable using a POST") print("********************************************") url = reverse('rentableList') data = {'store': None, 'type': unicode('WaveRunner'), 'isRented': True, 'dateRented': unicode('2015-03-11T23:29:56.947000Z'), 'dateDue': unicode('2015-03-11T23:29:56.947000Z'), 'dateReturned': unicode('2015-03-11T23:29:56.947000Z'),} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) print("Expected Status Code %s" % (status.HTTP_201_CREATED)) print("Returned Status Code %s" % (response.status_code)) self.assertEqual(response.data, data) print(response.data)
def messages(self): if hasattr(self.err, 'messages'): msgs = self.err.messages if type(msgs) == dict: return msgs.values() else: return msgs else: return [unicode(self.err)]
def test_rentable_list(self): url = reverse('rentableList') print("***********************************") print("Test using a GET to get all objects") print("***********************************") for x in range(0, 5): data = {'store': None, 'type': unicode('WaveRunner'), 'isRented': True, 'dateRented': unicode('2015-03-11T23:29:56.947000Z'), 'dateDue': unicode('2015-03-11T23:29:56.947000Z'), 'dateReturned': unicode('2015-03-11T23:29:56.947000Z'),} self.client.post(url, data, format='json') getResponse = self.client.get(url, format='json') print("Number of returned objects") print("Expected 5") print("Returned %s" % (len(getResponse.data))) self.assertEqual(len(getResponse.data), 5)
def text(self, text): """ Print alpha-numeric text """ if text: if self._codepage: self._raw(unicode(text).encode(self._codepage, errors='replace')) else: self._raw(text) else: raise TextError()
def text(self, text): """ Print alpha-numeric text """ if text: if self._codepage: self._raw( unicode(text).encode(self._codepage, errors='replace')) else: self._raw(text) else: raise TextError()
def save_cell(self, cellix, instance_id, valuestr): try: value = self.validate_cell(cellix, valuestr) self.data.save(cellix, instance_id, value) except exceptions.ValidationError as err: self.cell_errors[cellix] = CellError(err, cellix, valuestr) except Exception as err: logger.error("Error: save_cell %s %s => %s " % (str(cellix), valuestr, unicode(err))) raise err
def export_as_csv(modeladmin, request, queryset): if not request.user.is_staff: raise PermissionDenied replace_dc = {'\n': '* ', '\r': '', ';': ',', '\"': '|', '\'': '|', 'True': 'Si', 'False': 'No'} opts = modeladmin.model._meta response = HttpResponse(content_type='text/csv; charset=utf-8') response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_') w = csv.writer(response, delimiter='|') # import pdb; pdb.set_trace() try: field_names = modeladmin.model.get_csv_fields() v_field_names = field_names except: field_names = [field.name for field in opts.fields] v_field_names = [getattr(field, 'verbose_name') or field.name for field in opts.fields] # print field_names v_field_names = map(lambda x: x.encode('utf-8') if x != 'ID' else 'Id', v_field_names) w.writerow(v_field_names) ax = [] for obj in queryset: acc = {} for field in field_names: try: uf = unicode(getattr(obj, field)()).encode('utf-8') except: try: uf = unicode(getattr(obj, field)).encode('utf-8') except: uf = '' for i, j in replace_dc.iteritems(): uf = uf.replace(i, j) if uf == 'None': uf = '' acc[field] = uf ax.append(acc) response.write(get_csv_from_dict_list(field_names, ax)) return response
def _generate_addons_files( self ): # addon list addons = os.listdir( repo_root ) # final addons text addons_xml = u"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<addons>\n" found_an_addon = False # loop thru and add each addons addon.xml file for addon in addons: try: # skip any file or .svn folder if is_addon_dir( addon ): # create path _path = os.path.join( addon, "addon.xml" ) if os.path.exists(_path): found_an_addon = True # split lines for stripping xml_lines = open( _path, "r" ).read().splitlines() # new addon addon_xml = "" # loop thru cleaning each line for line in xml_lines: # skip encoding format line if ( line.find( "<?xml" ) >= 0 ): continue # add line addon_xml += unicode( line.rstrip() + "\n", "UTF-8" ) # we succeeded so add to our final addons.xml text addons_xml += addon_xml.rstrip() + "\n\n" except Exception as e: # missing or poorly formatted addon.xml print ("Excluding %s for %s" % ( _path, e, )) # clean and add closing tag addons_xml = addons_xml.strip() + u"\n</addons>\n" # only generate files if we found an addon.xml if found_an_addon: # save files self._save_file( addons_xml.encode( "UTF-8" ), self.addons_xml ) self._generate_md5_file() # notify user print( "Updated addons xml and addons.xml.md5 files") else: print ("Could not find any addons, so script has done nothing.")
def get_hotel_policy(self): hotelpolicy_json = '' try: # 获取酒店政策信息 hotelpolicy_tbody_tag_list = self.soup.find_all('tbody') hotelpolicy_tbody_th_list = hotelpolicy_tbody_tag_list[0].find_all('th') hotelpolicy = {} for hotelpolicy_tbody_th in hotelpolicy_tbody_th_list: # value集合 temp_list = [] # key信息 th_name = unicode(hotelpolicy_tbody_th.string) ul = hotelpolicy_tbody_th.next_sibling.next_sibling.ul for string in ul.stripped_strings: temp_list.append(unicode(string)) hotelpolicy[th_name] = temp_list hotelpolicy_json = json.dumps(hotelpolicy, ensure_ascii=False) # print(hotelpolicy_json) except BaseException as e: print('酒店政策信息解析失败') finally: return hotelpolicy_json
def test_update_invalid_rental(self): print("*****************************************************") print("Test the creation of an invalid rental using a POST") print("*****************************************************") # Create Rental url = reverse('rentalList') data = {'cusName': unicode('Jack Daniels'), 'cusPhoneNum': unicode('850-342-9485'), 'cusEmail': unicode('*****@*****.**'), 'price': unicode('23.54'), 'rentable': None} self.client.post(url, data, format='json') # Edit rental url = reverse('rental', args="1") data = {'cusName': '', 'cusPhoneNum': '', 'cusEmail': '', 'price': '', 'rentable': ''} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) print("Expected Status Code %s" % (status.HTTP_400_BAD_REQUEST)) print("Returned Status Code %s" % (response.status_code))
def test_bad_url_double_scheme(self): """ A bad URL with a double scheme should raise a DistutilsError. """ index = setuptools.package_index.PackageIndex(hosts=("www.example.com",)) # issue 20 url = "http://http://svn.pythonpaste.org/Paste/wphp/trunk" try: index.open_url(url) except distutils.errors.DistutilsError as error: msg = unicode(error) assert "nonnumeric port" in msg or "getaddrinfo failed" in msg or "Name or service not known" in msg return raise RuntimeError("Did not raise")
def test_bad_url_double_scheme(self): """ A bad URL with a double scheme should raise a DistutilsError. """ index = setuptools.package_index.PackageIndex( hosts=('www.example.com', )) # issue 20 url = 'http://http://svn.pythonpaste.org/Paste/wphp/trunk' try: index.open_url(url) except distutils.errors.DistutilsError as error: msg = unicode(error) assert 'nonnumeric port' in msg or 'getaddrinfo failed' in msg or 'Name or service not known' in msg return raise RuntimeError("Did not raise")
def team_form(request): if request.user.is_authenticated(): if request.POST: form = TeamForm(request.POST or None) if form.is_valid(): form.save() team = Team.objects.get(name=form.cleaned_data['name']) for user in form.cleaned_data['users']: team.user.add(user) else: if request.is_ajax(): errors_dict = {} if form.errors: for error in form.errors: e = form.errors[error] errors_dict[error] = unicode(e) return HttpResponseBadRequest(json.dumps(errors_dict)) else: form = TeamForm() return render(request, "team_form.html", {'form': form}) raise PermissionDenied
def cell(self, cellix): cellid = "table_cell_" + str(self.indexer.cellindex_to_int(cellix)) celldata = unicode(self._data.get(cellix)) if self._data else u'n/a' return u''.join(['<td id="', cellid, '">', celldata, '</td>'])
def get_svn_version(): code, data = _run_command(['svn', '--version', '--quiet']) if code == 0 and data: return unicode(data).strip() else: return unicode('')
def get_db_prep_value(self, value, connection, prepared=False): if not value: return assert (isinstance(value, list) or isinstance(value, tuple)) return self.token.join([unicode(s) for s in value])
def get_id(self): return unicode(self.id)