public_export: Add support for deleting export after access.

The RealmAuditLog object ID was stored in the event sent to the
deferred_work queue as a means to update the row's extra_data field.
The extra_data field then stores the location of the export.
This commit is contained in:
Wyatt Hoodes 2019-05-16 23:54:56 +01:00 committed by Tim Abbott
parent ed3e9be142
commit db69cdbcde
3 changed files with 14 additions and 6 deletions

View File

@ -32,6 +32,7 @@ class RealmExportTest(ZulipTestCase):
self.assertEqual(event['realm_id'], 1)
self.assertEqual(event['user_profile_id'], 5)
self.assertEqual(event['type'], 'realm_exported')
self.assertTrue(type(event['id']), int)
with patch('zerver.lib.export.do_export_realm',
side_effect=FileNotFoundError) as mock_export:
@ -57,6 +58,7 @@ class RealmExportTest(ZulipTestCase):
self.assertEqual(event['realm_id'], 1)
self.assertEqual(event['user_profile_id'], 5)
self.assertEqual(event['type'], 'realm_exported')
self.assertEqual(type(event['id']), int)
with patch('zerver.lib.export.do_export_realm',
side_effect=FileNotFoundError) as mock_export:

View File

@ -25,14 +25,15 @@ def public_only_realm_export(request: HttpRequest, user: UserProfile) -> HttpRes
if len(limit_check) >= time_delta_limit:
return json_error(_('Exceeded rate limit.'))
RealmAuditLog.objects.create(realm=realm,
event_type=event_type,
event_time=event_time)
row = RealmAuditLog.objects.create(realm=realm,
event_type=event_type,
event_time=event_time)
# Using the deferred_work queue processor to avoid
# killing the process after 60s.
# killing the process after 60s
event = {'type': event_type,
'time': event_time,
'realm_id': realm.id,
'user_profile_id': user.id}
'user_profile_id': user.id,
'id': row.id}
queue_json_publish('deferred_work', event)
return json_success()

View File

@ -43,7 +43,7 @@ from zerver.lib.db import reset_queries
from zerver.lib.redis_utils import get_redis_client
from zerver.context_processors import common_context
from zerver.lib.outgoing_webhook import do_rest_call, get_outgoing_webhook_service_handler
from zerver.models import get_bot_services
from zerver.models import get_bot_services, RealmAuditLog
from zulip_bots.lib import extract_query_without_mention
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler, EmbeddedBotQuitException
from zerver.lib.exceptions import RateLimited
@ -620,6 +620,11 @@ class DeferredWorker(QueueProcessingWorker):
delete_after_upload=True)
assert public_url is not None
# TODO: This enables support for delete after access, and needs to be tested.
export_event = RealmAuditLog.objects.get(id=event['id'])
export_event.extra_data = public_url
export_event.save(update_fields=['extra_data'])
# Send a private message notification letting the user who
# triggered the export know the export finished.
user_profile = get_user_profile_by_id(event['user_profile_id'])