Code Monkey home page Code Monkey logo

django-fixturemedia's Introduction

django-fixture-media

Build Status

Renamed 11/21/2013

Simple tool to manage media files for fixtures. Contributions welcome.

Put relative paths in your fixtures, put your media files in a media directory in you fixtures directory.

Run collectmedia and your fixture media will be copied to MEDIA_ROOT.

YMMV

Only supports JSON fixtures.

Installation

Using pip:

pip install django-fixture-media

Add 'fixture_media' to your installed apps.

Manual install:

Download the source code and run python setup.py install within the directory

Add 'fixture_media' to your installed apps.

Usage

  1. Add relative paths to your fixtures for file / image fields.

     [
         {
             "pk": 1,
             "model": "myapp.mymodel",
             "fields": {
                 "name": "Awesome Sauce",
                 "image": "images/awesome-sauce.jpg"
             }
         }
     ]
    
  2. Add a media directory in your fixtures directory. Add the matching child directories to match the path in the fixture and place your media file in the appropriate location.

     myapp/
         models.py
         fixtures/
             initial_data.json
             media/
                 images/
                     awesome-sauce.jpg
    
  3. Run django-admin.py collectmedia or manage.py collectmedia

     ./manage.py collectmedia
     This will overwrite any existing files. Proceed? yes
     Copied images/awesome-sauce.jpg to /tmp/django_media/images/awesome-sauce.jpg
    

CONFIGURATION

  • FIXTURE_MEDIA_REQUIRE_PREFIX (default: False)

    By default, the management command will try to copy everything that resembles a file path; if you have a lot of entries in your fixtures that looks like file paths but is not actually one, you can specify FIXTURE_MEDIA_REQUIRE_PREFIX = True in settings.py to make ./manage.py collectmedia copy only files that are prefixed with media://.

    Example:

      [
          {
              "pk": 1,
              "model": "myapp.mymodel",
              "fields": {
                  "name": "Awesome Sauce",
                  "image": "media://images/awesome-sauce.jpg"
                  "path": "this/is/ignored/awesome-sauce.jpg"
              }
          }
      ]
    

Thanks

lieryan swiharta

TODO

  • Add tests.
  • Convert to better command modeled after the excellent staticfiles app.

django-fixturemedia's People

Contributors

leetrout avatar lieryan avatar mrtable avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-fixturemedia's Issues

ImportError: cannot import name 'NoArgsCommand'

This is an error in newer version of Django (1.10.3). I think this is all that needs done in a few places:

try:
    from django.core.management.base import CommandError, NoArgsCommand
except ImportError:
    from django.core.management import BaseCommand as NoArgsCommand
    from django.core.management import CommandError

Revert FIXTURE_MEDIA_REQUIRE_PREFIX

I feel kinda responsible for this; the FIXTURE_MEDIA_REQUIRE_PREFIX option doesn't actually work. Well, it does what it says, but then when you do ./manage.py loaddata, it will load these file fields with the extra prefixes which obviously doesn't work with the rest of Django. It kinda misses the whole point of the collectmedia and loaddata command.

What will be needed to fix it is a way to extend loaddata command so that it strips the prefix this looks quite difficult cleanly. Either that or wrap loaddata so that it generates temporary fixture files with the media prefix stripped which is then passed to the original loaddata. That also looks quite painful to do.

For now, perhaps the best option is to revert the entire changes and rethink of a different way to denote what is and isn't to be collected by collectmedia.

copies images to MEDIA_ROOT from settings instead of local_settings

In my app, the settings file imports MEDIA_ROOT from local_settings, so that uploading files will be saved to the MEDIA_ROOT specified in local_settings, not in settings. However, when I run 'python manage.py collectmedia' using django-fixturemedia, the files are copied to the MEDIA_ROOT specified in settings instead of local_settings. Is there a way around this?

Regex doesn't seem to work on Windows

The regex string you have for matching file paths in JSON files...

 file_patt = re.compile(r'"([^"]+?\/[^"]+?\.[^."]+?)"')

...isn't working for me on Windows. My paths in JSON look like this:

"original": "media\\images\\408.jpg", 

If I figure out a compatible regex string, I'll pass it along. For now, it's easiest to replace all the \\ with /. Thanks for the program.

P.S. I also modified it so you can at least install from Github via pip (added piece of junk setup.py, couple lines).

can't match media inside html

Hi,
there's issue with matching patterns,
if I have html in fixture then I can't match media
eg.
{
"fix1" : "

"
}

can you fix that?

eg
(?P['"])(?:media://)(?P.*?)(?P=quote)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.