def write_feed(file_obj): """Write feed contents info provided file object.""" writer = csv.DictWriter(file_obj, ATTRIBUTES, dialect=csv.excel_tab) writer.writeheader() categories = Category.objects.all() discounts = Sale.objects.active(date.today()).prefetch_related( "products", "categories" ) attributes_dict = {a.slug: a.pk for a in Attribute.objects.all()} attribute_values_dict = { smart_text(a.pk): smart_text(a) for a in AttributeValue.objects.all() } category_paths = {} current_site = Site.objects.get_current() for item in get_feed_items(): item_data = item_attributes( item, categories, category_paths, current_site, discounts, attributes_dict, attribute_values_dict, ) writer.writerow(item_data)
def test_smart_text(self): class Test: if six.PY3: def __str__(self): return 'ŠĐĆŽćžšđ' else: def __str__(self): return 'ŠĐĆŽćžšđ'.encode('utf-8') class TestU: if six.PY3: def __str__(self): return 'ŠĐĆŽćžšđ' def __bytes__(self): return b'Foo' else: def __str__(self): return b'Foo' def __unicode__(self): return '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' self.assertEqual(smart_text(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') self.assertEqual(smart_text(TestU()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') self.assertEqual(smart_text(1), '1') self.assertEqual(smart_text('foo'), 'foo')
def set_field_from_cell(import_log, new_object, header_row_field_name, cell): """ Set a field from a import cell. Use referenced fields the field is m2m or a foreign key. """ if (not header_row_field_name.startswith('simple_import_custom__') and not header_row_field_name.startswith('simple_import_method__')): field, model, direct, m2m = new_object._meta.get_field_by_name(header_row_field_name) if m2m: new_object.simple_import_m2ms[header_row_field_name] = cell elif isinstance(field, ForeignKey): related_field_name = RelationalMatch.objects.get(import_log=import_log, field_name=field.name).related_field_name related_model = field.related.parent_model related_object = related_model.objects.get(**{related_field_name:cell}) setattr(new_object, header_row_field_name, related_object) elif field.choices and getattr(settings, 'SIMPLE_IMPORT_LAZY_CHOICES', True): # Prefer database values over choices lookup database_values, verbose_values = zip(*field.choices) if cell in database_values: setattr(new_object, header_row_field_name, cell) elif cell in verbose_values: for choice in field.choices: if smart_text(cell) == smart_text(choice[1]): setattr(new_object, header_row_field_name, choice[0]) else: setattr(new_object, header_row_field_name, cell)
def authenticate(self, request): """ Returns a :obj:`User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns :const:`None`. """ try: from django.utils.encoding import smart_text, DjangoUnicodeDecodeError except ImportError: from django.utils.encoding import smart_unicode as smart_text, DjangoUnicodeDecodeError if 'HTTP_AUTHORIZATION' in request.META: auth = request.META['HTTP_AUTHORIZATION'].split() if len(auth) == 2 and auth[0].lower() == "basic": try: auth_parts = base64.b64decode(auth[1].encode(settings.DEFAULT_CHARSET)).partition(b':') except TypeError: return None try: uname, passwd = smart_text(auth_parts[0]), smart_text(auth_parts[2]) except DjangoUnicodeDecodeError: return None user = authenticate(username=uname, password=passwd) if user is not None and user.is_active: return user return None
def test_discussion_default_moderator(self): location, = Location.objects.filter(name='Mountain View') today = timezone.now() event = SuggestedEvent.objects.create( user=self.user, title='Cool Title', slug='cool-title', short_description='Short Description', description='Description', start_time=today, location=location ) discussion = SuggestedDiscussion.objects.create( event=event, enabled=True, notify_all=True, moderate_all=True, ) discussion.moderators.add(self.user) url = reverse('suggest:discussion', args=(event.pk,)) response = self.client.get(url) eq_(response.status_code, 200) ok_(self.user.email in smart_text(response.content)) # suppose you have previously saved that there should another # moderator that isn't you bob = User.objects.create(username='******', email='*****@*****.**') discussion.moderators.clear() discussion.moderators.add(bob) response = self.client.get(url) eq_(response.status_code, 200) content = smart_text(response.content) ok_(bob.email in content) ok_(self.user.email not in content)
def _add_tb_to_test(self, test, test_result, err): '''Add a traceback to the test result element''' exc_class, exc_value, tb = err tb_str = self._exc_info_to_string(err, test) test_result.set('type', '%s.%s' % (exc_class.__module__, exc_class.__name__)) test_result.set('message', smart_text(exc_value)) test_result.text = smart_text(tb_str)
def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None, error_messages=None, show_hidden_initial=False, validators=[], localize=False): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should # be used for this Field when displaying it. Each Field has a # default Widget that it'll use if you don't specify this. In # most cases, the default widget is TextInput. # label -- A verbose name for this field, for use in displaying this # field in a form. By default, Django will use a "pretty" # version of the form field name, if the Field is part of a # Form. # initial -- A value to use in this Field's initial display. This value # is *not* used as a fallback if data isn't given. # help_text -- An optional string to use as "help text" for this Field. # error_messages -- An optional dictionary to override the default # messages that the field will raise. # show_hidden_initial -- Boolean that specifies if it is needed to render a # hidden widget with initial value after widget. # validators -- List of addtional validators to use # localize -- Boolean that specifies if the field should be localized. if label is not None: label = smart_text(label) self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial if help_text is None: self.help_text = '' else: self.help_text = smart_text(help_text) widget = widget or self.widget if isinstance(widget, type): widget = widget() # Trigger the localization machinery if needed. self.localize = localize if self.localize: widget.is_localized = True # Let the widget know whether it should display as required. widget.is_required = self.required # Hook into self.widget_attrs() for any Field-specific HTML attributes. extra_attrs = self.widget_attrs(widget) if extra_attrs: widget.attrs.update(extra_attrs) self.widget = widget # Increase the creation counter, and save our local copy. self.creation_counter = Field.creation_counter Field.creation_counter += 1 messages = {} for c in reversed(self.__class__.__mro__): messages.update(getattr(c, 'default_error_messages', {})) messages.update(error_messages or {}) self.error_messages = messages self.validators = self.default_validators + validators
def get_headers(self): headers = ('Footprint Title,Footprint Date,Footprint Location,' 'Footprint Owners,Written Work Title,' 'Imprint Display Title,Imprint Printers,' 'Imprint Publication Date,Imprint Creation Date,' 'Footprint Percent Complete,Literary Work LOC,' 'Imprint Actor and Role,Imprint BHB Number,' 'Imprint OCLC Number,Evidence Type,Evidence Location,' 'Evidence Call Number,Evidence Details,') for r in Role.objects.for_footprint(): role = 'Footprint Role ' + smart_text(r.name).encode('utf-8')\ + ' Actor' headers += role + ',' headers += (role + ' VIAF Number,') for r in Role.objects.for_imprint(): role = 'Imprint Role: ' + smart_text(r.name).encode('utf-8')\ + ' Actor' headers += role + ',' headers += (role + ' VIAF Number,') headers = headers[:-1] headers += '\r\n' return headers
def details(self): m = None o = BankTransactionDetail() o.type = 'NONE' if self.type == BankTransaction.CREDIT: m = virement_du_compte.match(smart_text(self.raw_text)) if m is not None: res = m.groups() o.type = 'COMPTE' o.account = res[0] o.contact = res[1] o.sender = res[2] o.message = res[3] else: m = virement_de_sic.match(smart_text(self.raw_text)) if m is not None: res = m.groups() o.type = 'SIC' o.sic_iid = res[0] o.contact = res[1] o.iban = res[2] o.message = res[3] if self.type == BankTransaction.DEBIT: m = e_finance.match(smart_text(self.raw_text)) if m is not None: res = m.groups() o.type = 'E_FINANCE' o.account = res[0] o.contact = res[1] o.iban = res[2] o.message = res[3] return o
def display_for_field(value, field , **kwargs): from xadmin.views.list import EMPTY_CHANGELIST_VALUE from xadmin.fields import ColorField, ImageWithThumbField if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_text(obj) for obj in value.all()]) elif isinstance(field, ColorField): return collor_field(value) elif isinstance(field, ImageWithThumbField): return image_field(value, field, **kwargs) else: return smart_text(value)
def value_to_string(self, obj): value_string = super(ListCommaSeparatedIntegerField, self).value_to_string(obj) if value_string is None: return value_string if isinstance(value_string, (list, tuple)): return ','.join([smart_text(v) for v in value_string]) return smart_text(value_string)
def get_searched_queryset(self, qs): model = self.model term = self.GET["term"] try: term = model.autocomplete_term_adjust(term) except AttributeError: pass try: search_fields = model.autocomplete_search_fields() except AttributeError: try: search_fields = AUTOCOMPLETE_SEARCH_FIELDS[model._meta.app_label][model._meta.module_name] except KeyError: search_fields = () if search_fields: for word in term.split(): search = [models.Q(**{smart_text(item): smart_text(word)}) for item in search_fields] search_qs = QuerySet(model) search_qs.query.select_related = qs.query.select_related search_qs = search_qs.filter(reduce(operator.or_, search)) qs &= search_qs else: qs = model.objects.none() return qs
def words_export(request, language): try: lang = Language.objects.get(code=language) except Language.DoesNotExist: raise Http404 items = Word.get_known_with_context(language=lang, context_separator="<p>") rows = [] for base_form, known, notes, context in items: base_raw = re.sub(ur'\[.*\]', u'', smart_text(base_form)) notes = smart_text(notes) if notes: notes = notes.replace(u"\n", u" ").replace(u"\t", u" ").strip() else: notes = u"" context = context if context: context = smart_text(context).replace(u"\n", u" ").replace(u"\t", u" ").strip() else: context = u"" rows.append(u"\t".join( [smart_text(base_form), base_raw, unicode(known), notes, context])) response = HttpResponse(u"\n".join(rows), content_type="text/csv") response['Content-Disposition'] = "attachment; filename=\"words-%s.csv\"" % (lang.code) return response
def get_formats(): """ Returns all formats strings required for i18n to work """ FORMAT_SETTINGS = ( "DATE_FORMAT", "DATETIME_FORMAT", "TIME_FORMAT", "YEAR_MONTH_FORMAT", "MONTH_DAY_FORMAT", "SHORT_DATE_FORMAT", "SHORT_DATETIME_FORMAT", "FIRST_DAY_OF_WEEK", "DECIMAL_SEPARATOR", "THOUSAND_SEPARATOR", "NUMBER_GROUPING", "DATE_INPUT_FORMATS", "TIME_INPUT_FORMATS", "DATETIME_INPUT_FORMATS", ) result = {} for module in [settings] + get_format_modules(reverse=True): for attr in FORMAT_SETTINGS: result[attr] = get_format(attr) formats = {} for k, v in result.items(): if isinstance(v, (six.string_types, int)): formats[k] = smart_text(v) elif isinstance(v, (tuple, list)): formats[k] = [smart_text(value) for value in v] return formats
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if request.FILES: filedata = request.FILES['Filedata'] # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) filedata.name = convert_filename(filedata.name) # HANDLE UPLOAD exists = default_storage.exists(os.path.join(get_directory(), folder, filedata.name)) abs_path = os.path.join(get_directory(), folder, filedata.name) uploadedfile = default_storage.save(abs_path, filedata) path = os.path.join(get_directory(), folder) file_name = os.path.join(path, filedata.name) if exists: default_storage.move(smart_text(uploadedfile), smart_text(file_name), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_text(file_name))) return HttpResponse('True')
def test_smart_text(self): class Test: if six.PY3: def __str__(self): return "ŠĐĆŽćžšđ" else: def __str__(self): return "ŠĐĆŽćžšđ".encode("utf-8") class TestU: if six.PY3: def __str__(self): return "ŠĐĆŽćžšđ" def __bytes__(self): return b"Foo" else: def __str__(self): return b"Foo" def __unicode__(self): return "\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" self.assertEqual(smart_text(Test()), "\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111") self.assertEqual(smart_text(TestU()), "\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111") self.assertEqual(smart_text(1), "1") self.assertEqual(smart_text("foo"), "foo")
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): decimal_places = field.decimal_places if decimal_places: point_pos = str(value).rfind('.') if point_pos != -1: # not integer removing_trailing_zeros = str(value).rstrip('0') decimal_places = len(removing_trailing_zeros) - point_pos - 1 else: # integer decimal_places = 0 return formats.number_format(value, decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_text(obj) for obj in value.all()]) else: return smart_text(value)
def update(self, request, initial): try: # public filter removed images, _more = glance.image_list_detailed(request) except: images = [] exceptions.handle(request, _("Unable to retrieve public images.")) image_mapping, image_choices = {}, [] for image in images: murano_property = image.properties.get('murano_image_info') if murano_property: # convert to dict because # only string can be stored in image metadata property try: murano_json = ast.literal_eval(murano_property) except ValueError: messages.error(request, _("Invalid value in image metadata")) else: title = murano_json.get('title') image_id = murano_json.get('id') if title and image_id: image_mapping[smart_text(title)] = smart_text(image_id) for name in sorted(image_mapping.keys()): image_choices.append((image_mapping[name], name)) if image_choices: image_choices.insert(0, ("", _("Select Image"))) else: image_choices.insert(0, ("", _("No images available"))) self.choices = image_choices
def clear_dir(self, path): """ Deletes the given relative path using the destination storage backend. """ if not self.storage.exists(path): return dirs, files = self.storage.listdir(path) for f in files: fpath = os.path.join(path, f) if self.dry_run: self.log("Pretending to delete '%s'" % smart_text(fpath), level=1) else: self.log("Deleting '%s'" % smart_text(fpath), level=1) try: full_path = self.storage.path(fpath) except NotImplementedError: self.storage.delete(fpath) else: if not os.path.exists(full_path) and os.path.lexists(full_path): # Delete broken symlinks os.unlink(full_path) else: self.storage.delete(fpath) for d in dirs: self.clear_dir(os.path.join(path, d))
def start_object(self, obj): """ Called as each object is handled. """ if not hasattr(obj, "_meta"): raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj)) self.start_fileblock(obj) self.indent(2) obj_pk, keytype = self._get_obj_pk(obj) attrs = { "restype": "row", "d:keytype": keytype } if obj_pk is not None: attrs["resname"] = "%s.%s" % (smart_text(obj._meta), obj_pk) else: attrs["resname"] = smart_text(obj._meta) self.xml.startElement("group", attrs) if obj._meta.pk.__class__.__name__ != "AutoField": self.handle_field(obj, obj._meta.pk)
def buildProduct(self,gprod): product = gprod.product offer_id = get_unique_id(gprod) product_data = { 'offerId': offer_id, 'title': smart_text(product.title), 'description': len(gprod.google_shopping_description) > 0 and bleach.clean(smart_text(gprod.google_shopping_description),strip=True) or bleach.clean(smart_text(gprod.parent.google_shopping_description),strip=True), 'link': SITE_ROOT + product.get_absolute_url(), 'imageLink': product.get_first_image_url(), 'brand': BRAND, 'contentLanguage': 'en', 'targetCountry': 'UK', 'channel': 'online', 'availability': resolve_google_availability(product), 'condition': 'new', 'googleProductCategory': smart_text(gprod.google_taxonomy.name), 'mpn': product.upc, 'price': {'value': str(product.stockrecords.first().price_incl_tax), 'currency': 'GBP'}, 'shipping': [{ 'country': 'UK', 'service': 'Standard shipping', 'price': {'value': '3.95', 'currency': 'GBP'} }], #'shippingWeight': {'value': '200', 'unit': 'grams'} } gprod.google_shopping_id = offer_id gprod.save() return product_data
def test_view_exception(self): """""" request = self.request_factory.request() request.site = get_current_site(request) request.user = get_user_object(TEST_USER_NAME) request.META['QUERY_STRING'] = urlencode({ 'pk': '999' }, doseq=True) response = select2_contenttypes_view(request, 'user') # Should be http code server error as user is not exists self.assertEqual(500, response.status_code) self.assertEqual(500, json.loads(smart_text(response.content))['meta']['status']) request.method = 'POST' response = select2_contenttypes_view(request, 'user') # Should be http code not permitted self.assertEqual(405, response.status_code) self.assertEqual(405, json.loads(smart_text(response.content))['meta']['status']) # Test: authorization request.user.is_superuser = False request.user.is_staff = False request.user.save() request.method = 'GET' # Manually Trigger Default Router Generation other than exception should be happen DynamicRouteMiddleware().process_request(request) # We are pushing newly generated url conf into current thread set_urlconf(request.urlconf) # Patch Done! response = select2_contenttypes_view(request, 'user') # Should Be Redirect to login page self.assertEqual(302, response.status_code) self.assertIn('admin/login', response.url)
def test_videos_by_order(self): factories.VideoFactory(state=Video.STATE_LIVE, title=u'FooC', recorded=datetime.datetime(2014, 1, 1, 10, 0)) factories.VideoFactory(state=Video.STATE_LIVE, title=u'FooA', recorded=datetime.datetime(2013, 1, 1, 10, 0)) factories.VideoFactory(state=Video.STATE_LIVE, title=u'FooB', recorded=datetime.datetime(2014, 2, 1, 10, 0)) # Filter by title. resp = self.auth_get('/api/v2/video/?ordering=title', content_type='application/json') data = json.loads(smart_text(resp.content)) assert [v['title'] for v in data['results']] == [u'FooA', u'FooB', u'FooC'] # Filter by recorded. resp = self.auth_get('/api/v2/video/?ordering=recorded', content_type='application/json') data = json.loads(smart_text(resp.content)) assert [v['title'] for v in data['results']] == [u'FooA', u'FooC', u'FooB'] # Filter by added (reverse order). resp = self.auth_get('/api/v2/video/?ordering=-added', content_type='application/json') data = json.loads(smart_text(resp.content)) assert [v['title'] for v in data['results']] == [u'FooB', u'FooA', u'FooC']
def basic_auth(http_request): if 'HTTP_AUTHORIZATION' in http_request.META: authdata = http_request.META['HTTP_AUTHORIZATION'].split() if len(authdata) == 2 and authdata[0].lower() == "basic": try: raw = authdata[1].encode('ascii') auth_parts = base64.b64decode(raw).split(b':') except: response = HttpResponse() response.status_code = 555 return response try: uname, passwd = (smart_text(auth_parts[0]), smart_text(auth_parts[1])) except DjangoUnicodeDecodeError: response = HttpResponse() response.status_code = 555 return response user = authenticate(username=uname, password=passwd) if user is not None and user.is_active: login(http_request, user) http_request.user = user response = HttpResponse() response.status_code = 222 return response response = HttpResponse() response.status_code = 555 return response
def _add_tb_to_test(self, test, test_result, err): """Add a traceback to the test result element""" exc_class, exc_value, tb = err tb_str = self._exc_info_to_string(err, test) test_result.set("type", "%s.%s" % (exc_class.__module__, exc_class.__name__)) test_result.set("message", smart_text(exc_value)) test_result.text = smart_text(tb_str)
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if request.FILES: filedata = request.FILES['Filedata'] directory = get_directory() # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path): default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_text(file_path))) return HttpResponse('True')
def handle_gm2m(value): try: natural = value.natural_key() use_natural_key = True except AttributeError: natural = smart_text(value._get_pk_val()) use_natural_key = False # Iterable natural keys are rolled out as subelements if use_natural_key: attrs = {'pk': smart_text(value._get_pk_val())} else: attrs = {} self.xml.startElement("object", attrs) # add content type information app, model = get_content_type(value).natural_key() self.xml.addQuickElement('contenttype', attrs={ 'app': app, 'model': model }) if use_natural_key: for key_value in natural: self.xml.startElement("natural", {}) self.xml.characters(smart_text(key_value)) self.xml.endElement("natural") self.xml.endElement("object")
def test_modelform_multiple(self): ''' Test, that ``Meta.objects`` transforms to ``CachedModelMultipleChoiceField.objects``. ''' form = self.ModelFormMultiple({ 'm2m_field': [smart_text(self.obj1.pk), smart_text(self.obj2.pk)], 'name': 'Name1' }) self.assertTrue( isinstance( form.fields['m2m_field'], CachedModelMultipleChoiceField ) ) # saving new_obj = form.save() self.assertEqual( set(new_obj.m2m_field.all()), set([self.obj1, self.obj2]) ) # loading back form = self.ModelFormSingle(instance=new_obj) self.assertEqual( set(form.initial['m2m_field']), set([self.obj1.pk, self.obj2.pk]) )
def get_attributes_dict_from_list(attributes, slug_to_id_map): """ :param attributes: list :return: dict Takes list on form [{"slug": "attr_slug", "value": "attr_value"}, {...}] and converts into dictionary {attr_pk: value_pk} """ attr_ids = {} value_slug_id = dict( AttributeChoiceValue.objects.values_list('name', 'id')) for attribute in attributes: attr_slug = attribute.get('slug') if attr_slug not in slug_to_id_map: raise ValueError( 'Attribute %r doesn\'t belong to given product type.' % ( attr_slug,)) value = attribute.get('value') if not value: continue if not value_slug_id.get(value): attr = ProductAttribute.objects.get(slug=attr_slug) value = AttributeChoiceValue( attribute_id=attr.pk, name=value, slug=slugify(value)) value.save() attr_ids[smart_text( slug_to_id_map.get(attr_slug))] = smart_text(value.pk) else: attr_ids[smart_text(slug_to_id_map.get(attr_slug))] = smart_text( value_slug_id.get(value)) return attr_ids
def handle_fk_field(self, obj, field): """ Called to handle a ForeignKey (we need to treat them slightly differently from regular fields). """ related_att = getattr(obj, field.get_attname()) if related_att is not None: if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'): self._start_relational_field(field, keytype="natural") related = getattr(obj, field.name) # If related object has a natural key, use it related = related.natural_key() nat_key = NATURAL_KEY_JOINER.join(related) self.xml.characters(smart_text(nat_key)) # Iterable natural keys are rolled out as subelements # for key_value in related: # self.xml.startElement("natural", {}) # self.xml.characters(smart_text(key_value)) # self.xml.endElement("natural") else: self._start_relational_field(field) self.xml.characters(smart_text(related_att)) else: self._start_relational_field(field) self.xml.addQuickElement("None") self.xml.endElement("source") self.indent(3) self.xml.endElement("trans-unit")
def model_instance_diff(old, new): """ Calculates the differences between two model instances. One of the instances may be ``None`` (i.e., a newly created model or deleted model). This will cause all fields with a value to have changed (from ``None``). :param old: The old state of the model instance. :type old: Model :param new: The new state of the model instance. :type new: Model :return: A dictionary with the names of the changed fields as keys and a two tuple of the old and new field values as value. :rtype: dict """ from auditlog.registry import auditlog if not (old is None or isinstance(old, Model)): raise TypeError( "The supplied old instance is not a valid model instance.") if not (new is None or isinstance(new, Model)): raise TypeError( "The supplied new instance is not a valid model instance.") diff = {} if old is not None and new is not None: fields = set(old._meta.fields + new._meta.fields) model_fields = auditlog.get_model_fields(new._meta.model) elif old is not None: fields = set(get_fields_in_model(old)) model_fields = auditlog.get_model_fields(old._meta.model) elif new is not None: fields = set(get_fields_in_model(new)) model_fields = auditlog.get_model_fields(new._meta.model) else: fields = set() model_fields = None # Check if fields must be filtered if model_fields and (model_fields["include_fields"] or model_fields["exclude_fields"]) and fields: filtered_fields = [] if model_fields["include_fields"]: filtered_fields = [ field for field in fields if field.name in model_fields["include_fields"] ] else: filtered_fields = fields if model_fields["exclude_fields"]: filtered_fields = [ field for field in filtered_fields if field.name not in model_fields["exclude_fields"] ] fields = filtered_fields for field in fields: old_value = get_field_value(old, field) new_value = get_field_value(new, field) if old_value != new_value: diff[field.name] = (smart_text(old_value), smart_text(new_value)) if len(diff) == 0: diff = None return diff
def get_attribute(self, pk): return self.attributes.get(smart_text(pk))
def set_attribute(self, pk, value_pk): self.attributes[smart_text(pk)] = smart_text(value_pk)
def get_slug(self): return slugify(smart_text(unidecode(self.name)))
def bytes_safe_decode(value, encoding='utf-8'): if isinstance(value, binary_type): spec = chardet.detect(value) encoding = spec.get('encoding', encoding) or encoding value = smart_text(value, encoding=encoding) return value
def __unicode__(self): val = smart_text(self.instance) if len(val) > DISPLAY_SIZE: return val[:DISPLAY_SIZE] + u'...' return val
def render(self, value): ids = [smart_text(getattr(obj, self.field)) for obj in value.all()] return self.separator.join(ids)
def _assertCount(self, response, found, count): self.assertEqual(count, smart_text(response.content).count(found))
def __unicode__(self): return smart_text(self.tag)
def valid_value(self, value): value = self.prepare_value(value) for k, v in self.choices: if value == smart_text(k): return True return False
def model_choices(using=DEFAULT_ALIAS): choices = [ (get_model_ct(m), capfirst(smart_text(m._meta.verbose_name_plural))) for m in connections[using].get_unified_index().get_indexed_models() ] return sorted(choices, key=lambda x: x[1])
def __str__(self): return "%s.%s" % (smart_text( self.app_label), smart_text(self.module_name))
def _encode_name(self, name): return smart_text(name, encoding=self.file_name_charset)
def __unicode__(self): return smart_text(self.first_name)
def result_item(self, obj, field_name, row): """ Generates the actual list of data. """ item = ResultItem(field_name, row) try: f, attr, value = lookup_field(field_name, obj, self) except (AttributeError, ObjectDoesNotExist, NoReverseMatch): item.text = mark_safe("<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE) else: if f is None: item.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: item.allow_tags = True item.text = boolean_icon(value) else: item.text = smart_text(value) else: if isinstance(f.remote_field, models.ManyToOneRel): field_val = getattr(obj, f.name) if field_val is None: item.text = mark_safe( "<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE) else: item.text = field_val else: item.text = display_for_field(value, f) if isinstance(f, models.DateField)\ or isinstance(f, models.TimeField)\ or isinstance(f, models.ForeignKey): item.classes.append('nowrap') item.field = f item.attr = attr item.value = value # If list_display_links not defined, add the link tag to the first field if (item.row['is_display_first'] and not self.list_display_links) \ or field_name in self.list_display_links: item.row['is_display_first'] = False item.is_display_link = True if self.list_display_links_details: item_res_uri = self.model_admin_url( "detail", getattr(obj, self.pk_attname)) if item_res_uri: if self.has_change_permission(obj): edit_url = self.model_admin_url( "change", getattr(obj, self.pk_attname)) else: edit_url = "" item.wraps.append( '<a data-res-uri="%s" data-edit-uri="%s" class="details-handler" rel="tooltip" title="%s">%%s</a>' % (item_res_uri, edit_url, _(u'Details of %s') % str(obj))) else: url = self.url_for_result(obj) item.wraps.append(u'<a href="%s">%%s</a>' % url) return item
def __str__(self): return '%s [%s]' % (smart_text(self.object), smart_text(self.tag))
def __str__(self): return smart_text(self.name)
def handle(self, *args, **options): exts_pool.load_all() self.csvwriter = csv.writer(self.stdout, delimiter=smart_text(options["sepchar"])) getattr(self, "export_{}".format(options["objtype"]))()
def __str__(self): return smart_text(self.title)
def byrows_update(modeladmin, request, queryset): # noqa """ by rows update queryset :type modeladmin: ModelAdmin :type request: HttpRequest :type queryset: QuerySet """ opts = modeladmin.model._meta perm = "{0}.{1}".format( opts.app_label.lower(), get_permission_codename('adminactions_byrowsupdate', opts)) if not request.user.has_perm(perm): messages.error( request, _('Sorry you do not have rights to execute this action')) return class modelform(modeladmin.form): def __init__(self, *args, **kwargs): super(modeladmin.form, self).__init__(*args, **kwargs) if self.instance: readonly_fields = (modeladmin.model._meta.pk.name, ) + modeladmin.get_readonly_fields(request) for fname in readonly_fields: if fname in self.fields: self.fields[fname].widget.attrs[ 'readonly'] = 'readonly' self.fields[fname].widget.attrs['class'] = 'readonly' fields = byrows_update_get_fields(modeladmin) ActionForm = modelform_factory(modeladmin.model, form=GenericActionForm, fields=fields) MFormSet = modelformset_factory(modeladmin.model, form=modelform, fields=fields, extra=0) if 'apply' in request.POST: actionform = ActionForm(request.POST) formset = MFormSet(request.POST) if formset.is_valid(): formset.save() messages.info(request, _("Updated record(s)")) return HttpResponseRedirect(request.get_full_path()) else: action_form_initial = { '_selected_action': request.POST.getlist(helpers.ACTION_CHECKBOX_NAME), 'select_across': request.POST.get('select_across') == '1', 'action': 'byrows_update' } actionform = ActionForm(initial=action_form_initial, instance=None) formset = MFormSet(queryset=queryset) adminform = helpers.AdminForm(actionform, modeladmin.get_fieldsets(request), {}, [], model_admin=modeladmin) tpl = 'adminactions/byrows_update.html' ctx = { 'adminform': adminform, 'actionform': actionform, 'action_short_description': byrows_update.short_description, 'title': u"%s (%s)" % ( byrows_update.short_description.capitalize(), smart_text(modeladmin.opts.verbose_name_plural), ), 'formset': formset, 'opts': modeladmin.model._meta, 'app_label': modeladmin.model._meta.app_label, } if django.VERSION[:2] > (1, 7): ctx.update(modeladmin.admin_site.each_context(request)) else: ctx.update(modeladmin.admin_site.each_context()) return render_to_response(tpl, RequestContext(request, ctx))
def _get_editable_context(self, context, instance, language, edit_fields, view_method, view_url, querystring, editmode=True): """ Populate the contex with the requested attributes to trigger the changeform """ request = context['request'] if hasattr(request, 'toolbar'): lang = request.toolbar.toolbar_language else: lang = get_language() with force_language(lang): extra_context = {} if edit_fields == 'changelist': instance.get_plugin_name = u"%s %s list" % (smart_text( _('Edit')), smart_text(instance._meta.verbose_name)) extra_context['attribute_name'] = 'changelist' elif editmode: instance.get_plugin_name = u"%s %s" % (smart_text( _('Edit')), smart_text(instance._meta.verbose_name)) if not context.get('attribute_name', None): # Make sure CMS.Plugin object will not clash in the frontend. extra_context['attribute_name'] = '-'.join(edit_fields) \ if not isinstance('edit_fields', six.string_types) else edit_fields else: instance.get_plugin_name = u"%s %s" % (smart_text( _('Add')), smart_text(instance._meta.verbose_name)) extra_context['attribute_name'] = 'add' extra_context['instance'] = instance extra_context['generic'] = instance._meta # view_method has the precedence and we retrieve the corresponding # attribute in the instance class. # If view_method refers to a method it will be called passing the # request; if it's an attribute, it's stored for later use if view_method: method = getattr(instance, view_method) if callable(method): url_base = method(context['request']) else: url_base = method else: # The default view_url is the default admin changeform for the # current instance if not editmode: view_url = 'admin:%s_%s_add' % (instance._meta.app_label, instance._meta.module_name) url_base = reverse(view_url) elif not edit_fields: if not view_url: view_url = 'admin:%s_%s_change' % ( instance._meta.app_label, instance._meta.module_name) if isinstance(instance, Page): url_base = reverse(view_url, args=(instance.pk, language)) else: url_base = reverse(view_url, args=(instance.pk, )) else: if not view_url: view_url = 'admin:%s_%s_edit_field' % ( instance._meta.app_label, instance._meta.module_name) if view_url.endswith('_changelist'): url_base = reverse(view_url) else: url_base = reverse(view_url, args=(instance.pk, language)) querystring['edit_fields'] = ",".join( context['edit_fields']) if editmode: extra_context['edit_url'] = "%s?%s" % (url_base, urlencode(querystring)) else: extra_context['edit_url'] = "%s" % url_base extra_context['refresh_page'] = True # We may be outside the CMS (e.g.: an application which is not attached via Apphook) # in this case we may only go back to the home page if getattr(context['request'], 'current_page', None): extra_context['redirect_on_close'] = context[ 'request'].current_page.get_absolute_url(language) else: extra_context['redirect_on_close'] = '' return extra_context
def __str__(self): return smart_text(self.get_username())
def search(self, query, rank_field=None, rank_function='ts_rank', config=None, rank_normalization=32, raw=False, using=None, fields=None, headline_field=None, headline_document=None): ''' Convert query with to_tsquery or plainto_tsquery, depending on raw is `True` or `False`, and return a QuerySet with the filter. If `rank_field` is not `None`, a field with this name will be added containing the search rank of the instances, and the queryset will be ordered by it. The rank_function and normalization are explained here: http://www.postgresql.org/docs/9.1/interactive/textsearch-controls.html#TEXTSEARCH-RANKING If an empty query is given, no filter is made so the QuerySet will return all model instances. If `fields` is not `None`, the filter is made with this fields instead of defined on a constructor of manager. If `headline_field` and `headline_document` is not `None`, a field with this `headline_field` name will be added containing the headline of the instances, which will be searched inside `headline_document`. Search headlines are explained here: http://www.postgresql.org/docs/9.1/static/textsearch-controls.html#TEXTSEARCH-HEADLINE ''' if not config: config = self.manager.config db_alias = using if using is not None else self.db connection = connections[db_alias] qn = connection.ops.quote_name qs = self if using is not None: qs = qs.using(using) if query: function = "to_tsquery" if raw else "plainto_tsquery" ts_query = smart_text( "%s('%s', %s)" % (function, config, adapt(query)) ) full_search_field = "%s.%s" % ( qn(self.model._meta.db_table), qn(self.manager.search_field) ) # if fields is passed, obtain a vector expression with # these fields. In other case, intent use of search_field if # exists. if fields: search_vector = self.manager._get_search_vector(config, using, fields=fields) else: if not self.manager.search_field: raise ValueError("search_field is not specified") search_vector = full_search_field where = " (%s) @@ (%s)" % (search_vector, ts_query) select_dict, order = {}, [] if rank_field: select_dict[rank_field] = '%s(%s, %s, %d)' % ( rank_function, search_vector, ts_query, rank_normalization ) order = ['-%s' % (rank_field,)] if headline_field is not None and headline_document is not None: select_dict[headline_field] = "ts_headline('%s', %s, %s)" % ( config, headline_document, ts_query ) qs = qs.extra(select=select_dict, where=[where], order_by=order) return qs
def instance_form(self, **kwargs): formset = self.get_formset(**kwargs) attrs = {'instance': self.model_instance, 'queryset': self.queryset()} if self.request_method == 'post': attrs.update({ 'data': self.request.POST, 'files': self.request.FILES, 'save_as_new': "_saveasnew" in self.request.POST }) instance = formset(**attrs) instance.view = self helper = FormHelper() helper.form_tag = False helper.include_media = False # override form method to prevent render csrf_token in inline forms, see template 'bootstrap/whole_uni_form.html' helper.form_method = 'get' style = style_manager.get_style('one' if self.max_num == 1 else self.style)(self, instance) style.name = self.style if len(instance): layout = copy.deepcopy(self.form_layout) if layout is None: layout = Layout(*instance[0].fields.keys()) elif type(layout) in (list, tuple) and len(layout) > 0: layout = Layout(*layout) rendered_fields = [i[1] for i in layout.get_field_names()] layout.extend([ f for f in instance[0].fields.keys() if f not in rendered_fields ]) helper.add_layout(layout) style.update_layout(helper) # replace delete field with Dynamic field, for hidden delete field when instance is NEW. helper[DELETION_FIELD_NAME].wrap(DeleteField) instance.helper = helper instance.style = style readonly_fields = self.get_readonly_fields() if readonly_fields: for form in instance: form.readonly_fields = [] inst = form.save(commit=False) if inst: meta_field_names = [ field.name for field in inst._meta.get_fields() ] for readonly_field in readonly_fields: value = None label = None if readonly_field in meta_field_names: label = inst._meta.get_field( readonly_field).verbose_name value = smart_text(getattr(inst, readonly_field)) elif inspect.ismethod( getattr(inst, readonly_field, None)): value = getattr(inst, readonly_field)() label = getattr(getattr(inst, readonly_field), 'short_description', readonly_field) elif inspect.ismethod( getattr(self, readonly_field, None)): value = getattr(self, readonly_field)(inst) label = getattr(getattr(self, readonly_field), 'short_description', readonly_field) if value: form.readonly_fields.append({ 'label': label, 'contents': value }) return instance
def get_nav_menu(self): site_menu = list(self.get_site_menu() or []) had_urls = [] def get_url(menu, had_urls): if 'url' in menu: had_urls.append(menu['url']) if 'menus' in menu: for m in menu['menus']: get_url(m, had_urls) get_url({'menus': site_menu}, had_urls) nav_menu = OrderedDict() for model, model_admin in self.admin_site._registry.items(): if getattr(model_admin, 'hidden_menu', False): continue app_label = model._meta.app_label app_icon = None model_dict = { 'title': smart_text(capfirst(model._meta.verbose_name_plural)), 'url': self.get_model_url(model, "changelist"), 'icon': self.get_model_icon(model), 'perm': self.get_model_perm(model, 'view'), 'order': model_admin.order, } if model_dict['url'] in had_urls: continue app_key = "app:%s" % app_label if app_key in nav_menu: nav_menu[app_key]['menus'].append(model_dict) else: # Find app title app_title = smart_text(app_label.title()) if app_label.lower() in self.apps_label_title: app_title = self.apps_label_title[app_label.lower()] else: app_title = smart_text(apps.get_app_config(app_label).verbose_name) # find app icon if app_label.lower() in self.apps_icons: app_icon = self.apps_icons[app_label.lower()] nav_menu[app_key] = { 'title': app_title, 'menus': [model_dict], } app_menu = nav_menu[app_key] if app_icon: app_menu['first_icon'] = app_icon elif ('first_icon' not in app_menu or app_menu['first_icon'] == self.default_model_icon) and model_dict.get('icon'): app_menu['first_icon'] = model_dict['icon'] if 'first_url' not in app_menu and model_dict.get('url'): app_menu['first_url'] = model_dict['url'] for menu in nav_menu.values(): menu['menus'].sort(key=sortkeypicker(['order', 'title'])) nav_menu = list(nav_menu.values()) nav_menu.sort(key=lambda x: x['title']) site_menu.extend(nav_menu) return site_menu
def __repr__(self): return smart_text(self.action_time)
def elem_content(self, elem): return smart_text(elem['text'])
def post_response(self): self.message_user(_('The %(model)s "%(name)s" was recovered successfully. You may edit it again below.') % {"model": force_text(self.opts.verbose_name), "name": smart_text(self.new_obj)}, 'success') return HttpResponseRedirect(self.model_admin_url('change', self.new_obj.pk))
def test_search_results(self): BugAssociation.objects.create( bug_id=123456, signature="nsASDOMWindowEnumerator::GetNext()" ) def mocked_supersearch_get(**params): assert "_columns" in params if "product" in params and "WaterWolf" in params["product"]: results = { "hits": [ { "signature": "nsASDOMWindowEnumerator::GetNext()", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa1", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": 888981, }, { "signature": "mySignatureIsCool", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa2", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": 888981, }, { "signature": "mineIsCoolerThanYours", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa3", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": None, }, { "signature": "EMPTY", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa4", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": None, }, ], "facets": { "signature": [ {"term": "nsASDOMWindowEnumerator::GetNext()", "count": 1}, {"term": "mySignatureIsCool", "count": 1}, {"term": "mineIsCoolerThanYours", "count": 1}, {"term": "EMPTY", "count": 1}, ] }, "total": 4, } results["hits"] = self.only_certain_columns( results["hits"], params["_columns"] ) return results elif "product" in params and "SeaMonkey" in params["product"]: results = { "hits": [ { "signature": "nsASDOMWindowEnumerator::GetNext()", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": 888981, }, { "signature": "mySignatureIsCool", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": 888981, }, ], "facets": {"build_id": [{"term": "888981", "count": 2}]}, "total": 2, } results["hits"] = self.only_certain_columns( results["hits"], params["_columns"] ) return results elif ( "signature" in params and "~nsASDOMWindowEnumerator" in params["signature"] ): results = { "hits": [ { "signature": "nsASDOMWindowEnumerator::GetNext()", "date": "2017-01-31T23:12:57", "uuid": "aaaaaaaaaaaaa", "product": "WaterWolf", "version": "1.0", "platform": "Linux", "build_id": 12345678, } ], "facets": { "signature": [ {"term": "nsASDOMWindowEnumerator::GetNext()", "count": 1} ] }, "total": 1, } results["hits"] = self.only_certain_columns( results["hits"], params["_columns"] ) return results else: return {"hits": [], "facets": [], "total": 0} SuperSearchUnredacted.implementation().get.side_effect = mocked_supersearch_get url = reverse("supersearch:search_results") response = self.client.get(url, {"product": "WaterWolf"}) assert response.status_code == 200 # Test results are existing assert 'table id="reports-list"' in smart_text(response.content) assert "nsASDOMWindowEnumerator::GetNext()" in smart_text(response.content) assert "mySignatureIsCool" in smart_text(response.content) assert "mineIsCoolerThanYours" in smart_text(response.content) assert "EMPTY" in smart_text(response.content) assert "aaaaaaaaaaaaa1" in smart_text(response.content) assert "888981" in smart_text(response.content) assert "Linux" in smart_text(response.content) assert "2017-01-31 23:12:57" in smart_text(response.content) # Test facets are existing assert 'table id="facets-list-' in smart_text(response.content) # Test bugs are existing assert '<th scope="col">Bugs</th>' in smart_text(response.content) assert "123456" in smart_text(response.content) # Test links on terms are existing assert "product=%3DWaterWolf" in smart_text(response.content) # Test with empty results response = self.client.get(url, {"product": "NightTrain", "date": "2012-01-01"}) assert response.status_code == 200 assert 'table id="reports-list"' not in smart_text(response.content) assert "No results were found" in smart_text(response.content) # Test with a signature param response = self.client.get(url, {"signature": "~nsASDOMWindowEnumerator"}) assert response.status_code == 200 assert 'table id="reports-list"' in smart_text(response.content) assert "nsASDOMWindowEnumerator::GetNext()" in smart_text(response.content) assert "123456" in smart_text(response.content) # Test with a different facet response = self.client.get(url, {"_facets": "build_id", "product": "SeaMonkey"}) assert response.status_code == 200 assert 'table id="reports-list"' in smart_text(response.content) assert 'table id="facets-list-' in smart_text(response.content) assert "888981" in smart_text(response.content) # Bugs should not be there, they appear only in the signature facet assert "<th>Bugs</th>" not in smart_text(response.content) assert "123456" not in smart_text(response.content) # Test with a different columns list response = self.client.get( url, {"_columns": ["build_id", "platform"], "product": "WaterWolf"} ) assert response.status_code == 200 assert 'table id="reports-list"' in smart_text(response.content) assert 'table id="facets-list-' in smart_text(response.content) # The build and platform appear assert "888981" in smart_text(response.content) assert "Linux" in smart_text(response.content) # The crash id is always shown assert "aaaaaaaaaaaaa1" in smart_text(response.content) # The version and date do not appear assert "1.0" not in smart_text(response.content) assert "2017" not in smart_text(response.content) # Test missing parameters don't raise an exception. response = self.client.get( url, {"product": "WaterWolf", "date": "", "build_id": ""} ) assert response.status_code == 200
def get_model_instance_label(instance): if getattr(instance, "related_label", None): return instance.related_label() return smart_text(instance)