def confirm(request, user_id, secret, request_id=None): if request.user.is_authenticated(): messages.add_message(request, messages.ERROR, _('You are logged in and cannot use a confirmation link.')) return redirect('account-show') user = get_object_or_404(auth.models.User, pk=int(user_id)) if user.is_active: return redirect('account-login') account_manager = AccountManager(user) if account_manager.confirm_account(secret, request_id): messages.add_message(request, messages.WARNING, _('Your email address is now confirmed and you are logged in. You should change your password now by filling out the form below.')) login_user(request, user) if request_id is not None: foirequest = FoiRequest.confirmed_request(user, request_id) if foirequest: messages.add_message(request, messages.SUCCESS, _('Your request "%s" has now been sent') % foirequest.title) next = request.GET.get('next', request.session.get('next')) if next: if 'next' in request.session: del request.session['next'] return redirect(next) return redirect(reverse('account-show') + "?new#change-password-now") else: messages.add_message(request, messages.ERROR, _('You can only use the confirmation link once, please login with your password.')) return redirect('account-login')
def confirm(request, user_id, secret, request_id=None): if request.user.is_authenticated: if request.user.id != user_id: messages.add_message( request, messages.ERROR, _('You are logged in and cannot use a confirmation link.')) return redirect('account-show') user = get_object_or_404(auth.get_user_model(), pk=int(user_id)) if user.is_active or (not user.is_active and user.email is None): return redirect('account-login') account_service = AccountService(user) result = account_service.confirm_account(secret, request_id) if not result: messages.add_message( request, messages.ERROR, _('You can only use the confirmation link once, ' 'please login with your password.')) return redirect('account-login') auth.login(request, user) params = {} if request.GET.get('ref'): params['ref'] = request.GET['ref'] if request_id is not None: foirequest = FoiRequest.confirmed_request(user, request_id) if foirequest: params['request'] = str(foirequest.pk).encode('utf-8') default_url = reverse('account-confirmed') + urlencode(params) return get_redirect(request, default=default_url, params=params)
def test_feed(self): response = self.client.get(reverse('foirequest-feed_latest')) self.assertRedirects(response, reverse('foirequest-list_feed'), status_code=301) response = self.client.get(reverse('foirequest-feed_latest_atom')) self.assertRedirects(response, reverse('foirequest-list_feed_atom'), status_code=301) response = self.client.get(reverse('foirequest-list_feed')) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_feed_atom')) self.assertEqual(response.status_code, 200) juris = Jurisdiction.objects.all()[0] response = self.client.get(reverse('foirequest-list_feed', kwargs={ 'jurisdiction': juris.slug })) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_feed_atom', kwargs={ 'jurisdiction': juris.slug })) self.assertEqual(response.status_code, 200) topic = PublicBodyTag.objects.filter(is_topic=True)[0] response = self.client.get(reverse('foirequest-list_feed', kwargs={ 'jurisdiction': juris.slug, 'topic': topic.slug })) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_feed_atom', kwargs={ 'jurisdiction': juris.slug, 'topic': topic.slug })) self.assertEqual(response.status_code, 200) status = FoiRequest.get_status_url()[0][0] response = self.client.get(reverse('foirequest-list_feed', kwargs={ 'jurisdiction': juris.slug, 'status': status })) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-list_feed_atom', kwargs={ 'jurisdiction': juris.slug, 'status': status })) self.assertEqual(response.status_code, 200) req = FoiRequest.objects.all()[0] response = self.client.get(reverse('foirequest-feed_atom', kwargs={"slug": req.slug})) self.assertEqual(response.status_code, 200) response = self.client.get(reverse('foirequest-feed', kwargs={"slug": req.slug})) self.assertEqual(response.status_code, 200)
def test_list_jurisdiction_requests(self): juris = Jurisdiction.objects.all()[0] response = self.client.get(reverse('foirequest-list'), kwargs={'jurisdiction': juris.slug}) self.assertEqual(response.status_code, 200) for urlpart, _, status in FoiRequest.get_status_url(): response = self.client.get(reverse('foirequest-list', kwargs={"status": urlpart, 'jurisdiction': juris.slug})) self.assertEqual(response.status_code, 200) for topic in PublicBodyTag.objects.filter(is_topic=True): response = self.client.get(reverse('foirequest-list', kwargs={"topic": topic.slug, 'jurisdiction': juris.slug})) self.assertEqual(response.status_code, 200)
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 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 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 _batch_update(update_requester=True, update_follower=True): event_black_list = ("message_received", "message_sent", 'set_concrete_law',) translation.activate(settings.LANGUAGE_CODE) requests = {} users = {} gte_date = timezone.now() - timedelta(days=1) updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) for comment in Comment.objects.filter(content_type=message_type, submit_date__gte=gte_date): try: message = FoiMessage.objects.get(pk=comment.object_pk) if message.request_id not in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( ( comment.submit_date, _("%(time)s: New comment by %(name)s") % { "time": tf.format(_(settings.TIME_FORMAT)), "name": comment.name }, comment.user_id ) ) except FoiMessage.DoesNotExist: pass if update_requester: requester_updates = defaultdict(dict) # send out update on comments to request users for req_id, request in iteritems(requests): if not request.user.is_active: continue if not request.user.email: continue if not updates[req_id]: continue if not any([x for x in updates[req_id] if x[2] != request.user_id]): continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) requester_updates[request.user][request] = { 'events': [x[1] for x in sorted_events] } for user, request_dict in iteritems(requester_updates): FoiRequest.send_update(request_dict, user=user) if update_follower: # update followers for event in FoiEvent.objects.filter(timestamp__gte=gte_date).select_related("request"): if event.event_name in event_black_list: continue if event.request_id not in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( ( event.timestamp, _("%(time)s: %(text)s") % { "time": tf.format(_("TIME_FORMAT")), "text": event.as_text() }, event.user_id ) ) # Send out update on comments and event to followers follower_updates = defaultdict(dict) for req_id, request in iteritems(requests): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) followers = FoiRequestFollower.objects.filter( request=request).select_related('user') for follower in followers: if follower.user is None and not follower.confirmed: continue if follower.user and ( not follower.user.is_active or not follower.user.email): continue if not request.is_visible(None): continue if not any([x for x in updates[req_id] if x[2] != follower.user_id]): continue users[follower.user_id] = follower.user ident = follower.user_id or follower.email follower_updates[ident][request] = { 'unfollow_link': follower.get_unfollow_link(), 'events': [x[1] for x in updates[req_id]] } for user_id, req_event_dict in iteritems(follower_updates): user = users.get(user_id) email = None if user is None: email = user_id FoiRequestFollower.send_update(req_event_dict, user=user, email=email)
def _batch_update(update_requester=True, update_follower=True, since=None): if since is None: since = timezone.now() - timedelta(days=1) event_black_list = ( "message_received", "message_sent", 'set_concrete_law', ) translation.activate(settings.LANGUAGE_CODE) requests = {} users = {} updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) comments = Comment.objects.filter(content_type=message_type, submit_date__gte=since) for comment in comments: try: message = FoiMessage.objects.get(pk=comment.object_pk) if message.request_id not in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( (comment.submit_date, _("%(time)s: New comment by %(name)s") % { "time": tf.format(_(settings.TIME_FORMAT)), "name": comment.user_name }, comment.user_id)) except FoiMessage.DoesNotExist: pass if update_requester: requester_updates = defaultdict(dict) # send out update on comments to request users for req_id, request in requests.items(): if not request.user.is_active: continue if not request.user.email: continue if not updates[req_id]: continue if not any([x for x in updates[req_id] if x[2] != request.user_id]): continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) requester_updates[request.user][request] = { 'events': [x[1] for x in sorted_events] } for user, request_dict in requester_updates.items(): FoiRequest.send_update(request_dict, user=user) if update_follower: # update followers for event in FoiEvent.objects.filter( timestamp__gte=since).select_related("request"): if event.event_name in event_black_list: continue if event.request_id not in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( (event.timestamp, _("%(time)s: %(text)s") % { "time": tf.format(_("TIME_FORMAT")), "text": event.as_text() }, event.user_id)) # Send out update on comments and event to followers follower_updates = defaultdict(dict) for req_id, request in requests.items(): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) followers = FoiRequestFollower.objects.filter( request=request).select_related('user') for follower in followers: if follower.user is None and not follower.confirmed: continue if follower.user and (not follower.user.is_active or not follower.user.email): continue if not request.is_public(): continue if not any( [x for x in updates[req_id] if x[2] != follower.user_id]): continue users[follower.user_id] = follower.user ident = follower.user_id or follower.email follower_updates[ident][request] = { 'unfollow_link': follower.get_unfollow_link(), 'events': [x[1] for x in updates[req_id]] } for user_id, req_event_dict in follower_updates.items(): user = users.get(user_id) email = None if user is None: email = user_id FoiRequestFollower.send_update(req_event_dict, user=user, email=email)