Best practice for auth.User/UserProfile division?
I'm helping build a social networking site which makes heavy use of django built-in User model and its relationship to other Users on the site. We created a UserProfile per the django documentation [http:// docs.djangoproject.com/en/dev/topics/auth/#storing-additional- information-about-users] that links to a single User object. Hence, each time we make a database call involving a user, we retrieve the UserProfile model via request.user.get_profile(), which occurs quite frequently. This seems rather odd, considering the user is passed to our views by default, yet has no useful information. I'm no django expert, but my assumption is that this design decision was made to allow decoupled apps to work together. Since we're basically writing everything in- house (generic apps always have *just enough* discrepancies to what we want to do that we end up rewriting them), perhaps we're not reaping this benefit. My intuition tells me that we should have ignore django.contrib.auth.models.User and created our own to which everything else on our site relates. Thoughts? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Baffled by self.client.login(...) not working in unit tests
I forgot to mention: I'm using Django 1.0 on Ubuntu. On Apr 9, 10:41 pm, adambossy <adambo...@gmail.com> wrote: > I have created users for my unit tests in two ways: > > 1) Create a fixture for "auth.user" that looks roughly like this: > > { > "pk": 1, > "model": "auth.user", > "fields": { > "username": "homer", > "is_active": 1, > "password": > "sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2", > ... > } > } > > I've left out the seemingly unimportant parts. > > 2) Use 'create_user' in the setUp function (although I'd rather keep > everything in my fixtures class): > > def > setUp(self): > User.objects.create_user('homer', 'ho...@simpson.net', > 'simpson') > > Note that the password is simpson in both cases. > > I've verified that this info is correctly being loaded into the test > database time and time again. I can grab the User object using > User.objects.get. I can verify the password is correct using > 'check_password.' The user is active. > > Yet, invariably, self.client.login(username='homer', > password='simpson') FAILS. I'm baffled as to why. I think I've read > every single Internet discussion pertaining to this. Can anybody help? > My login code looks like this: > > login = self.client.login(username='homer', > password='simpson') > self.assertTrue(login) > > Thanks, > > Adam -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Baffled by self.client.login(...) not working in unit tests
I have created users for my unit tests in two ways: 1) Create a fixture for "auth.user" that looks roughly like this: { "pk": 1, "model": "auth.user", "fields": { "username": "homer", "is_active": 1, "password": "sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2", ... } } I've left out the seemingly unimportant parts. 2) Use 'create_user' in the setUp function (although I'd rather keep everything in my fixtures class): def setUp(self): User.objects.create_user('homer', 'ho...@simpson.net', 'simpson') Note that the password is simpson in both cases. I've verified that this info is correctly being loaded into the test database time and time again. I can grab the User object using User.objects.get. I can verify the password is correct using 'check_password.' The user is active. Yet, invariably, self.client.login(username='homer', password='simpson') FAILS. I'm baffled as to why. I think I've read every single Internet discussion pertaining to this. Can anybody help? My login code looks like this: login = self.client.login(username='homer', password='simpson') self.assertTrue(login) Thanks, Adam -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How do I properly unit test a Django session?
Thierry, Thanks for the response and the code sample. To be clear, the "variables" in accounts/accueil/ are session variables? I assume your view for that URL looks like this: def accueil(request): ... request.session['domaine'] = Domaine.objects.get(nom='DomaineAdmin') ... My problem is that I am trying to _write_ session variables before calling my view. For example: def view(request): if 'foo' not in request.session: return HttpResponseRedirect('failure') return HttpResponse('success') class SessionTestCase(TestCase): def test_view(self): self.client.session['foo'] = 'bar' self.client.get('view') self.assertEqual(self.client.status_code, 200) This fails because Django doesn't properly pass the session to 'view,' and I'm wondering what the best way is to ensure it's passed. On Mar 31, 12:42 am, Thierry Chich <thierry.ch...@gmail.com> wrote: > Le mercredi 31 mars 2010 05:40:43, adambossy a écrit : > > > > > The behavior of Django sessions changes between "standard" views code > > and test code, making it unclear how test code is written for > > sessions. Googling this yields two relevant discussions about this > > issue: > > > 1. "Easier manipulation of sessions by test client" [http:// > > code.djangoproject.com/ticket/10899] > > 2. "test.Client.session.save() raises error for anonymous > > users" [http://code.djangoproject.com/ticket/11475] > > > I'm confused because both tickets have different ways of dealing with > > this problem and they were both Accepted. I assume this means they > > were patched and the behavior is now different. I also don't know to > > which versions these patches would pertain. > > > If I'm writing a unit test in Django 1.0, how would I set up my > > session store for sessions to work as they do in the browser? > > Hi, > Perhaps, you should be more explicit with your problem. I am using the client > and there is no problem at all. > > For instance; > class GeneriqueTestCases(TestCase): > def test_login_admin(self): > > self.failUnlessEqual(self.client.login(username='admin',password='admin'),T > rue) > self.client.logout() > response=self.client.get("/accounts/accueil/") > > self.assertRedirects(response,'/accounts/login/?login=/accounts/accueil/') > > def test_login_domaine(self): > self.client.login(username='admin', password='admin') > response=self.client.get('/accounts/accueil/') > self.assertEqual(response.context['user'].username, 'admin') > d=Domaine.objects.get(nom='DomaineAdmin') > self.assertEqual(self.client.session.get('domaine'),d) > > In the last test, I am explicitely using informations stored in a session. > (However, I have searched a long time before I understand that some variables > was set in the accounts/accueil/ page :-( ) > > Thierry -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
How do I properly unit test a Django session?
The behavior of Django sessions changes between "standard" views code and test code, making it unclear how test code is written for sessions. Googling this yields two relevant discussions about this issue: 1. "Easier manipulation of sessions by test client" [http:// code.djangoproject.com/ticket/10899] 2. "test.Client.session.save() raises error for anonymous users" [http://code.djangoproject.com/ticket/11475] I'm confused because both tickets have different ways of dealing with this problem and they were both Accepted. I assume this means they were patched and the behavior is now different. I also don't know to which versions these patches would pertain. If I'm writing a unit test in Django 1.0, how would I set up my session store for sessions to work as they do in the browser? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Named urls and urlconf includes are incompatible
Bingo. The value should have been invitation_key and not invitation_key.key. Good catch! On Feb 8, 3:41 pm, Karen Tracey <kmtra...@gmail.com> wrote: > On Mon, Feb 8, 2010 at 6:24 PM, adambossy <adambo...@gmail.com> wrote: > > I'm trying to install the django-invitation app on bitbucket, but I'm > > running into an error I've been seeing repeatedly with urls. I've > > googled this and various permutations have occurred to various people, > > but not this one in particular. The general problem occurs when I try > > to include a url configuration: > > > ... > > urlpatterns += patterns('', > > (r'^accounts/', > > > include('invitation.urls')), > > ) > > ... > > > This is the included urls file, located in /myproject/invitation: > > >http://dpaste.com/156349/ > > > And the error I receive is a NoReverseMatch in my template: > > >http://dpaste.com/156350/ > > This does not appear to have anything to do with including url patterns, > it's a straightforward mismatch of the pattern and provided arguments. > > The {% url %} tag in the template is: > > {% url invitation_invited invitation_key=invitation_key.key %} > > The pattern for invitation_invited is: > > url(r'^invited/(?P\w+)/ > > The NoReverseMatch exception states: > > NoReverseMatch: Reverse for 'djroot.invitation_invited' with arguments '()' > and keyword arguments '{'invitation_key': ''}' not found. > > The invitation_key value is shown as an empty string. This is not allowed by > the pattern, which requires one or more alphanumeric characters (\w is > alphanumeric, + is one or more) in invitation_key. Therefore no reverse > match is found. > > Are you sure invitation_key.key is the correct value for invitation_key in > the {% url %} tag? > > Karen -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Named urls and urlconf includes are incompatible
I'm trying to install the django-invitation app on bitbucket, but I'm running into an error I've been seeing repeatedly with urls. I've googled this and various permutations have occurred to various people, but not this one in particular. The general problem occurs when I try to include a url configuration: ... urlpatterns += patterns('', (r'^accounts/', include('invitation.urls')), ) ... This is the included urls file, located in /myproject/invitation: http://dpaste.com/156349/ And the error I receive is a NoReverseMatch in my template: http://dpaste.com/156350/ If I open the shell and import django.core.urlresolvers.reverse, the error still occurs. Since "invitation_invite" (one of the other included urls) works, however, I don't believe it's due to stale pyc files. Any ideas? I'm running django v1.0. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Reversed URLs problem
Hey folks, thanks for the responses. Your questions alone helped me find the answer. Apparently, it was a problem with my apache2 configuration. I basically copied the directives from the django website, with one small modification: http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#basic-configuration I changed to so my Django project would be served at my site's root directory. I had to remove '/mysite' from the django.root option. My final output looks like this, with no value after django.root:" SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE mysite.settings PythonOption django.root PythonDebug On For the record, I am running: Django 1.0 Apache 2.2 mod_python 3.3.1 (i think... :)) Ubuntu Hardy 8.04.4 On Feb 7, 5:55 am, Karen Traceywrote: > On Sun, Feb 7, 2010 at 4:12 AM, Daniel Roseman wrote: > > > If all URLs work both with and without a prefix, it would seem that > > you've somehow duplicated your URL definitions somewhere. Could you > > post your entire urls.py? Preferably somewhere like dpaste.com. > > Also some information about deployment environment might be illuminating. > If not the dev server, what exactly, and what sort of config have you set > up for it? > > Karen -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reversed URLs problem
Hi folks, I'm having trouble with reversed urls when they are being called as template tags. I posted my question on Stack Overflow last week with no adequate responses. I'll copy it here (with slight modifications) for convenience. http://stackoverflow.com/questions/2189119/project-name-inserted-automatically-in-url-when-using-django-template-url-tag I have my urls named like so in my root urls.py file... ... url(r'^login/$', 'login', name='site_login'), ... This allows me to access /login at my site's root. I have my template tag defined like so... ... ... It works fine, except that Django automatically resolves that url as / myprojectname/login, not /login. Both urls are accessible. Only one is defined. Why is the projectname being inserted to the url automagically? This occurs for all url tags, not just this one. Thanks, Adam -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How do I make Django evaluate a ForeignKey for json serialization?
Russ, Thanks for the reply. Specifically, I am wondering if there is some feature of the Models API that allows me to retrieve the foreign key object so that it is included in the serialized string. That is, without writing my own serializer (which I suspect I may have to do). Alternatively, I was hoping somebody may be able to offer a technique for passing messages that I may have overlooked. With queries such as those in my example, I may be retrieving m Type objects, each of which may have n unique Class objects with foreign key pointers, leading to a whole mess of data to be passed in the json, making it a nightmare to deserialize it on the other end. Throughout my application, I am using SimpleJSON for some queries, and serializers.serialize for others. SimpleJSON is useful for python dictionaries and serializers.serialize is good for QuerySets. Ultimately, though, all the information I am passing is retrieved from QuerySets; my choice of serializer is based on whether I am iterating through the QuerySets and writing the data to python dictionaries (simplejson) or simply passing them directly to a response object (serializers.serialize). The reason for this is that the object vary in complexity and sizel for simple queries, I'll just pass the whole QuerySet to serialize() and for complex ones I'll break it down and create a dictionary to pass through simplejson. I suppose that my overall intent is a cry for help for a simpler, one- size-fits-all solution that can allow me to easily serialize objects, yet pass small messages. Perhaps a middle layer between Django and my front end (the Google Web Toolkit) is necessary. The fact that GWT only has basic JSON support doesn't help, as every message I pass requires a custom handler to decode the JSON. I'll keep tinkering... Let me know if you have any ideas. Adam On Dec 27, 4:45 am, "Russell Keith-Magee" <freakboy3...@gmail.com> wrote: > On Sat, Dec 27, 2008 at 2:11 PM, adambossy <adambo...@gmail.com> wrote: > > > I have a model that refers to a ForeignKey, e.g.: > > > class Template(models.Model): > > type = models.ForeignKey(Type) > > ... > > > class Type(models.Model) > > name = models.CharField(max_length=32) > > ... > > > I can fetch these objects fine using the model API. > > > t = Template.objects.get(id=1) > > print t.type > >>> > > print t.type.id > >>> 3 > > print t.type.name > >>> TemplateType3 > > > If I serialize these, I cannot get 'name.' I know Django lazily > > retrieves these, but even by iterating or calling t.type.name, they > > are unavailable during serialization. Is there any way to have Django > > evaluate the ForeignKey object so that I can retrieve the field, > > 'name?' I need fields to contain "name": "TemplateType3" (or whatever > > it may be)! :) > > > from django.core import serializers > > print serializers.serialize('json', Template.objects.get(id=1)) > > >>> { > > "pk": 1, > > "model": "template", > > "fields": { > > "type": 3 > > } > > }, > > It isn't entirely clear what you're trying to do. Do you want to > change the Django serializers so that they output: > > "type": "TemplateType3" > > If this is the case, then you will need to write your own serializer. > Django serializes objects in a specific way, optimized for > deserialization later on as fixtures. This output format won't suit > many applications. If you need a different format, you will need to > write a serializer that outputs in the format you require. Django > ships with SimpleJSON, so you can build serialized output from > primitives if need be. You could also use Django's serialization > library to write your own serializer that outputs in a format of your > choice. If your desired output format is similar to Django's default > format, you may find that you can use django/core/serializers/json.py > as a starting point for this implementation. > > Alternatively, are you trying to manipulate a deserialized object in > some way? If this is the case, you will need to provide more specific > details on what it is you are trying to achieve. > > Yours > Russ Magee %-) --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
How do I make Django evaluate a ForeignKey for json serialization?
I have a model that refers to a ForeignKey, e.g.: class Template(models.Model): type = models.ForeignKey(Type) ... class Type(models.Model) name = models.CharField(max_length=32) ... I can fetch these objects fine using the model API. t = Template.objects.get(id=1) print t.type >> print t.type.id >> 3 print t.type.name >> TemplateType3 If I serialize these, I cannot get 'name.' I know Django lazily retrieves these, but even by iterating or calling t.type.name, they are unavailable during serialization. Is there any way to have Django evaluate the ForeignKey object so that I can retrieve the field, 'name?' I need fields to contain "name": "TemplateType3" (or whatever it may be)! :) from django.core import serializers print serializers.serialize('json', Template.objects.get(id=1)) >> { "pk": 1, "model": "template", "fields": { "type": 3 } }, Thanks, Adam --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: I love both Django and the Google Web Toolkit. Who else thinks tighter integration would be nice?
Thank you! I'll look into it. I would have never found this on my own. On Dec 16, 11:31 am, "Ian Lawrence"wrote: > Hi > look athttp://autotest.kernel.org/ > this is a djand and gwt application used for kernel testing. It basically > sets up an RPC django server which passes json into gwt. > It is a great app to learn how this all works > > Ian > > --http://ianlawrence.info --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
I love both Django and the Google Web Toolkit. Who else thinks tighter integration would be nice?
I am a staunch advocate of the unconventional Google Web Toolkit (GWT) as a framework for dynamic Web application development. I also use Django, of course. In my current project, much of my time has been wasted devising how to transfer content to and from Django and GWT. The root of my problem is that I am trying to maintain synchronization between my Django ModelForm and what GWT is displaying. Being an idealist, I hate coding the same thing in two different places when it should be written once. I should be able to define a ModelForm and have GWT read it. This isn't a severe problem at the moment, but I imagine it will be as my project grows and becomes more complex. It seems that to adequately address my problem, I would have to write classes to substantially mimic those of ModelForm, BoundField, etc. I would also have to create some process to serialize them properly in Django. By extension, a lot of Django's functionality could be extended into GWT for tighter integration. Based on the silence of Stack Overflow, this is territory that has yet to be treaded on: http://stackoverflow.com/questions/369230/what-is-the-best-way-to-serialize-a-modelform-object-in-django My question to Django users is, would you find such code to be useful for this specific AJAX framework? What specific functionalities would you seek to be integrated? Forms, or something else? --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---