Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ATLbeer
Sep 26, 2004
Über nerd

Stabby McDamage posted:

Is there an easy way to do user authentication against users on the server itself? ...within a certain group?

I could go with an LDAP setup for something I'm working on, but it would be much easier to auto-create and authenticate with the normal Linux user mechanism for users of a certain group. (The users are NIS authenticated, but there's a locally defined group for our team, which is not encoded in NIS or LDAP.)

This is what I use in production.

I pass authentication UP the chain to Apache and rely on Apache LDAP to do HTTP Digest authentication against whatever LDAP group you want to go against. Apache will simply just pass back an "authorized user" to Django and the app will just log that user in automatically. Here's a sample from my Django settings.py

code:
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.RemoteUserMiddleware',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.RemoteUserBackend',
)
and then relevant portions from httpd.conf from Apache
code:
LoadModule ldap_module         /opt/apache/modules/mod_ldap.so
LoadModule auth_basic_module         /opt/apache/modules/mod_auth_basic.so
LoadModule authnz_ldap_module         /opt/apache/modules/mod_authnz_ldap.so

<Location "/">         
        AuthType Basic
        AuthBasicProvider ldap
        AuthzLDAPAuthoritative on
        AuthName "[Use your NT login/password]"
        AuthLDAPURL ldap://ldap.yourdomain.com/DC=youroa,DC=com?sAMAccountName?sub?(objectClass=*)
        AuthLDAPBindDN "CN=auth_user,CN=users,DC=youroa,DC=com"
        AuthLDAPBindPassword auth_user_password
        Require valid-user
        Satisfy any
        Order allow,deny
</Location>

Adbot
ADBOT LOVES YOU

ATLbeer
Sep 26, 2004
Über nerd

king_kilr posted:

That's because that code won't work lower('profile__name') tries to use Django's ORM syntax in pure SQL.

Hmm.... Might be a good suggestion for Django to throw a exception Warning for using an ORM Syntax that isn't 100% compliant against all backends.

ATLbeer
Sep 26, 2004
Über nerd
I'm trying to use Django's built in serializer object since I've just been directly calling simplejson forever but, I'm running into some quirks.

I'm going to have a lot of views returning API information and am trying to follow from DRY concepts so I am writing a wrapper to handle the serialization and returning a proper HttpResponse. My problem is that I'm not always returning Django objects. Sometimes I'm just returning normal Python objects. The serializer will choke when it doesn't see Django's _meta tag.

Is there a work around here?

I'd like to use the Django serialization function since I'd like to support all the formats it supports (json, xml, yaml)


----

I'm feeling a little worn out right now so I'm probably explaining this horridly... So hopefully the code will speak for itself. Here is my current 'helper' module and an example implementation

code:
def serializer(obj, format="json"):
    """
        Return a serialized object in specified format        
        >>> test = {}
        >>> test['somevalue'] = "test value"
        >>> test['somearray'] = []
        >>> test['somearray'].append('first value')
        >>> test['somearray'].append('second value')
        >>> test['someint'] = 1
        >>> serializer(test)
        '{"somevalue": "test value", "someint": 1, "somearray": ["first value", "second value"]}'
    """    
    return json.dumps(obj)
    
def HttpServiceResponse(obj, format="json"):
    """
        Return an HttpResponse object with serialized object
        >>> test = {}
        >>> test['somevalue'] = "test value"
        >>> test['somearray'] = []
        >>> test['somearray'].append('first value')
        >>> test['somearray'].append('second value')
        >>> test['someint'] = 1
        >>> HttpServiceResponse(test) #doctest: +ELLIPSIS
        <django.http.HttpResponse object at 0x...>
        
    """
    return HttpResponse(serializer(obj, format))

def HttpServiceErrorResponse(error, format="json"):
    """
        Return an HttpResponse object with serialized error message
        >>> error = "this is a generic error message"
        >>> response = HttpServiceErrorResponse(error) 
        >>> response #doctest: +ELLIPSIS
        <django.http.HttpResponse object at 0x...>
        >>> response.content
        '{"error_message": "this is a generic error message", "error": true}'
    """
    error = {
                'error'         : True,
                'error_message' : error
            }
    return HttpServiceResponse(error, format)
And a related view

