Beispiel #1
0
def save_file_redirect(request,
                       app_label=[],
                       exclude=[],
                       filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    try:
        filename = '%s.%s' % (datetime.now().isoformat(),
                              settings.SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        if not isinstance(app_label, list):
            app_label = [app_label]
        response = serialize_to_response(app_label, exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        with open(
                os.path.join(dj_settings.BASE_DIR,
                             dj_settings.SMUGGLER_FIXTURE_DIR + '/' +
                             filename), 'w+') as file:
            file.write(response.content)
        messages.info(request, 'Backup done.')
        return HttpResponseRedirect('/admin')
    except CommandError as e:
        messages.error(
            request,
            _('An exception occurred while dumping data: %s') % force_text(e))
    return HttpResponseRedirect(request.build_absolute_uri().split('dump')[0])
Beispiel #2
0
def dump_selected(modeladmin, request, queryset):
    """Produce a smuggler dump for a selected set of model objects"""
    objects = queryset.all()
    filename = '%s-%s_%s.%s' % ('homesnippets', modeladmin.dump_name,
                                datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return serialize_to_response(objects, response)
Beispiel #3
0
def dump_selected(modeladmin, request, queryset):
    """Produce a smuggler dump for a selected set of model objects"""
    objects = queryset.all()
    filename = '%s-%s_%s.%s' % ('homesnippets', modeladmin.dump_name,
                                datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return serialize_to_response(objects, response)
Beispiel #4
0
def dump_model_data(request, app_label, model_label):
    """Exports data from a model.
    """
    model = get_model(app_label, model_label)
    objects = model._default_manager.all()
    filename = "%s-%s_%s.%s" % (app_label, model_label, datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response["Content-Disposition"] = "attachment; filename=%s" % filename
    return serialize_to_response(objects, response)
Beispiel #5
0
def dump_selected(modeladmin, request, queryset):
    objects = []
    for doc in queryset.all():
        rev = Revision.objects.get(id=doc.current_revision.id)
        doc.current_revision = None
        objects.append(doc)
        objects.append(rev)
    serializers.get_serializer("json")
    filename = "documents_%s.%s" % (datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response["Content-Disposition"] = "attachment; filename=%s" % filename
    return serialize_to_response(objects, response)
Beispiel #6
0
def dump_app_data(request, app_label):
    """Exports data from a application.
    """
    objects = []
    models = set(get_models(get_app(app_label))) - get_excluded_models_set()
    for model in models:
        if not model._meta.proxy:
            objects.extend(model._default_manager.all())
    filename = "%s_%s.%s" % (app_label, datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response["Content-Disposition"] = "attachment; filename=%s" % filename
    return serialize_to_response(objects, response)
Beispiel #7
0
def dump_to_response(request, app_label=None, exclude=[], filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    try:
        filename = '%s.%s' % (datetime.now().isoformat(), SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        response = serialize_to_response(app_label and [app_label] or [], exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        return response
    except CommandError, e:
        messages.add_message(request, messages.ERROR,
             _('An exception occurred while dumping data: %s' % unicode(e)))
Beispiel #8
0
def dump_to_response(request, app_label=None, exclude=[], filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    try:
        filename = '%s.%s' % (datetime.now().isoformat(), SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        response = serialize_to_response(app_label and [app_label] or [], exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        return response
    except CommandError, e:
        messages.add_message(request, messages.ERROR,
             _('An exception occurred while dumping data: %s' % unicode(e)))
Beispiel #9
0
def dump_data(request):
    """Exports data from whole project.
    """
    objects = []
    for app in get_apps():
        models = set(get_models(app)) - get_excluded_models_set()
        for model in models:
            if not model._meta.proxy:
                objects.extend(model._default_manager.all())
    filename = '%s.%s' % (datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return serialize_to_response(objects, response)
Beispiel #10
0
def dump_to_response(request, app_label=[], exclude=[], filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    try:
        filename = '%s.%s' % (datetime.now().isoformat(),
                              settings.SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        if not isinstance(app_label, list):
            app_label = [app_label]
        response = serialize_to_response(app_label, exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        return response
    except CommandError as e:
        messages.error(
            request,
            _('An exception occurred while dumping data: %s') % force_text(e))
    return HttpResponseRedirect(request.build_absolute_uri().split('dump')[0])
Beispiel #11
0
def dump_to_response(request, app_label=[], exclude=[], filename_prefix=None):
    """Utility function that dumps the given app/model to an HttpResponse.
    """
    try:
        filename = '%s.%s' % (datetime.now().isoformat(),
                              settings.SMUGGLER_FORMAT)
        if filename_prefix:
            filename = '%s_%s' % (filename_prefix, filename)
        if not isinstance(app_label, list):
            app_label = [app_label]
        response = serialize_to_response(app_label, exclude)
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        return response
    except CommandError as e:
        messages.error(
            request,
            _('An exception occurred while dumping data: %s') % force_text(e))
    return HttpResponseRedirect(request.build_absolute_uri().split('dump')[0])
Beispiel #12
0
def dump_selected_snippets(modeladmin, request, queryset):
    """Produce a smuggler dump for a selected set of snippets, along with
    associated client match rules."""
    snippets = queryset.all()

    # Assemble a unique set of client match rules used by selected snippets.
    rules = dict()
    for s in snippets:
        for rule in s.client_match_rules.all():
            rules[rule.pk] = rule

    # Combine set of rules and snippets for output
    objects = rules.values() + list(snippets)

    filename = '%s-%s_%s.%s' % ('homesnippets', 'snippets',
                                datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return serialize_to_response(objects, response)
Beispiel #13
0
def dump_selected_snippets(modeladmin, request, queryset):
    """Produce a smuggler dump for a selected set of snippets, along with
    associated client match rules."""
    snippets = queryset.all()

    # Assemble a unique set of client match rules used by selected snippets.
    rules = dict()
    for s in snippets:
        for rule in s.client_match_rules.all():
            rules[rule.pk] = rule

    # Combine set of rules and snippets for output
    objects = rules.values() + list(snippets)

    filename = '%s-%s_%s.%s' % ('homesnippets', 'snippets',
                                datetime.now().isoformat(), SMUGGLER_FORMAT)
    response = HttpResponse(mimetype="text/plain")
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return serialize_to_response(objects, response)
Beispiel #14
0
 def test_serialize_exclude(self):
     stream = StringIO.StringIO()
     utils.serialize_to_response(exclude=['sites'], response=stream)
     out = self.normalize(stream.getvalue())
     self.assertEquals(out, '[ %s ]' % self.FLATPAGE_DUMP)
Beispiel #15
0
 def test_serialize_exclude(self):
     stream = StringIO()
     utils.serialize_to_response(exclude=['sites', 'auth', 'contenttypes'],
                                 response=stream)
     out = self.normalize(stream.getvalue())
     self.assertEqual(out, self.PAGE_DUMP)
Beispiel #16
0
 def test_serialize_to_response(self):
     stream = StringIO.StringIO()
     utils.serialize_to_response(response=stream)
     out = self.normalize(stream.getvalue())
     self.assertEquals(out, self.BASIC_DUMP)
Beispiel #17
0
 def test_serialize_include(self):
     stream = StringIO()
     utils.serialize_to_response(app_labels=['sites'], response=stream)
     out = self.normalize(stream.getvalue())
     self.assertEqual(out, self.SITE_DUMP)