def apply_message_redaction(self, content, replacements=None): if replacements is None: replacements = {} if self.address and replacements.get('address') is not False: for line in self.address.splitlines(): if line.strip(): content = content.replace( line, replacements.get('address', str(_("<< Address removed >>")))) if self.email and replacements.get('email') is not False: content = content.replace( self.email, replacements.get('email', str(_("<< Email removed >>")))) if not self.private or replacements.get('name') is False: return content name_replacement = replacements.get('name', str(_("<< Name removed >>"))) content = replace_custom(settings.FROIDE_CONFIG['greetings'], name_replacement, content) content = replace_word(self.last_name, name_replacement, content) content = replace_word(self.first_name, name_replacement, content) content = replace_word(self.get_full_name(), name_replacement, content) if self.organization: content = replace_word(self.organization, name_replacement, content) return content
def parse_header_field(self, field): if field is None: return None # preprocess head field # see http://stackoverflow.com/questions/7331351/python-email-header-decoding-utf-8 field = re.sub(r"(=\?.*\?=)(?!$)", r"\1 ", field) decodefrag = decode_header(field) fragments = [] for s, enc in decodefrag: if enc: try: s = str(s, enc, errors='replace') except UnicodeDecodeError: # desperate move here try: s = s.decode("latin1") except: pass else: try: if not isinstance(s, str): s = s.decode("latin1") except UnicodeDecodeError: s = str(s, errors='ignore') fragments.append(s.strip(' ')) field = u' '.join(fragments) return field.replace('\n\t', " ").replace('\n', '').replace('\r', '')
def parse_header_field(self, field): if field is None: return None # preprocess head field # see http://stackoverflow.com/questions/7331351/python-email-header-decoding-utf-8 field = re.sub(r"(=\?.*\?=)(?!$)", r"\1 ", field) decodefrag = decode_header(field) fragments = [] for s, enc in decodefrag: if enc: try: s = str(s, enc, errors="replace") except UnicodeDecodeError: # desperate move here try: s = s.decode("latin1") except: pass else: try: if not isinstance(s, str): s = s.decode("latin1") except UnicodeDecodeError: s = str(s, errors="ignore") fragments.append(s.strip(" ")) field = u" ".join(fragments) return field.replace("\n\t", " ").replace("\n", "").replace("\r", "")
def apply_message_redaction(self, content, replacements=None): if replacements is None: replacements = {} if self.address and replacements.get('address') is not False: for line in self.address.splitlines(): if line.strip(): content = content.replace(line, replacements.get('address', str(_("<< Address removed >>"))) ) if self.email and replacements.get('email') is not False: content = content.replace(self.email, replacements.get('email', str(_("<< Email removed >>"))) ) if not self.private or replacements.get('name') is False: return content last_name = self.last_name first_name = self.first_name full_name = self.get_full_name() name_replacement = replacements.get('name', str(_("<< Name removed >>"))) content = content.replace(full_name, name_replacement) content = content.replace(last_name, name_replacement) content = content.replace(first_name, name_replacement) if self.organization: content = content.replace(self.organization, name_replacement) return content
def generate_confirmation_secret(self, *args): to_sign = [str(self.user.pk), self.user.email] for a in args: to_sign.append(str(a)) if self.user.last_login: to_sign.append(self.user.last_login.strftime("%Y-%m-%dT%H:%M:%S")) return hmac.new(settings.SECRET_KEY.encode('utf-8'), (".".join(to_sign)).encode('utf-8')).hexdigest()
def generate_confirmation_secret(self, *args): to_sign = [str(self.user.pk), self.user.email] for a in args: to_sign.append(str(a)) if self.user.last_login: to_sign.append(self.user.last_login.strftime("%Y-%m-%dT%H:%M:%S")) return hmac.new( settings.SECRET_KEY.encode('utf-8'), (".".join(to_sign)).encode('utf-8') ).hexdigest()
def parse_body(self, parts, attachments, body, html): for part in parts: attachment = self.parse_attachment(part) if attachment: attachments.append(attachment) elif part.get_content_type() == "text/plain": charset = part.get_content_charset() or "ascii" body.append(str(part.get_payload(decode=True), charset, "replace")) elif part.get_content_type() == "text/html": charset = part.get_content_charset() or "ascii" html.append(str(part.get_payload(decode=True), charset, "replace"))
def parse_body(self, parts, attachments, body, html): for part in parts: attachment = self.parse_attachment(part) if attachment: attachments.append(attachment) elif part.get_content_type() == "text/plain": charset = part.get_content_charset() or 'ascii' body.append( str(part.get_payload(decode=True), charset, 'replace')) elif part.get_content_type() == "text/html": charset = part.get_content_charset() or 'ascii' html.append( str(part.get_payload(decode=True), charset, 'replace'))
def apply_message_redaction(self, content, replacements=None): if replacements is None: replacements = {} if self.address and replacements.get('address') is not False: for line in self.address.splitlines(): if line.strip(): content = content.replace(line, replacements.get('address', str(_("<< Address removed >>"))) ) if self.email and replacements.get('email') is not False: content = content.replace(self.email, replacements.get('email', str(_("<< Email removed >>"))) ) if not self.private or replacements.get('name') is False: return content last_name = self.last_name first_name = self.first_name full_name = self.get_full_name() name_replacement = replacements.get('name', str(_("<< Name removed >>"))) content = content.replace(full_name, name_replacement) content = content.replace(last_name, name_replacement) content = content.replace(first_name, name_replacement) if self.organization: content = content.replace(self.organization, name_replacement) greeting_replacement = replacements.get('greeting', str(_("<< Greeting >>"))) if settings.FROIDE_CONFIG.get('greetings'): for greeting in settings.FROIDE_CONFIG['greetings']: match = greeting.search(content, re.I) if match is not None and len(match.groups()): content = content.replace(match.group(1), greeting_replacement) if settings.FROIDE_CONFIG.get('closings'): for closing in settings.FROIDE_CONFIG['closings']: match = closing.search(content, re.I) if match is not None: content = content[:match.end()] return content
class UserDeleteForm(forms.Form): CONFIRMATION_PHRASE = str(_('Freedom of Information Act')) password = forms.CharField( widget=forms.PasswordInput(attrs={'class': 'form-control'}), label=_('Password'), help_text=_('Please type your password to confirm.')) confirmation = forms.CharField( widget=ConfirmationWidget(phrase=CONFIRMATION_PHRASE, attrs={'class': 'form-control'}), label=_('Confirmation Phrase'), help_text=_('Type the phrase above exactly as displayed.')) def __init__(self, user, *args, **kwargs): self.user = user super(UserDeleteForm, self).__init__(*args, **kwargs) def clean_password(self): password = self.cleaned_data['password'] user = auth.authenticate(username=self.user.email, password=password) if not user: raise forms.ValidationError(_('You provided the wrong password!')) return '' def clean_confirmation(self): confirmation = self.cleaned_data['confirmation'] if confirmation != self.CONFIRMATION_PHRASE: raise forms.ValidationError( _('You did not type the confirmation phrase exactly right!')) return ''
def send_confirmation_mail(self, request_id=None, password=None, reference=None, redirect_url=None): secret = self.generate_confirmation_secret(request_id) url_kwargs = {"user_id": self.user.pk, "secret": secret} if request_id: url_kwargs['request_id'] = request_id url = reverse('account-confirm', kwargs=url_kwargs) params = {} if reference: params['ref'] = reference.encode('utf-8') if redirect_url: params['next'] = redirect_url.encode('utf-8') if params: url = '%s?%s' % (url, urlencode(params)) message = render_to_string('account/confirmation_mail.txt', {'url': settings.SITE_URL + url, 'password': password, 'name': self.user.get_full_name(), 'site_name': settings.SITE_NAME, 'site_url': settings.SITE_URL }) # Translators: Mail subject send_mail(str(_("%(site_name)s: please confirm your account") % { "site_name": settings.SITE_NAME}), message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
def generate_autologin_secret(self): to_sign = [str(self.user.pk)] if self.user.last_login: to_sign.append(self.user.last_login.strftime("%Y-%m-%dT%H:%M:%S")) return hmac.new(settings.SECRET_KEY.encode('utf-8'), (".".join(to_sign)).encode('utf-8'), digestmod=hashlib.md5).hexdigest()
def redact_content(content): content = replace_email_name(content, _("<<name and email address>>")) content = replace_email(content, _("<<email address>>")) if settings.FROIDE_CONFIG.get('custom_replacements'): content = replace_custom(settings.FROIDE_CONFIG['custom_replacements'], str(_('<<removed>>')), content) return content
def generate_autologin_secret(self): to_sign = [str(self.user.pk)] if self.user.last_login: to_sign.append(self.user.last_login.strftime("%Y-%m-%dT%H:%M:%S")) return hmac.new( settings.SECRET_KEY.encode('utf-8'), (".".join(to_sign)).encode('utf-8') ).hexdigest()
def display_name(self): if self.private: return str(_(u"<< Name Not Public >>")) else: if self.organization: return u'%s (%s)' % (self.get_full_name(), self.organization) else: return self.get_full_name()
def display_name(self): if self.private: return str(_(u"Name Not Public")) else: if self.organization: return u'%s (%s)' % (self.get_full_name(), self.organization) else: return self.get_full_name()
def test_go(self): user = User.objects.get(username='******') other_user = User.objects.get(username='******') # test url is not cached and does not cause 404 test_url = reverse('foirequest-make_request') # Try logging in via link: success autologin = user.get_autologin_url(test_url) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'], user) self.assertTrue(response.context['user'].is_authenticated) self.client.logout() # Try logging in via link: other user is authenticated ok = self.client.login(email='*****@*****.**', password='******') self.assertTrue(ok) autologin = user.get_autologin_url(test_url) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'], other_user) self.assertTrue(response.context['user'].is_authenticated) self.client.logout() # Try logging in via link: user not active autologin = user.get_autologin_url(test_url) user.is_active = False user.save() response = self.client.get(autologin) self.assertEqual(response.status_code, 404) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous) # Try logging in via link: wrong user id autologin = reverse('account-go', kwargs=dict(user_id='80000', secret='a' * 32, url=test_url)) response = self.client.get(autologin) self.assertEqual(response.status_code, 404) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous) user.is_active = True user.save() # Try logging in via link: wrong secret autologin = reverse('account-go', kwargs=dict(user_id=str(user.id), secret='a' * 32, url=test_url)) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous)
def test_go(self): user = User.objects.get(username='******') other_user = User.objects.get(username='******') # test url is not cached and does not cause 404 test_url = reverse('foirequest-make_request') # Try logging in via link: success autologin = user.get_autologin_url(test_url) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'], user) self.assertTrue(response.context['user'].is_authenticated()) self.client.logout() # Try logging in via link: other user is authenticated ok = self.client.login(username='******', password='******') self.assertTrue(ok) autologin = user.get_autologin_url(test_url) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'], other_user) self.assertTrue(response.context['user'].is_authenticated()) self.client.logout() # Try logging in via link: user not active autologin = user.get_autologin_url(test_url) user.is_active = False user.save() response = self.client.get(autologin) self.assertEqual(response.status_code, 404) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous()) # Try logging in via link: wrong user id autologin = reverse('account-go', kwargs=dict( user_id='80000', secret='a' * 32, url=test_url )) response = self.client.get(autologin) self.assertEqual(response.status_code, 404) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous()) user.is_active = True user.save() # Try logging in via link: wrong secret autologin = reverse('account-go', kwargs=dict( user_id=str(user.id), secret='a' * 32, url=test_url )) response = self.client.get(autologin) self.assertEqual(response.status_code, 302) response = self.client.get(test_url) self.assertTrue(response.context['user'].is_anonymous())
def test_request_prefilled_redirect(self): p = PublicBody.objects.all()[0] query = '?body=THEBODY&subject=THESUBJECT' response = self.client.get( reverse('foirequest-make_request', kwargs={'public_body_id': str(p.pk)}) + query) self.assertRedirects(response, reverse('foirequest-make_request', kwargs={'public_body': p.slug}) + query, status_code=301)
def test_request_prefilled_redirect(self): p = PublicBody.objects.all()[0] query = '?body=THEBODY&subject=THESUBJECT' response = self.client.get(reverse('foirequest-make_request', kwargs={'public_body_id': str(p.pk)}) + query) self.assertRedirects( response, reverse('foirequest-make_request', kwargs={ 'public_body': p.slug }) + query, status_code=301 )
def send_confirmation_mail(self, request_id=None, password=None): secret = self.generate_confirmation_secret(request_id) url_kwargs = {"user_id": self.user.pk, "secret": secret} if request_id: url_kwargs['request_id'] = request_id url = reverse('account-confirm', kwargs=url_kwargs) message = render_to_string('account/confirmation_mail.txt', {'url': settings.SITE_URL + url, 'password': password, 'name': self.user.get_full_name(), 'site_name': settings.SITE_NAME, 'site_url': settings.SITE_URL }) # Translators: Mail subject send_mail(str(_("%(site_name)s: please confirm your account") % { "site_name": settings.SITE_NAME}), message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
def test_list_requests(self): response = self.client.get(reverse('foirequest-list')) self.assertEqual(response.status_code, 200) for urlpart in FOIREQUEST_FILTER_DICT: response = self.client.get( reverse('foirequest-list', kwargs={"status": str(urlpart)})) self.assertEqual(response.status_code, 200) for topic in Category.objects.filter(is_topic=True): response = self.client.get( reverse('foirequest-list', kwargs={"topic": topic.slug})) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_not_foi')) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list') + '?page=99999') self.assertEqual(response.status_code, 200)
def redact_plaintext(content, is_response=True, user=None): content = redact_content(content) greeting_replacement = str(_("<< Greeting >>")) if not settings.FROIDE_CONFIG.get('public_body_officials_public'): if is_response: content = remove_closing(content) else: if settings.FROIDE_CONFIG.get('greetings'): content = replace_custom(settings.FROIDE_CONFIG['greetings'], greeting_replacement, content) if user: account_service = user.get_account_service() content = account_service.apply_message_redaction(content) return content
def test_list_requests(self): response = self.client.get(reverse('foirequest-list')) self.assertEqual(response.status_code, 200) for urlpart, _, status in FoiRequest.get_status_url(): response = self.client.get( reverse('foirequest-list', kwargs={"status": str(urlpart)})) self.assertEqual(response.status_code, 200) url = reverse('foirequest-list', kwargs={"status": 'successful'}) url = url.replace('successful', 'non-existing') response = self.client.get(url) self.assertEqual(response.status_code, 404) for topic in Category.objects.filter(is_topic=True): response = self.client.get( reverse('foirequest-list', kwargs={"topic": topic.slug})) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_not_foi')) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list') + '?page=99999') self.assertEqual(response.status_code, 200)
def send_email_change_mail(self, email): secret = self.generate_confirmation_secret(email) url_kwargs = { "user_id": self.user.pk, "secret": secret, "email": email } url = '%s%s?%s' % ( settings.SITE_URL, reverse('account-change_email'), urlencode(url_kwargs) ) message = render_to_string('account/change_email.txt', {'url': url, 'name': self.user.get_full_name(), 'site_name': settings.SITE_NAME, 'site_url': settings.SITE_URL }) # Translators: Mail subject send_mail(str(_("%(site_name)s: please confirm your new email address") % { "site_name": settings.SITE_NAME}), message, settings.DEFAULT_FROM_EMAIL, [email])
def send_email_change_mail(self, email): secret = self.generate_confirmation_secret(email) url_kwargs = { "user_id": self.user.pk, "secret": secret, "email": email } url = '%s%s?%s' % (settings.SITE_URL, reverse('account-change_email'), urlencode(url_kwargs)) message = render_to_string( 'account/change_email.txt', { 'url': url, 'name': self.user.get_full_name(), 'site_name': settings.SITE_NAME, 'site_url': settings.SITE_URL }) # Translators: Mail subject send_mail( str( _("%(site_name)s: please confirm your new email address") % {"site_name": settings.SITE_NAME}), message, settings.DEFAULT_FROM_EMAIL, [email])
def test_list_requests(self): response = self.client.get(reverse('foirequest-list')) self.assertEqual(response.status_code, 200) for urlpart, _, status in FoiRequest.STATUS_URLS: response = self.client.get(reverse('foirequest-list', kwargs={"status": str(urlpart)})) self.assertEqual(response.status_code, 200) url = reverse('foirequest-list', kwargs={"status": 'successful'}) url = url.replace('successful', 'non-existing') response = self.client.get(url) self.assertEqual(response.status_code, 404) for topic in PublicBodyTag.objects.filter(is_topic=True): response = self.client.get(reverse('foirequest-list', kwargs={"topic": topic.slug})) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_not_foi')) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list') + '?page=99999') self.assertEqual(response.status_code, 200)
def send_team_invite(self, invited_by): secret = self.generate_invite_secret() url_kwargs = { "pk": self.member.pk, "secret": secret, } url = '%s%s' % ( settings.SITE_URL, reverse('team-join', kwargs=url_kwargs), ) message = render_to_string( 'team/emails/team_invite.txt', { 'url': url, 'name': invited_by.get_full_name(), 'site_name': settings.SITE_NAME, 'site_url': settings.SITE_URL }) # Translators: Mail subject send_mail( str( _("%(site_name)s: team invite from %(name)s") % { "site_name": settings.SITE_NAME, "name": invited_by.get_full_name() }), message, settings.DEFAULT_FROM_EMAIL, [self.member.email])
def get_status_url(cls): if cls._STATUS_URLS is None: cls._STATUS_URLS = [(str(s), t, u) for s, t, u in cls.STATUS_URLS] return cls._STATUS_URLS
def replace_email_name(text, replacement=u""): return EMAIL_NAME_RE.sub(str(replacement), text)
def generate_invite_secret(self): to_sign = [str(self.member.pk), str(self.member.email)] return hmac.new(settings.SECRET_KEY.encode('utf-8'), ('.'.join(to_sign)).encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
def get_status_description(cls, status): return str(cls.STATUS_RESOLUTION_DICT.get(status, (None, _("Unknown")))[1])
def list_requests( request, status=None, topic=None, tag=None, jurisdiction=None, public_body=None, not_foi=False, feed=None ): context = {"filtered": True} manager = FoiRequest.published if not_foi: manager = FoiRequest.published_not_foi topic_list = PublicBodyTag.objects.get_topic_list() if status is None: status = request.GET.get(str(_("status")), None) status_url = status foi_requests = manager.for_list_view() if status is not None: func_status = FoiRequest.get_status_from_url(status) if func_status is None: raise Http404 func, status = func_status foi_requests = foi_requests.filter(func(status)) context.update( { "status": FoiRequest.get_readable_status(status), "status_description": FoiRequest.get_status_description(status), } ) elif topic is not None: topic = get_object_or_404(PublicBodyTag, slug=topic) foi_requests = manager.for_list_view().filter(public_body__tags=topic) context.update({"topic": topic}) elif tag is not None: tag = get_object_or_404(Tag, slug=tag) foi_requests = manager.for_list_view().filter(tags=tag) context.update({"tag": tag}) else: foi_requests = manager.for_list_view() context["filtered"] = False if jurisdiction is not None: jurisdiction = get_object_or_404(Jurisdiction, slug=jurisdiction) foi_requests = foi_requests.filter(jurisdiction=jurisdiction) context.update({"jurisdiction": jurisdiction}) elif public_body is not None: public_body = get_object_or_404(PublicBody, slug=public_body) foi_requests = foi_requests.filter(public_body=public_body) context.update({"public_body": public_body}) context["filtered"] = True context["jurisdiction_list"] = Jurisdiction.objects.get_visible() else: context["jurisdiction_list"] = Jurisdiction.objects.get_visible() context["filtered"] = False if feed is not None: if feed == "rss": klass = LatestFoiRequestsFeed else: klass = LatestFoiRequestsFeedAtom return klass(foi_requests, status=status_url, topic=topic, tag=tag, jurisdiction=jurisdiction)(request) count = foi_requests.count() page = request.GET.get("page") paginator = Paginator(foi_requests, 20) try: foi_requests = paginator.page(page) except PageNotAnInteger: foi_requests = paginator.page(1) except EmptyPage: foi_requests = paginator.page(paginator.num_pages) context.update( { "page_title": _("FoI Requests"), "count": count, "not_foi": not_foi, "object_list": foi_requests, "status_list": [(str(x[0]), FoiRequest.get_readable_status(x[2]), x[2]) for x in FoiRequest.STATUS_URLS], "topic_list": topic_list, } ) return render(request, "foirequest/list.html", context)
def get_url_from_status(cls, status): if cls._STATUS_URLS_DICT is None: cls._STATUS_URLS_DICT = dict([(str(x[-1]), x[0]) for x in cls.get_status_url()]) return cls._STATUS_URLS_DICT.get(status)
name='foirequest-list_feed'), # Translators: part in request filter URL url(r'^%s/(?P<public_body>[-\w]+)/$' % pgettext('URL part', 'to'), list_requests, name='foirequest-list'), url(r'^%s/(?P<public_body>[-\w]+)/feed/$' % pgettext('URL part', 'to'), list_requests, kwargs={'feed': 'atom'}, name='foirequest-list_feed_atom'), url(r'^%s/(?P<public_body>[-\w]+)/rss/$' % pgettext('URL part', 'to'), list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed'), ] + [ url(r'^(?P<status>%s)/$' % str(urlinfo[0]), list_requests, name='foirequest-list') for urlinfo in FoiRequest.get_status_url() ] + [ url(r'^(?P<status>%s)/feed/$' % str(urlinfo[0]), list_requests, kwargs={'feed': 'atom'}, name='foirequest-list_feed_atom') for urlinfo in FoiRequest.get_status_url() ] + [ url(r'^(?P<status>%s)/rss/$' % str(urlinfo[0]), list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed') for urlinfo in FoiRequest.get_status_url() ]
def replace_email(text, replacement=""): return EMAIL_RE.sub(str(replacement), text)
def replace_email_name(text, replacement=""): return EMAIL_NAME_RE.sub(str(replacement), text)
def test_model(self): self.assertIn(self.article.title, str(self.article)) self.assertIn(self.source.name, str(self.source))
def replace_email(text, replacement=u""): return EMAIL_RE.sub(str(replacement), text)
def add_message_from_email(self, email, publicbody=None): from .message import FoiMessage from .attachment import FoiAttachment message = FoiMessage(request=self) message.subject = email['subject'] or '' message.subject = message.subject[:250] message_id = email.get('message_id', '') if message_id: message.email_message_id = message_id[:512] message.is_response = True message.sender_name = email['from'][0] message.sender_email = email['from'][1] if publicbody is None: publicbody = get_publicbody_for_email(message.sender_email, self) if publicbody is None: publicbody = self.public_body message.sender_public_body = publicbody if message.sender_public_body == self.law.mediator: message.content_hidden = True if email['date'] is None: message.timestamp = timezone.now() else: message.timestamp = email['date'] message.recipient_email = self.secret_address message.recipient = self.user.display_name() message.plaintext = email['body'] message.html = email['html'] if not message.plaintext and message.html: message.plaintext = strip_tags(email['html']) message.subject_redacted = message.redact_subject()[:250] message.plaintext_redacted = message.redact_plaintext() message.save() self._messages = None self.status = 'awaiting_classification' self.last_message = message.timestamp self.save() has_pdf = False account_service = AccountService(self.user) for i, attachment in enumerate(email['attachments']): att = FoiAttachment(belongs_to=message, name=attachment.name, size=attachment.size, filetype=attachment.content_type) if not att.name: att.name = _("attached_file_%d") % i # Translators: replacement for person name in filename repl = str(_('NAME')) att.name = account_service.apply_name_redaction(att.name, repl) att.name = re.sub(r'[^A-Za-z0-9_\.\-]', '', att.name) att.name = att.name[:255] if att.name.endswith('pdf') or 'pdf' in att.filetype: has_pdf = True attachment._committed = False att.file = File(attachment) att.save() if (has_pdf and settings.FROIDE_CONFIG.get( "mail_managers_on_pdf_attachment", False)): mail_managers(_('Message contains PDF'), self.get_absolute_domain_url()) self.message_received.send(sender=self, message=message)
def get_status_from_url(cls, status_slug): if cls._URLS_STATUS_DICT is None: cls._URLS_STATUS_DICT = dict([(str(x[0]), x[1:]) for x in cls.get_status_url()]) return cls._URLS_STATUS_DICT.get(status_slug)
list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed'), # Translators: part in request filter URL url(r'^%s/(?P<tag>[-\w]+)/$' % pgettext('URL part', 'tag'), list_requests, name='foirequest-list'), url(r'^%s/(?P<tag>[-\w]+)/feed/$' % pgettext('URL part', 'tag'), list_requests, kwargs={'feed': 'atom'}, name='foirequest-list_feed_atom'), url(r'^%s/(?P<tag>[-\w]+)/rss/$' % pgettext('URL part', 'tag'), list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed'), # Translators: part in request filter URL url(r'^%s/(?P<public_body>[-\w]+)/$' % pgettext('URL part', 'to'), list_requests, name='foirequest-list'), url(r'^%s/(?P<public_body>[-\w]+)/feed/$' % pgettext('URL part', 'to'), list_requests, kwargs={'feed': 'atom'}, name='foirequest-list_feed_atom'), url(r'^%s/(?P<public_body>[-\w]+)/rss/$' % pgettext('URL part', 'to'), list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed'), ] + [url(r'^(?P<status>%s)/$' % str(urlinfo[0]), list_requests, name='foirequest-list') for urlinfo in FoiRequest.get_status_url() ] + [url(r'^(?P<status>%s)/feed/$' % str(urlinfo[0]), list_requests, kwargs={'feed': 'atom'}, name='foirequest-list_feed_atom') for urlinfo in FoiRequest.get_status_url() ] + [url(r'^(?P<status>%s)/rss/$' % str(urlinfo[0]), list_requests, kwargs={'feed': 'rss'}, name='foirequest-list_feed') for urlinfo in FoiRequest.get_status_url()] urlpatterns += foirequest_urls urlpatterns += [ url(r'^(?P<jurisdiction>[-\w]+)/', include(foirequest_urls)) ]
def get(self, field): if isinstance(field, bytes): return field return str(field)
def get_readable_status(cls, status): return str(cls.STATUS_RESOLUTION_DICT.get(status, (_("Unknown"), None))[0])
def list_requests(request, status=None, topic=None, tag=None, jurisdiction=None, public_body=None, not_foi=False, feed=None): context = { 'filtered': True } manager = FoiRequest.published if not_foi: manager = FoiRequest.published_not_foi topic_list = PublicBodyTag.objects.get_topic_list() if status is None: status = request.GET.get(str(_('status')), None) status_url = status foi_requests = manager.for_list_view() if status is not None: func_status = FoiRequest.get_status_from_url(status) if func_status is None: raise Http404 func, status = func_status foi_requests = foi_requests.filter(func(status)) context.update({ 'status': FoiRequest.get_readable_status(status), 'status_description': FoiRequest.get_status_description(status) }) elif topic is not None: topic = get_object_or_404(PublicBodyTag, slug=topic) foi_requests = manager.for_list_view().filter(public_body__tags=topic) context.update({ 'topic': topic, }) elif tag is not None: tag = get_object_or_404(Tag, slug=tag) foi_requests = manager.for_list_view().filter(tags=tag) context.update({ 'tag': tag }) else: foi_requests = manager.for_list_view() context['filtered'] = False if jurisdiction is not None: jurisdiction = get_object_or_404(Jurisdiction, slug=jurisdiction) foi_requests = foi_requests.filter(jurisdiction=jurisdiction) context.update({ 'jurisdiction': jurisdiction }) elif public_body is not None: public_body = get_object_or_404(PublicBody, slug=public_body) foi_requests = foi_requests.filter(public_body=public_body) context.update({ 'public_body': public_body }) context['filtered'] = True context['jurisdiction_list'] = Jurisdiction.objects.get_visible() else: context['jurisdiction_list'] = Jurisdiction.objects.get_visible() context['filtered'] = False if feed is not None: if feed == 'rss': klass = LatestFoiRequestsFeed else: klass = LatestFoiRequestsFeedAtom return klass(foi_requests, status=status_url, topic=topic, tag=tag, jurisdiction=jurisdiction)(request) count = foi_requests.count() page = request.GET.get('page') paginator = Paginator(foi_requests, 20) try: foi_requests = paginator.page(page) except PageNotAnInteger: foi_requests = paginator.page(1) except EmptyPage: foi_requests = paginator.page(paginator.num_pages) context.update({ 'page_title': _("FoI Requests"), 'count': count, 'not_foi': not_foi, 'object_list': foi_requests, 'status_list': [(str(x[0]), FoiRequest.get_readable_status(x[2]), x[2]) for x in FoiRequest.get_status_url()], 'topic_list': topic_list }) return render(request, 'foirequest/list.html', context)
(r'^%s/(?P<tag>[-\w]+)/$' % pgettext('URL part', 'tag'), 'list_requests', {}, 'foirequest-list'), (r'^%s/(?P<tag>[-\w]+)/feed/$' % pgettext('URL part', 'tag'), 'list_requests', {'feed': 'atom'}, 'foirequest-list_feed_atom'), (r'^%s/(?P<tag>[-\w]+)/rss/$' % pgettext('URL part', 'tag'), 'list_requests', {'feed': 'rss'}, 'foirequest-list_feed'), # Translators: part in request filter URL (r'^%s/(?P<public_body>[-\w]+)/$' % pgettext('URL part', 'to'), 'list_requests', {}, 'foirequest-list'), (r'^%s/(?P<public_body>[-\w]+)/feed/$' % pgettext('URL part', 'to'), 'list_requests', {'feed': 'atom'}, 'foirequest-list_feed_atom'), (r'^%s/(?P<public_body>[-\w]+)/rss/$' % pgettext('URL part', 'to'), 'list_requests', {'feed': 'rss'}, 'foirequest-list_feed'), ] + [(r'^(?P<status>%s)/$' % str(urlinfo[0]), 'list_requests', {}, 'foirequest-list') for urlinfo in FoiRequest.STATUS_URLS ] + [(r'^(?P<status>%s)/feed/$' % str(urlinfo[0]), 'list_requests', {'feed': 'atom'}, 'foirequest-list_feed_atom') for urlinfo in FoiRequest.STATUS_URLS ] + [(r'^(?P<status>%s)/rss/$' % str(urlinfo[0]), 'list_requests', {'feed': 'rss'}, 'foirequest-list_feed') for urlinfo in FoiRequest.STATUS_URLS] urlpatterns += patterns("froide.foirequest.views", *foirequest_urls ) urlpatterns += patterns("froide.foirequest.views", *[(r'^(?P<jurisdiction>[-\w]+)/%s' % r[0][1:], r[1], r[2], r[3]) for r in foirequest_urls] )
def test_show_law(self): law = FoiLaw.objects.filter(meta=False)[0] self.assertIn(law.jurisdiction.name, str(law)) response = self.client.get(law.get_absolute_url()) self.assertEqual(response.status_code, 200) self.assertIn(law.name, response.content.decode("utf-8"))
from django.utils.six import text_type as str from django.urls import reverse from django.conf.urls import url, include from django.utils.translation import pgettext from django.shortcuts import redirect from ..views import ( list_requests, list_unchecked, delete_draft, ) from ..filters import FOIREQUEST_FILTERS STATUS_URLS = [str(x[0]) for x in FOIREQUEST_FILTERS] urlpatterns = [ url(r'^%s/$' % pgettext('URL part', 'not-foi'), list_requests, kwargs={'not_foi': True}, name='foirequest-list_not_foi'), # Old feed URL url(r'^%s/feed/$' % pgettext('URL part', 'latest'), lambda r: redirect(reverse('foirequest-list_feed_atom'), permanent=True), name='foirequest-feed_latest_atom'), url(r'^%s/rss/$' % pgettext('URL part', 'latest'), lambda r: redirect(reverse('foirequest-list_feed'), permanent=True), name='foirequest-feed_latest'), url(r'^unchecked/$', list_unchecked, name='foirequest-list_unchecked'), url(r'^delete-draft$', delete_draft, name='foirequest-delete_draft'),