code:
def someview(request):
    if check_for_certain_things_that_should_be_right:
        return HttpServiceResponse(#a proper object)
    else:
        return HttpServiceErrorResponse("you must include the POST parameters'")
I'd like to be able to pass any object and any serializable type (json, yaml, xml) to the HttpServiceReponse function and get a proper response in return

ATLbeer fucked around with this message at 20:21 on Nov 19, 2009

ATLbeer
Sep 26, 2004
Über nerd
Just an FYI of something that I bumped into. Not specifically Django related but, you can run into it.


When a unicode character is passed into an ORM statement

example ("& #9829;" = ♥ but, vBul alters it below)
code:
some_user_input_from_form = "&#9829;"
YourModel.objects.filter(field = some_user_input_from_form)
An MySQLdb.OperationalError is thrown. That's not a problem until you try to catch it. MySQLdb.OperationalError is WAY too broad. It covers Collation errors (which the above is) and things like DB connect errors, DB memory errors, etc. So you can catch these collation errors but, you'll also silence a lot of other errors that you may want to handle.

Of course this only happens if your DB tables are not UTF-8 but, it looks like the default for MySQL might be latin1 so, unless you changed it before, you probably are running latin1.

The way we are catching it currently is by casting the user input into a string and catching the UnicodeEncodeError and handling that error instead of the OperationalError.

Just another FYI, never trust any user input, ever.

ATLbeer
Sep 26, 2004
Über nerd

bitprophet posted:

if you're lucky, you can introspect the exception's string value or other elements (depends on who wrote the exception and what they do with it) and derive additional meaning from that. Though I still find it nasty to have to hard-code that sort of comparison.

I tried that as a patch but, drat was it ugly. I also didn't like to be dependent upon the fact that the exception might not change but, there's never a guarantee that the text of the error in the exception might change in MySQLdb (which I was never a big fan of, I'm falling into the dislike category now)

It's less hacky than inspecting the exception information but, I think the solution of forcing the exception before possible reaching the OperationalError stage is a slightly better solution.

Has anyone used anything like ( https://launchpad.net/myconnpy )? A pure Python MySQL connector would be much nicer than the current MySQLdb connector.

ATLbeer
Sep 26, 2004
Über nerd
So.. The OP is WAY out of date by now and I was going to restart the thread with a brand new OP but, thought about all going web 2.0 twitter feedy with it..

I figured I should transfer the OP to a blog and completely rewrote it for Django >1.0

Here's what I have now: http://degizmo.com/series-django-tutorials/

Suggestions? Comments? Criticisms? Needs more Ponies?

ATLbeer
Sep 26, 2004
Über nerd

Mulozon Empuri posted:

Tests? And perhaps something about fancy stuff like virtualenv?

Virtualenv is probably out of scope in a basic tutorial but, tests... drat right, adding it to my to-do list

ATLbeer
Sep 26, 2004
Über nerd

king_kilr posted:

There's a better source than adrian: http://twitter.com/TheOnion/status/10921296161 :) If anyone has any questions about it I can probably answer them, I interned there last summer

I'd be curious on URL migration. Did they decide to just maintain their old URL structure from Drupal?

ATLbeer
Sep 26, 2004
Über nerd

Yay posted:

One would hope so; anything else would be barbarous (yes, even redirects). So sign me up as curious, too.

I probably should have expanded by asking if they were planning on staying with the same URL pattern in perpetuity for a while, are they just supporting old URLs in place with no desire to change, migrating the old URLs to the the new URLs with 302 redirects, etc?

ATLbeer
Sep 26, 2004
Über nerd
Random post on the internet speaking from the Onion Tech team's point of view. Looks legit but, no way to really verify I guess

king_kllr? Any opinion on the validity of their statements?

http://www.reddit.com/r/django/comments/bhvhz/the_onion_uses_django_and_why_it_matters_to_us/

ATLbeer
Sep 26, 2004
Über nerd

Lamacq posted:

How often does the list of countries and states change?

Tangentially realated: Google Maps have had a few problems, specifically with India : Kashmir, and China : Taiwan. They resolved it by showing different maps, with different boundaries and names in different geo-ip located regions.

ATLbeer
Sep 26, 2004
Über nerd
From the reddit onion thread

quote:

And the biggest performance boost of all: caching 404s and sending Cache-Control headers to the CDN on 404. Upwards of 66% of our server time is spent on serving 404s from spiders crawling invalid urls and from urls that exist out in the wild from 6-10 years ago. [Edit: We dropped our outgoing bandwidth by about 66% and our load average on our web server cluster by about 50% after implementing that change]

wow...

ATLbeer
Sep 26, 2004
Über nerd

Doh004 posted:

May I ask what environments you guys develop Django applications on? Right now, I have a WAMP installation on my desktop with mod_wsgi. It also does that thing where it reloads apache each time a page loads so I don't have to manually restart apache each time I make a change to the python.

Use the built in development server "python manage.py runserver"

Dev: OS X, sqlite, redis / ubuntu, sqlite, redis
Prod: REHL, apache mod_wsgi, MySQL, redis

All virtualenv, pip

ATLbeer
Sep 26, 2004
Über nerd

LuckySevens posted:

How can I set that up on a timer?

http://www.djangosnippets.org/snippets/374/ + cron

ATLbeer
Sep 26, 2004
Über nerd

LuckySevens posted:

Anyone know a good way to stream a movie of your desktop via a django page?

This isn't related to Django at all...

BUT, http://github.com/kanaka/noVNC

ATLbeer
Sep 26, 2004
Über nerd

Ferg posted:

So question about unit testing in Django (and maybe this is Python in general). I'm writing up unit tests for a module which manages a user's friend list. This involves functions to add friends, remove friends, get a list of the users friends, etc. Am I correct in my understanding that each unit test runs exclusively, so if I'm wanting to test retrieving a user's friends that I should also add friends in that same test?

I suppose this type of thing belongs in the setUp() function but it just feels redundant to add friends in the setUp() and then test adding friends in testAddFriends().

Might want to look at nose if you want some full automated unit testing

http://code.google.com/p/python-nose/
http://code.google.com/p/nose-django/

ATLbeer
Sep 26, 2004
Über nerd
For models, the only thing I test are additional methods or manager that are added. No reason to test the basics of the models themselves

Adbot
ADBOT LOVES YOU

ATLbeer
Sep 26, 2004
Über nerd

MonkeyMaker posted:

Has anyone implemented their own AdminSite? How easy/hard was the basic implementation?

I'm in the midst of building a "cms" that's going to be quite large and decently complex in some areas and I'm wondering if using something like AdminSite would save some time. I'd rather not have to make my own implementation for admin actions and inlines and the like (I know I can just use inline forms but that's not as simple as inlines = [Foo]).

When using a custom AdminSite, is further customization equivalent to customizing the standard Django admin?

Two questions

1) Have you looked into some of the existing django CMS options (including Ellington?)
2) Why reinvent the wheel. Just build, chop and modify the existing admin site to your needs. I can't imagine something you wouldn't be able to do within the existing admin system.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply