Wednesday, November 30, 2011

Creating an app for recurring features

Hi,


I have made several projects now for internal use and some of the functionality keeps coming back.
For instance, language selection, user authentication, ad syncronization of users.

I want to put these in seperate apps that I can easily install in a project. All projects use virtualenv.
My first try is with the language stuff as this is only some settings and a view.
I've put the urls.py, views and so on in a seperate dir and then copied this dir in my current project.
Made a setup.py file to install the app with "python setup.py develop".

This works, and I can access the view from the project. So far so good.
I'm however not sure this is the right way to put some recurring functionality in a reusable app.
Some questions that arise:

- For the language selection view, I use small flags to make the language selection nicer.
These images are installed in the static directory of the application.
I then "installed" them in my project by including the correct dir in STATICFILES_DIRS
in the projec settings and then collected them with "python manage.py collectstatic --link"
I know I could have linked to the files but this seemed more Django like, yet, I'm not
sure this is the right way.

- I would like to be able to test the application. I thought that I could get away with a
minimal system (virtualenv + django) but it seems I need a rather complete settings.py
file and a typical project/app structure setup. I don't have that, so testing doesn't work.
How can I test the application code? Is there another way besides creating a full django project?

The settings file of the application (comments etc. left out)
=======================================
ugettext = lambda s: s
LANGUAGES = (
('nl', ugettext('Dutch')),
('fr', ugettext('French')),
('en', ugettext('English')),
)
DEFAULT_LANGUAGE = 1
LANGUAGE_CODE = 'nl'


USE_I18N = True
USE_L10N = True
=======================================

In my testproject, I made these changes to load the app succesfully.

1. I enabled- the LocaleMiddleware
2. I add the app to the INSTALLED_APPS
3. I load the language app settings file at the end of the projecs settings.py
This feels kind of hackish.
4. I add the path with the app static files to STATICFILES_DIRS
5. Add the app urls to the test project urls.

As I said, this is for local projects only, not meant to be distributed to the outside world.

Regards,
Benedict

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment