This site is an archive; learn more about 8 years of OpenHatch.

[OH-Dev] views.py + controllers.py: refactoring thoughts requested

Nathan Yergler nathan at yergler.net
Sun Jan 8 06:35:00 UTC 2012


I haven't looked at the specific example you give, but here are some
general thoughts:

* Every project of a certain size is going to need some "workhorse"
tools. Some people call them "utils", some people call them
"controllers". I'm not a huge fan of "controller" because it's so
overloaded in other web frameworks that it makes it harder to explain
what it means for your project.
* In my experience passing around requests is usually a sign you don't
have a very well defined (or test[ed|able]) API; happy to hear you're
trying to avoid that.
* If the utility is model-related, it should obviously go in models
(and usually on the manager if it doesn't deal with a specific
instance).
* I really love class based views in Django, particularly once you
wrap your head around the mixins available
* I haven't yet done enough with them, however, to have an intuitive
feeling for how you test their members.

I'll look at the profile code tomorrow (probably evening) and see if
anything else comes to mind. I suppose a question for you is why are
you second-guessing what you've done already? Is it intuition, or are
you running into issues? Is get_geoip_guess_for_ip() an egregious
example for a particular reason?

Best,

NRY

On Sat, Jan 7, 2012 at 11:17 AM, Asheesh Laroia <asheesh at asheesh.org> wrote:
> Hi OH-Dev folks,
>
> Something that we've done in OpenHatch's codebase typically is separate some
> generic workhorse code from the stuff that users interact with. The idea is
> to make little pieces that are accessible from lots of parts of the code.
>
> I've called these little pieces "controllers", and the user-facing part
> "views". In general, I've tried to make it so that "controllers" don't take
> "request" objects, making them generally easier to test.
>
> I'm starting to second-guess this practice, and wanted tips from people who
> have read/written more Django code than I have.
>
> Sample "controllers" function:
>
> * get_geoip_guess_for_ip(), in mysite/profile/controllers.py
>
> What would be a good way to re-organize this?
>
> For some of these workhorse things, they're only actually used by one view.
> In those cases, we can move the code into a method on a view class, so long
> as the we switch that view to being class-based.
>
> Quite curious to know how to do things well,
>
> -- Asheesh.
>
> P.S. Nathan, I'm especially curious what you think.
> _______________________________________________
> Devel mailing list
> Devel at lists.openhatch.org
> http://lists.openhatch.org/mailman/listinfo/devel


More information about the Devel mailing list