Quick question. In my syndication feed framework code,
http://docs.djangoproject.com/en/dev/ref/contrib/syndication/
what is the best way to get access to the session? I don't have
access to the request, and I can't use
from django.contrib.sessions.backends.db import SessionStore
as I don't know the session ID, but I need to access some of the
variables in the session.
i.e. I have:
from django.contrib.syndication.feeds import Feed
class LatestPhotos(Feed):
...
and in that LatestPhotos class, I need to access something in the session to help control the logic flow. I can't find any documentation on the best way to do it.
Thanks
Thanks!
It seems like a design flaw to be trying to access session data in the LatestPhoto's class. I would assume that if your syndication feed depended on a session variable, then the items you're syndicating (LatestPhotos) should be constructed with that variable?
Can you make the logic flow decision before you construct the LatestPhotos object, or at the very least pass the session ID in to the LatestPhotos init routine?
Figured it out - drrr, so simple. The syndication framework Feed class has a member called request...so simple I never thought of it :)
[this comment applies to django 1.1 and earlier syndication framework]
Related
I am working on a Django application that uses the SimpleGmail package to fetch mails from a Gmail inbox and I need to persist them. Normally I'd have written a model for the class, but given it's an external class, I can't figure out how to cleanly persist it.
I have come across solutions such as making it an attribute of a new model that is persisted or multiple inheritance of the desired class, but none of these seem correct to me.
How do I properly register an external class as a model for Django's persistence?
It is too long to comment. So I will try to write an answer.
One way is is to create a model class with properties which can be mapped from external class with all properties.
Another way would be just import external class in your application and create an instance of this external class. I am sorry, I am not Python guy, so code implementation would not be provided.
I'd like to save a WSGIRequest to a Django model to do some debugging. Is it possible to create a model to do this? I get all kinds of errors when I try:
class MyRequest(models.Model):
request = WSGIRequest()
Thanks in advance.
Why would you expect to be able to persist an object with all kinds of ephermal state to the database?
You best bet is probably to write a function that takes a Request object, pulls out whatever information is relevant to you and creates a string with the info in persist that.
Doing a json.dumps on the whole object work.
I have RESTFul API written in pyramid. View functions processes data in request.POST and request.matchdict and returns json response.
Eg: A method inside view class.
#view_config(route_name="temp_name", request_method="PUT")
def put_item(self):
# validates and processes self.request.POST
# validates and processes self.request.matchdict
# returns json reponse
As you can see, I'm doing validation inside view method, which I want to avoid.My intention is to separate validation from actual functionality.
How do I handle this?
I saw colander http://cornice.readthedocs.org/en/latest/validation.html#using-colander which looks really good in my case. But looks like it is integrated with cornice which I'm not using at all. And also, I can't convert whole app into cornice now. Is it possible to use colander in the same way as given in the above link with my app?
This is the first time I'm writing RESTFul API's, also just started learning pyramid and colander. Need your help. Thanks in advance.
You can use Colander independently of cornice. The most basic example for using Colander Schema in a pyramid application I remember you find here:
http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/zodb/index.html
This way you can encapsulate schema validation using colander schemas and validators.
A more recent introduction of pyramid 1.5 branch into that topic you find here:
http://pyramid.readthedocs.org/en/1.5-branch/quick_tutorial/forms.html
Oh, and look at that SO question. I liked it, may be it will be helpful to you as well:
Which one is the correct approach for form validation ? Colander's Schema validation or Deform's form validation?
I am trying to do some pre-processing at Django startup (I put a startup script that runs once in urls.py) and then use the created instance of an object in my views. How would I go about doing that?
Try to use the singleton design pattern.
You can use a Context Processor to add it to your template context.
If you want it in the View, rather than the Template, then you can either have a base View class that has this, or just import the reference into the module your view is in (and access it directly).
Be aware that each django thread may have a different copy of the object in memory, so this should really only be used for read-only access. If you make changes to it, you are likely to find yourself in a world of hurt.
I'm making a very simple website in Django. On one of the pages there is a vertical ticker box. I need to give the client a way to edit the contents of the ticker box as an HTMLField.
The first way that came to mind was to make a model Ticker which will have only one instance. Then I thought, instead of making sure manually that only one instance exists, perhaps there is (or there should be) something like a SingletonModel class in Django, which is like a normal model, except it makes sure no more than one instance gets created?
Or perhaps I should be solving my problem in a different way?
Try django-solo, it works in django 1.5 + for sure, django-singletons doesn't work with 1.5 + because it uses a deprecated feature.
You can use django_singletons. It has a built in admin support.
I think having a "singleton" model is ugly; it's dumb use of the relational database and it's bad UI, because the admin UI is built around working with lists of objects.
Instead I prefer to use a generic solution like django-chunks or django-flatblocks for this.
rewrite your save method so that every time a Ticker object gets saved it overwrites the existing one (if one exists).
A model with only one instance, a singleton, is sometime useful for things like global settings that you want to edit from the admin instead of having them in Django settings.py.
There are several third party applications that helps implementing singleton models and improve the admin interface for instance, django-solo, django-singleton-admin, django-singletons.