Beispiel #1
0
def _export_page(request):
    ctx = {}
    ctx.update(get_app_permissions(request))

    form = None
    if request.method == 'POST':
        form = DatabaseDumpForm(request.POST)
        if form.is_valid():
            print("Form submitted is valid!")
            if can_create_dump():
                new_obj = DatabaseDumpFile()
                new_obj.author = request.user
                new_obj.export_format = form.cleaned_data['format']
                new_obj.save()
                subprocess.Popen([python_executable(), "manage.py",
                              "create_database_dump",
                              str(request.user.pk), form.cleaned_data['format'], str(new_obj.pk) ])
                messages.add_message(request, messages.INFO, "Success! The system is now generating your export file. Please reload in a few seconds to update the list.")
            else:
                messages.add_message(request, messages.ERROR, "Cannot create file. You have already reached the limit of existing dump files. Please delete old unused exports and try again.")
    else:
        form = DatabaseDumpForm()

    # get all active DB Dump Files
    # if current existing DB Dump Files are less than the limit, enable form submission
    db_objs = DatabaseDumpFile.objects.filter(~Q(status='expired'))

    ctx['objects'] = db_objs
    if can_create_dump():
        ctx['enable_form'] = True

    ctx['form'] = form
    return render_to_resp(request=request, template_name="explorer/export_page.html", context=ctx)
Beispiel #2
0
def export_page(request):
    ctx = {}
    ctx.update(get_app_permissions(request))

    form = None
    if request.method == 'POST':
        form = DatabaseDumpForm(request.POST)
        if form.is_valid():
            print("Form submitted is valid!")
            if can_create_dump():
                new_obj = DatabaseDumpFile()
                new_obj.author = request.user
                new_obj.export_format = form.cleaned_data['format']
                new_obj.save()
                subprocess.Popen([python_executable(), "manage.py",
                              "create_database_dump",
                              str(request.user.pk), form.cleaned_data['format'], str(new_obj.pk) ])
                messages.add_message(request, messages.INFO, "Success! The system is now generating your export file. Please reload in a few seconds to update the list.")
            else:
                messages.add_message(request, messages.ERROR, "Cannot create file. You have already reached the limit of existing dump files. Please delete old unused exports and try again.")
    else:
        form = DatabaseDumpForm()

    # get all active DB Dump Files
    # if current existing DB Dump Files are less than the limit, enable form submission
    db_objs = DatabaseDumpFile.objects.filter(~Q(status='expired'))

    ctx['objects'] = db_objs
    if can_create_dump():
        ctx['enable_form'] = True

    ctx['form'] = form
    return render_to_response("explorer/export_page.html", ctx,
                                context_instance=RequestContext(request))
    def handle(self, *args, **options):
        import datetime
        import uuid
        from django.core.management import call_command
        from django.contrib.auth.models import User
        from tendenci.apps.emails.models import Email
        from tendenci.apps.explorer_extensions.models import DatabaseDumpFile, VALID_FORMAT_CHOICES

        dump_obj = None
        d_id = options.get('obj_id', None)
        if d_id:
            dump_obj = DatabaseDumpFile.objects.filter(pk=d_id)
            if dump_obj.exists():
                dump_obj = dump_obj[0]
            else:
                dump_obj = None

        if not dump_obj:
            dump_obj = DatabaseDumpFile()

        user_id = int(options['user_id'])

        if user_id == 0:
            msg = 'User ID is required. Usage: ./manage.py create_database_dump <user_id>'
            raise CommandError(msg)

        author = User.objects.filter(pk=user_id)
        if author.exists():
            author = author[0]
        else:
            author = None

        if not author:
            raise CommandError('Nonexistent user.')

        dump_obj.author = author

        fmt = options['export_format']

        if fmt not in VALID_FORMAT_CHOICES:
            raise CommandError('Format %s is not supported. Please use one of the following: %s' % (fmt, VALID_FORMAT_CHOICES))

        dump_obj.export_format = fmt
        dump_obj.save()

        print("Creating database dump...")

        content = ''
        dump_obj.dbfile.save(str(uuid.uuid4()), ContentFile(content))
        call_command('dumpdata', format=fmt, output=dump_obj.dbfile.path, exclude=['captcha.captchastore', 'files.multiplefile', 'events.standardregform', 'help_files', 'explorer_extensions'])

        dump_obj.status = "completed"
        dump_obj.end_dt = datetime.datetime.now() + datetime.timedelta(days=3)
        dump_obj.save()

        # File is created.
        # Send email to author
        context = { 'obj':dump_obj, 'author':dump_obj.author }
        email_subject = "Your database export (id:%d) is ready for download" % dump_obj.id
        email_body = render_to_string(template_name="explorer/dbdump_ready_email_body.html", context=context)
        email = Email(recipient=dump_obj.author.email, subject=email_subject, body=email_body)
        email.send()

        print("Done!")
    def handle(self, *args, **options):
        import datetime
        import uuid
        from StringIO import StringIO
        from django.core.management import call_command
        from django.core.files import File
        from django.contrib.auth.models import User
        from tendenci.apps.emails.models import Email
        from tendenci.apps.explorer_extensions.models import DatabaseDumpFile, VALID_FORMAT_CHOICES

        dump_obj = None
        d_id = options.get('obj_id', None)
        if d_id:
            dump_obj = DatabaseDumpFile.objects.filter(pk=d_id)
            if dump_obj.exists():
                dump_obj = dump_obj[0]
            else:
                dump_obj = None

        if not dump_obj:
            dump_obj = DatabaseDumpFile()

        user_id = int(options['user_id'])

        if user_id == 0:
            msg = 'User ID is required. Usage: ./manage.py create_database_dump <user_id>'
            raise CommandError(msg)

        author = User.objects.filter(pk=user_id)
        if author.exists():
            author = author[0]
        else:
            author = None

        if not author:
            raise CommandError('Nonexistent user.')

        dump_obj.author = author

        fmt = options['export_format']

        if fmt not in VALID_FORMAT_CHOICES:
            raise CommandError('Format %s is not supported. Please use one of the following: %s' % (fmt, VALID_FORMAT_CHOICES))

        dump_obj.export_format = fmt
        dump_obj.save()

        print("Creating database dump...")

        content = StringIO()
        call_command('dumpdata', format=fmt, stdout=content, exclude=['captcha.captchastore', 'files.multiplefile', 'events.standardregform', 'help_files', 'explorer_extensions'])

        dump_obj.dbfile.save(str(uuid.uuid1()), File(content))

        dump_obj.status = "completed"
        dump_obj.end_dt = datetime.datetime.now() + datetime.timedelta(days=3)
        dump_obj.save()

        # File is created.
        # Send email to author
        context = { 'obj':dump_obj, 'author':dump_obj.author }
        email_subject = "Your database export (id:%d) is ready for download" % dump_obj.id
        email_body = render_to_string("explorer/dbdump_ready_email_body.html", context)
        email = Email(recipient=dump_obj.author.email, subject=email_subject, body=email_body)
        email.send()

        print("Done!")