def _parse_file(self, f): """Build nested dicts to mirror the XML format of the settings file.""" tree = etree.iterparse(f, events=('start', 'end')) settings = {} current_level = settings for event, element in tree: # print event, element.tag, element.text text = SafeUnicode(element.text).strip() if not text: if event == 'start': # Add a nested dict, saving the parent. current_level[element.tag] = {'_parent': current_level} current_level = current_level[element.tag] elif event == 'end': # Un-nest back to the parent level. parent = current_level['_parent'] del current_level['_parent'] current_level = parent elif event == 'start': if element.tag in current_level: if not isinstance(current_level[element.tag], list): current_level[element.tag] = [ current_level[element.tag] ] current_level[element.tag].append(text) else: current_level[element.tag] = text # Fix for email-ends-with s = settings['site-settings'] if not isinstance(s['email-endswith'], list): s['email-endswith'] = [s['email-endswith']] return s
def __new__(cls, *items): output = [] output.append(u'<ul class="helpList">') for item in items: output.extend([u'<li>', unicode(item), u'</li>']) output.append(u'</ul>') return SafeUnicode.__new__(cls, u''.join(output))
def _format_sentences(sentences, fun): sentence_groups = _group_sentences(sentences) formatted = '' for s_list in sentence_groups: raw_text = [(s.text) for s in s_list] formatted = formatted + fun(' '.join(raw_text)) formatted = SafeUnicode(formatted) return formatted
def custom_force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ This is a wrapper of django.utils.encoding.force_unicode to return SafeUnicode objects instead of unicode, respecting protected_types and cases order to keep performance. """ # Handle the common case first, saves 30-40% in performance when s # is an instance of unicode. This function gets called often in that # setting. if isinstance(s, unicode): return SafeUnicode(s) if strings_only and is_protected_type(s): return s return SafeUnicode( original_force_unicode(s, encoding, strings_only, errors))
def sanitize_html(value, args=''): document = etree.fromstring(u'<body>%s</body>' % value, parser=etree.HTMLParser()) args = args.split(',') + [None, None] id_prefix, class_prefix = args[0] or 'sani', args[1] or 'sani' document = transform(document, 'utils/sanitize_html.xslt', { 'id_prefix': id_prefix, 'class_prefix': class_prefix, }) return SafeUnicode(etree.tostring(document)[6:-7])
def roster_parser(season_id, team_id, division=1): team_season = TeamSeason.objects.select_related().get( team__ncaa_id=team_id, season__end_year=season_id) url = "http://stats.ncaa.org/team/index/%s?org_id=%s" % ( team_season.season.ncaa_id, team_id) soup = soupify(url) rows = soup.findAll('table')[2].findAll('tr') player_links = rows[2:len(rows)] for p in player_links: try: ncaa_id = int( float(p.findAll('td')[1].find('a')['href'].split('=', 2)[2])) name = extract_player_name( p.findAll('td')[1].find('a').contents[0].split(',')) except: ncaa_id = -1 name = extract_player_name( p.findAll('td')[1].contents[0].split(',')) player, player_created = Player.objects.get_or_create(name=name, ncaa_id=ncaa_id) player_season, ps_created = PlayerSeason.objects.get_or_create( player=player, team_season=team_season) if ps_created: try: player_season.jersey = int(p.findAll('td')[0].contents[0]) except: player_season.jersey = None try: player_season.position = SafeUnicode( p.findAll('td')[2].contents[0]) player_season.feet = int( p.findAll('td')[3].contents[0].split('-')[0]) player_season.inches = int( p.findAll('td')[3].contents[0].split('-')[1]) player_season.year = SafeUnicode( p.findAll('td')[4].contents[0]) except: pass player_season.save()
def __unicode__(self): piirto = unicode(self.management_form) for form in self.forms: linkki = "" nimi = "" if form.instance: linkki = unicode(form.instance.id) + "/" nimi = unicode(form.instance.jarjestysnro) + ". " + unicode( form.instance.nimi) piirto = piirto + "<a href=" + linkki + ">" + nimi + "</a> " + form.as_p( ) + "<br>" piirto = piirto.replace("<p>", "").replace("</p>", "") return SafeUnicode(piirto)
def test_safeunicode(self): '''Slug fields have type SafeUnicode, we need to support this field type.''' from testapp.models import Simple from django.core.management import call_command from django.utils.safestring import SafeUnicode call_command('syncdb') n = SafeUnicode('a name') s = Simple(name=n) s.save()
def linked_unicode(item): """Return a unicode string with a HTML link to given item's page. Args: item (django.db.models.Model): a link to this item will be returned Returns: str. the result unicode text """ app = item._meta.app_label pagename = item.__class__.__name__.lower() return SafeUnicode("<a href='/admin/%s/%s/%d/'>%s</a>" % (app, pagename, item.id, item))
def team_parser(season_id=2011, division="1"): # defaults to division 1, but also supports division 3 season = Season.objects.get(end_year=season_id) url = "http://stats.ncaa.org/team/inst_team_list/%s?division=%s" % ( season.ncaa_id, division) soup = soupify(url) team_links = [x.find('a') for x in soup.findAll('td')] for team in team_links: ncaa_id = int(team["href"].split("=")[1]) name = SafeUnicode(team.contents[0]) t, created = Team.objects.get_or_create(ncaa_id=ncaa_id, name=name) team_season, created = TeamSeason.objects.get_or_create(team=t, season=season, division=1)
def _parse_file(self, f): """Build a list of dictionaries from the sites file.""" tree = etree.iterparse(f, events=('start', 'end')) sites = [] site = None for event, element in tree: # print event, element.tag, element.text if element.tag == 'site': if event == 'start': site = {} sites.append(site) if event == 'end': site = None elif site is not None: site[element.tag] = SafeUnicode(element.text) return sites
def __new__(cls, value): # Force input to be Unicode if not already. if isinstance(value, str): value = value.decode(_ASSUMED_ENCODING) # Define the HTML-encoded version of this value # that will be used if this string is substituted # directly into an HTML template. s = SafeUnicode.__new__(cls, u'<span class="splunk-tokensafe" data-value=' + quoteattr(value.encode(_ASSUMED_ENCODING)) + u'></span>' ) # Keep the original value for further processing # in case a token-aware tag receives a TokenSafeString # as an attribute. s.value = value return s
def render( self, name, value, attrs = None ): if attrs is None: attrs={} rendered=super( ForeignKeySearchInput, self ).render( name, value, attrs ) if value: label=self.label_for_value( value ) else: label=u'' return SafeUnicode( rendered+u''' <input type="text" id="lookup_%(name)s" value="%(label)s" size="50" search_fields="%(search_fields)s" app_label="%(app_label)s" model_name="%(model_name)s" autocomplete_mode="ForeignKey" class="to_autocomplete"/> <a class="deletelink" title="Eliminar enlace" onclick="autocomplete_delete(this)"> </a> '''%{ 'search_fields': ','.join( self.search_fields ), 'MEDIA_URL': settings.MEDIA_URL, 'model_name': self.rel.to._meta.module_name, 'app_label': self.rel.to._meta.app_label, 'label': label, 'name': name, 'value': value, } )
def render(self, *args, **kwargs): return SafeUnicode( super(SubdomainInput, self).render(*args, **kwargs) + MUAccount.subdomain_root)
def __unicode__(self): return render_to_string('graficos/grafico_agendamento.html', { 'grafico': self }) + SafeUnicode('<script>' + self.js + '</script>')
def __unicode__(self): return SafeUnicode(self.contenu)[0:15]
def __unicode__(self): return SafeUnicode(self.author) + ' | ' \ + SafeUnicode(self.experiment.id) + ' | ' \ + SafeUnicode(self.order)
def render(self, name, value, attrs=None): original = super(ValueAndHiddenInput, self).render(name, value, attrs) if value is not None: return SafeUnicode(original + smart_unicode(value)) else: return original
class Event(models.Model): name = models.CharField('navn', max_length=100) startdate = models.DateTimeField('startdato') enddate = models.DateTimeField('sluttdato') has_registration = models.BooleanField(SafeUnicode(u'påmelding'), default=False) registration_opens = models.DateTimeField( SafeUnicode(u'påmeldingen åpner'), null=True, blank=True) registration_closes = models.DateTimeField( SafeUnicode(u'påmeldingen stenger'), null=True, blank=True) binding_registration = models.BooleanField( SafeUnicode(u'bindende påmelding'), default=False) number_of_spots = models.IntegerField('antall plasser', null=True, blank=True, help_text='0 = ubegrenset') participants_by_id = models.CommaSeparatedIntegerField('deltager-IDer', max_length=4000, null=True, blank=True) summary = models.TextField('sammendrag') description = models.TextField('beskrivelse') location = models.CharField('sted', max_length=100) class Meta: ordering = ['startdate'] def __unicode__(self): return self.name def to_json(self): json = { 'name': self.name, 'start_date': self.startdate.isoformat(), 'end_date': self.enddate.isoformat(), 'id': self.id, 'has_registration': self.has_registration, 'number_of_spots': self.number_of_spots, 'summary': self.summary, 'description': self.description, } if self.has_registration: json['registration_opens'] = self.registration_opens.isoformat( ) if self.registration_opens else None json['registration_closes'] = self.registration_closes.isoformat( ) if self.registration_closes else None json['binding_registration'] = self.binding_registration return json def get_absolute_url(self): return reverse('event_detail', kwargs={'event_id': str(self.id)}) def duration_as_string(self): if time.days_between(self.startdate, self.enddate) != 0: return 'Fra %s til %s' % ( self._format_date(time.utc_to_nor(self.startdate)), self._format_date(time.utc_to_nor(self.enddate))) else: return 'Fra %s til %s' % (self._format_date( time.utc_to_nor(self.startdate)), time.utc_to_nor( self.enddate).strftime('%H:%M')) def _format_date(self, date): return '%s %d. %s %s' % (_WEEKDAYS[date.weekday()], date.day, _MONTHS[date.month - 1], date.strftime('%H:%M')) def seconds_until_registration_opens(self): if self.has_registration and self.registration_opens: return time.seconds_to(self.registration_opens) else: return 0 def registration_closes_as_string(self): return self._format_date(time.utc_to_nor(self.registration_closes)) def registration_opens_as_string(self): return self._format_date(time.utc_to_nor(self.registration_opens)) def is_over(self): return time.is_past(self.enddate) def is_underway(self): return time.is_past(self.startdate) and time.is_future(self.enddate) def has_opened(self): if self.registration_opens: return time.is_past(time.utc_to_nor(self.registration_opens)) else: return True def percentage_filled(self): num_participants = float(self.num_participants()) percentage = num_participants / self.number_of_spots * 100 return int(percentage) def num_participants(self): return len(self.get_participants_id()) def is_user_participant(self, user): return user.id in self.get_participants_id() def is_open_for_registration(self): if self.registration_opens is None: return True if self.registration_closes is None: return time.is_past(self.registration_opens - timedelta(seconds=1)) else: return time.is_past(self.registration_opens) and time.is_future( self.registration_closes) def is_registration_closed(self): if self.registration_closes is None: return False return time.is_past(self.registration_closes) def is_full(self): if self.number_of_spots is None or self.number_of_spots == 0: return False else: return self.num_participants() >= self.number_of_spots def _add_user(self, user): participants = self.get_participants_id() participants.append(user.id) self.update_participating_users(participants) def add_user(self, user): """ Add the user to the event, if possible. Possible causes if this fails is that the event is not yet open for registration, the event is full, or the user is already registered.""" if not self.is_open_for_registration(): raise UserAddError( "%s isn't open for registration yet! Registration opens %s." % (self.name, self.registration_opens_as_string())) if self.is_full(): raise UserAddError('All %d spots are taken!' % self.number_of_spots) if self.is_user_participant(user): raise UserAddError('%s is already registered to %s!' % (user, self.name)) self._add_user(user) add_user.alters_data = True def _remove_user(self, user): participants = self.get_participants_id() participants.remove(user.id) self.update_participating_users(participants) def remove_user(self, user): if not self.is_user_participant(user): raise NotRegisteredError(event=self, action='Remove user') if self.binding_registration: raise EventError('You cannot unregister from a binding event!') self._remove_user(user) remove_user.alters_data = True def get_participants_id(self): user_ids = [] if self.participants_by_id: user_ids = [ int(user_id) for user_id in self.participants_by_id.split(',') ] return user_ids def get_participating_users(self): if not self.has_opened(): return [] user_ids = self.get_participants_id() users = [User.objects.get(id=user_id) for user_id in user_ids] return users def update_participating_users(self, list_of_new_ids): list_of_new_ids = [str(user_id) for user_id in list_of_new_ids] self.participants_by_id = ','.join(list_of_new_ids) self.save() update_participating_users.alters_data = True def get_participating_emails(self): users = self.get_participating_users() emails = [] for user in users: emails.append(user.email) return emails
def __unicode__(self): padding = self.level * 4 display = ' ' * padding + self.name return SafeUnicode(display)
def getSingleResult(elements): if len(elements) == 1: return SafeUnicode(elements[0]) else: return None
def render(self, name, value=None, attrs=None): return SafeUnicode( super(HelpWidget, self).render(name, value, attrs) ) + '<span onmouseover="tooltip.show(\'' + self.helptext + '\');" onmouseout="tooltip.hide();"><img src="/kipamedia/help_small.png" /></span>'
def render(self, *args, **kwargs): self.attrs = {"style": "width: 10em; display:inline-block;"} return SafeUnicode( super(SubdomainInput, self).render(*args, **kwargs) + settings.IKARI_SUBDOMAIN_ROOT)
def html2plaintext(html, body_id=None, encoding='utf8', width=80): """ from an HTML text, convert the HTML to plain text. If @body_id is provided then this is the tag where the body (not necessarily <body>) starts. """ if encoding == 'utf8': from django.utils.safestring import SafeUnicode html = SafeUnicode(html) from django.utils.encoding import force_unicode html = force_unicode(html) html = html.encode('ascii', 'xmlcharrefreplace') urls = [] if body_id is not None: strainer = SoupStrainer(id=body_id) else: strainer = SoupStrainer('body') soup = BeautifulSoup(html, parseOnlyThese=strainer, fromEncoding=encoding) for link in soup.findAll('a'): title = link.renderContents() for url in [x[1] for x in link.attrs if x[0]=='href']: urls.append(dict(url=str(url), tag=str(link), title=str(title))) html = soup.__str__(encoding) url_index = [] i = 0 for d in urls: if d['title'] == d['url'] or 'http://'+d['title'] == d['url']: html = html.replace(d['tag'], d['url']) elif d['url'].startswith('#'): # don't show anchor content html = html.replace(d['tag'], '') else: i += 1 html = html.replace(d['tag'], '%s [%s]' % (d['title'], i)) url_index.append(d['url']) #html = html.replace('<strong>','*').replace('</strong>','*') #html = html.replace('<b>','*').replace('</b>','*') #html = html.replace('<h3>','*').replace('</h3>','*') #html = html.replace('<h2>','**').replace('</h2>','**') #html = html.replace('<h1>','**').replace('</h1>','**') #html = html.replace('<em>','/').replace('</em>','/') # the only line breaks we respect is those of ending tags and # breaks html = html.replace('\n',' ') html = html.replace('<br>', '\n') html = html.replace('</p>', '\n\n') html = re.sub('<br\s*/>', '\n', html) html = html.replace('</tr>', '\n') #html = html.replace('</table>', '\n\n') html = html.replace(' ' * 2, ' ') # for all other tags we failed to clean up, just remove then and # complain about them on the stderr def desperate_fixer(g): #print >>sys.stderr, "failed to clean up %s" % str(g.group()) return ' ' html = re.sub('<.*?>', desperate_fixer, html) # lstrip all lines html = '\n'.join([x.lstrip() for x in html.splitlines()]) for i, url in enumerate(url_index): if i == 0: html += '\n\n' html += '[%s] %s\n' % (i+1, url) html = unescape(html) # reduce consecutive empty lines to one pat = re.compile(r'(\n\s*\n)+', re.M) html = pat.sub('\n\n', html) # wrap long lines #html = word_wrap(html, width) # Use the python TextWrapper instead of the builtin function wrapper = TextWrapper(width=80) html = wrapper.fill(html) return html
def render(self, name, value, attrs=None): if not 'id' in attrs: attrs['id'] = "id_{0}".format(name) render = super(ColorFieldWidget, self).render(name, value, attrs) return SafeUnicode(u"%s%s" % (render, self.render_script(attrs['id'])))
def render(self, name, value, attrs={}): if not 'id' in attrs: attrs['id'] = "#id_%s" % name render = super(ColorFieldWidget, self).render(name, value, attrs) return SafeUnicode(u"%s%s" % (render, self.render_script(attrs['id'])))
label="Tasapisteissä määräävät tehtävät: 1:", widget=forms.TextInput(attrs={'size': '3'}), initial=1) tasapiste_teht2 = forms.IntegerField( label="2:", widget=forms.TextInput(attrs={'size': '3'}), initial=2) tasapiste_teht3 = forms.IntegerField( label="3:", widget=forms.TextInput(attrs={'size': '3'}), initial=3) vartion_maksimikoko = forms.IntegerField(widget=forms.HiddenInput, required=False) vartion_minimikoko = forms.IntegerField(widget=forms.HiddenInput, required=False) SarjaFormSet = inlineformset_factory(Kisa, Sarja, extra=8, form=SarjaForm) SarjaFormSet.helppiteksti = SafeUnicode( '<span onmouseover="tooltip.show(\'Sarjan <strong>nimet</strong> voivat sisältää ääkkösiä ja välilyöntejä.<br><strong>Tasapisteissä määräävät tehtävät</strong> -kohdat kertovat tasapisteissä määräävien tehtävien numerot. Palaa täyttämään ne määriteltyäsi kyseiset tehtävät.\');" onmouseout="tooltip.hide();"><img src="/kipamedia/help_small.png" /></span>' ) TehtavaValintaFormSet = inlineformset_factory(Sarja, Tehtava, fields=('jarjestysnro', )) class TuhoaTehtavaForm(ModelForm): nimi = forms.CharField(widget=forms.HiddenInput, required=False) tehtavaryhma = forms.CharField(widget=forms.HiddenInput, required=False) tehtavaluokka = forms.CharField(widget=forms.HiddenInput, required=False) rastikasky = forms.CharField(widget=forms.HiddenInput, required=False) jarjestysnro = forms.CharField(widget=forms.HiddenInput, required=False) kaava = forms.CharField(widget=forms.HiddenInput, required=False) sarja = forms.ModelChoiceField(queryset=Sarja.objects.all(),
def test_safestr(self): c = Company(cents_payed=12, products_delivered=1) c.name = SafeUnicode(u'Iñtërnâtiônàlizætiøn1') c.save() c.name = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8')) c.save()
def __unicode__(self): return SafeUnicode(self.jti) + ' | ' \ + SafeUnicode(self.created_time)
def to_img(thumbnail, alt=""): return SafeUnicode('<img src="%s" width="%d" height="%d" alt="%s"/>' \ % ( force_unicode(thumbnail), thumbnail.width(), thumbnail.height(), alt))
def render_context(log_item, context): """ helper tag needed for adding extra context when rendering a LogItem """ return SafeUnicode(log_item.render(**context))