Exemplo n.º 1
0
def _export_matter(matter, requested_by, provider=None):
    if provider is None:
        matter_export_service = MatterExportService(matter=matter, requested_by=requested_by)
        matter_export_service.process()

    if provider == 'box':
        management.call_command('sync_box', matter.slug, usernames=requested_by.username)

    if provider == 'dropbox-oauth2':
        management.call_command('sync_dropbox', matter.slug, usernames=requested_by.username)
Exemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.export_service = MatterExportService(matter=self.object, requested_by=request.user)

        created_at = dateutil.parser.parse(kwargs.get('created_at'))

        if request.user.pk != kwargs.get('user_pk'):
            #
            # user_id does not match, only matter.lawyer can download atm
            #
            logger.critical("%s tried accessing %s (%s) but is not allowed." % (request.user, self.object, created_at))
            return HttpResponseForbidden('You are not allowed to access this file.')

        if self.has_not_expired(created_at=created_at):

            # get the name of the zip file on the s3 storage device
            zip_filename = self.export_service.get_zip_filename(kwargs)

            if not self.storage.exists(zip_filename):
                #
                # File was not found
                #
                return HttpResponseNotFound('%s was not found on s3' % zip_filename)

            else:
                response = HttpResponse()
                response['Content-Disposition'] = 'attachment; filename=%s_%s.zip' % \
                                                  (kwargs.get('matter_slug'), created_at.strftime('%Y-%m-%d_%H-%M-%S'))
                response['Content-Type'] = 'application/zip'

                #
                # Open the file on s3 and write its contents out to the response
                #
                with self.storage.open(zip_filename, 'r') as exported_zipfile:
                    response.write(exported_zipfile.read())
                #
                # Record this event
                #
                USER_DOWNLOADED_EXPORTED_MATTER.send(sender=self, matter=self.object, user=request.user)

                return response

        logger.info("%s tried accessing %s (%s) but his link had expired." % (request.user, self.object, created_at))
        return HttpResponseForbidden('Your download link has expired.')
Exemplo n.º 3
0
class MatterDownloadExportView(DetailView):
    model = Workspace

    def dispatch(self, request, *args, **kwargs):
        #
        # take the passed in token and decode it, use the decoded parameters
        # to try to find and serve the exported zip file from s3
        #
        self.storage = _managed_S3BotoStorage()
        self.export_service = None

        token_data = signing.loads(kwargs.get('token'), salt=settings.URL_ENCODE_SECRET_KEY)

        kwargs.update(token_data)
        kwargs.update({'slug': token_data.get('matter_slug')})
        self.kwargs = kwargs

        return super(MatterDownloadExportView, self).dispatch(request, *args, **kwargs)

    def has_not_expired(self, created_at):
        return created_at + datetime.timedelta(days=MATTER_EXPORT_DAYS_VALID) > datetime.datetime.today()

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.export_service = MatterExportService(matter=self.object, requested_by=request.user)

        created_at = dateutil.parser.parse(kwargs.get('created_at'))

        if request.user.pk != kwargs.get('user_pk'):
            #
            # user_id does not match, only matter.lawyer can download atm
            #
            logger.critical("%s tried accessing %s (%s) but is not allowed." % (request.user, self.object, created_at))
            return HttpResponseForbidden('You are not allowed to access this file.')

        if self.has_not_expired(created_at=created_at):

            # get the name of the zip file on the s3 storage device
            zip_filename = self.export_service.get_zip_filename(kwargs)

            if not self.storage.exists(zip_filename):
                #
                # File was not found
                #
                return HttpResponseNotFound('%s was not found on s3' % zip_filename)

            else:
                response = HttpResponse()
                response['Content-Disposition'] = 'attachment; filename=%s_%s.zip' % \
                                                  (kwargs.get('matter_slug'), created_at.strftime('%Y-%m-%d_%H-%M-%S'))
                response['Content-Type'] = 'application/zip'

                #
                # Open the file on s3 and write its contents out to the response
                #
                with self.storage.open(zip_filename, 'r') as exported_zipfile:
                    response.write(exported_zipfile.read())
                #
                # Record this event
                #
                USER_DOWNLOADED_EXPORTED_MATTER.send(sender=self, matter=self.object, user=request.user)

                return response

        logger.info("%s tried accessing %s (%s) but his link had expired." % (request.user, self.object, created_at))
        return HttpResponseForbidden('Your download link has expired.')