Re: How to get current session user in models.py?
Hi, I actually have already sent this to Shawn, but thought it might be useful if I post to it a broader audience. Here is my solution to this problem: user = None outer_frames = inspect.getouterframes(inspect.currentframe()) for tpl in outer_frames: arginfo = inspect.getargvalues(tpl[0]) if(type(arginfo[3]) is dict): arg_dict = arginfo[3].copy() for k in arg_dict: if(type(arg_dict.get(k)) is WSGIRequest): user = arg_dict.get(k).user break del arg_dict del arginfo if(user): break This code is quite generic and should work in any place you ended up provided the origin was the django view/class method. I'm using it on models and customized admin forms for storing audit trails. It uses inspect to scan the parameters from the callers of the current frame stack. You can modify it to fish out pretty much anything.. there is no need to change the interfaces. Hope it helps. Cheers, Waldek -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/dzmUtcvpXrUJ. 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: How to get current session user in models.py?
@Andy: I agree it may be convoluted a bit indeed.. but your alternative a) is not an option - I would not do it if I did not needed it, wouldn't I ;-) b) requires interface changes, sometimes it's even messier.. changing the signatures means you need make sure all the callers would accept that change , c) local threading way posted by you is not as straight forward as it looks - I'd say it's very similar in complexity to mine.. you still need to explicitly call 'activate' somewhere(view method) to populate local thread storage while in my case you 'only' inspect the call stack - the change is limited to a single block of code.. All in all - I never claimed the way I solved it's perfect - it just works fine for me and wanted to share it with others. Thanks for your comment! I really appreciate it :-) Cheerio, Waldek On Jan 2, 7:49 pm, Andy McKay wrote: > Inspecting your stack to find the user seems a pretty convoluted way to do > it. > > You could either a) not access users in your models b) explicitly pass the > user around or c) just place your user into a local threading and retrieve > it anywhere else for > example:https://github.com/andymckay/arecibo/blob/master/listener/lib/usersto... > any other methods for doing that. -- 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: How to get current session user in models.py?
Indeed, it's clean and goes well with Django design principles.. However if I need to track just a few updates a day on a single model or admin form, the extra thousands or millions of calls generated by middleware(on every single request), it is really hard to chew.. at least one needs to be aware of this penalty. Otherwise really good point again! For a broader use I'd go for your solution, my usecase is a bit different tough. Cheers Andy! On Jan 4, 4:04 am, Andy McKay wrote: > On Mon, Jan 2, 2012 at 12:25 PM, Waldek Herka > wrote: > > > c) local threading way posted by you is not as straight > > forward as it looks - I'd say it's very similar in complexity to > > mine.. you still need to explicitly call 'activate' somewhere(view > > method) > > Middleware can do that just fine. -- 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: Correct folder layout for a big Django application
Hi, I'd recommend 'reviewboard' - code.google.com/p/reviewboard/ I learned great deal from this application. It is very mature and well thought through. Cheers, Waldek On Jan 4, 6:51 pm, IgorS wrote: > Can someone recommend a big existing Django application with the > "right" folder layout? > > I would like to learn from some big application that has multiple > model, test, view, template, .js, .css, image, utility, etc. files, > which are nicely organized in different folders positioned below the > application's main folder. > > Some people would recommend splitting the application into several > application, but this is not what is needed here. I would like to > learn how a big application should be properly structured (and see a > good example if it odes exist). > > Sorry if the question is too demanding... :-) > > Thank you, > -igor -- 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: Way to identify affected rows from a delete in the admin
Hi, here is the essence: from django.contrib.admin.util NestedObjects collector = NestedObjects(using=None) collector.collect(User.objects.all()) to_delete = collector.nested() It gives you a raw list of all the objects. For more friendly output see the usage of NestedObjects.nested with the callback. Best would be to have a look into admin sources: django\contrib\admin \util.py Cheers, Waldek On Jan 4, 5:37 pm, diafygi wrote: > In the django admin site, it lists the entries that will be deleted > via cascade if you want to delete something. I'm wondering if you can > do something similar in the shell to identify the rows affected by a > deletion. > > Is there such a function in django to show the list of casade deletes > like in the admin? If not, what can I do to create that list? > > This would be very helpful if you accidentally deleted an entry that > affected many rows (i.e. an auth User or something), so that you could > identify the affected rows from a backup, then re-insert them into the > database. > > Thanks, > Daniel -- 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: project root or its parent in pythonpath?
Hi, how about creating a proper package and installing it as an egg? That would be a proper way I think. See this tutorial: http://peak.telecommunity.com/DevCenter/setuptools It is only a matter of creating the setup.py with the list of the stuff you need to package, then you run(from the project dir): python setup.py build python setup.py install .. or even easy_install ./ Then, if you use PyDev or any othe IDE you have to remember to refresh the list of the locations/eggs if your list is strict(you don't need to if you rely on dirs only). Hope it helps. Cheers, Waldek On Jan 5, 11:19 pm, "d.w. harks" wrote: > On Thursday, January 05, 2012 2:21:30 PM, Demetrio Girardi wrote: > > > I have two apps which are inter-dependent and import stuff from one > > another; if I decide to go for the project.app route, is there a way > > to import > > things without a reference to the project name? otherwise the imports > > would > > have to be rewritten when the apps are installed in another projcet. > > If you have two apps that import from one another, then I'd suggest > merging them into a single app -- it indicates that the features of the > two are coupled and not useful without each other anyhow. > > That said, I would vote that it's better to add to your PYTHONPATH in > the IDE settings than it is to have the apps import stuff via an 'import > project.app'. > > -- > David W. Harks -- 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.