Exemplo n.º 1
0
    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", "")
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
 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()
Exemplo n.º 4
0
 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"))
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
 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()
Exemplo n.º 7
0
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
Exemplo n.º 8
0
 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()
Exemplo n.º 9
0
Arquivo: tests.py Projeto: fin/froide
    def test_go(self):
        user = User.objects.get(username='dummy')
        other_user = User.objects.get(username='sw')
        # 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='sw', password='froide')
        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())
Exemplo n.º 10
0
 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
     )
Exemplo n.º 11
0
 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])
Exemplo n.º 12
0
 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])
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
        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))
]
Exemplo n.º 15
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.STATUS_URLS],
            "topic_list": topic_list,
        }
    )

    return render(request, "foirequest/list.html", context)
Exemplo n.º 16
0
    (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]
)
Exemplo n.º 17
0
 def get(self, field):
     if isinstance(field, bytes):
         return field
     return str(field)
Exemplo n.º 18
0
def replace_email(text, replacement=u""):
    return EMAIL_RE.sub(str(replacement), text)
Exemplo n.º 19
0
def replace_email_name(text, replacement=u""):
    return EMAIL_NAME_RE.sub(str(replacement), text)
Exemplo n.º 20
0
 def test_model(self):
     self.assertIn(self.article.title, str(self.article))
     self.assertIn(self.source.name, str(self.source))
Exemplo n.º 21
0
 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"))
Exemplo n.º 22
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)