[omaha] Django question

Jeff Hinrichs - DM&T jeffh at dundeemt.com
Fri Jun 15 14:46:32 CEST 2012


At the end of the request:
def load_deck(request):
   from anki.deck import DeckStorage
   deck = DeckStorage.Deck('media/test10.anki')   #at the end of the
request cycle I go out of scope
   return render(request, 'flfanki/load_deck.html', { 'deck': deck })

deck goes out of scope and goes away.  There would be nothing to close.
 Web interactions are state-less, think of the scenario where I pull up a
deck in your weblication on a windows pc, and IE causes the machine to
reboot.  Even if "deck" didn't go away at the end of the request, how would
you know when to close it manually?

If you want to track state, it is up to you.  You have to send stuff to the
client that it sends back with each request. (a.k.a cookies)  Django
sessions https://docs.djangoproject.com/en/dev/topics/http/sessions/  are
how you take care of state tracking.  You might want to look at making deck
a session variable.

Best,

Jeff

On Thu, Jun 14, 2012 at 12:32 PM, Shawn Hermans <shawnhermans at gmail.com>wrote:

> I am not sure, but serialization might help out in this instance.  The
> error might be caused by file objects not being able to be pickled by
> Python.  If you base64 encode the data, then this should be no problem,
> because it is just plain text.  Again, just a guess.
>
> On Tue, Jun 12, 2012 at 4:58 PM, Steve Young <wereapwhatwesow at gmail.com
> >wrote:
>
> > Thanks to everyone for the comments and suggestions!!
> >
> > Firstly, Mike
> >
> > > There may be a way around it (use Django for views and Anki db for
> > > database access)
> >
> >
> > This is what I am attempting.  More of a proof of concept, with
> converting
> > to Django models later (maybe)... I am using Django auth for users and
> > permissions.
> >
> > Jeff and Shawn -
> >
> > >  From the sounds of it you have a bunch of flash cards that are stored
> > in a
> > > single or  multiple SQLite databases.  You want to make these flash
> cards
> > > web accessible.  What I am not clear on is whether you want users to be
> > > able to customize flash cards or you want them to be able to use
> > > pre-canned flash card sets.
> >
> >
> > So 1 SQLite database per deck, and the ability to create and edit decks.
> >  Each user will have his own decks.  I am thinking about trying the
> > FileField approach to associate the users with their decks.  Encoding is
> > overkill at the moment, but I might try it, as it would be useful if I
> ever
> > deploy this publicly.
> >
> > BUT back to my original question about multi-user - the error I was
> getting
> > originally when trying to open 2 decks at once was a file in use error -
> I
> > was mistakenly trying to open the same deck/db file in each instance.
>  So I
> > corrected this, and am able to open 2 different decks/files at the same
> > time.
> >
> > So in the load_deck view, I create a deck and render it in the template:
> > def load_deck(request):
> >    from anki.deck import DeckStorage
> >    deck = DeckStorage.Deck('media/test10.anki')
> >    return render(request, 'flfanki/load_deck.html', { 'deck': deck })
> >
> > I now need to be able to have a link in the rendered template that says
> > something like - I am finished studying - and pulls up the close_deck
> view
> > and closes the deck.   But how do I pass the deck object into the view so
> > it knows which deck to close?  I tried adding the deck to request object:
> >    request.session['deckObject'] = deck
> > but get an error saying you can't pickle an instancemethod.
> >
> > Or if I implement the FileField approach, will that help with the issue?
> >
> > I am stuck once again...
> >
> > Steve
> >
> >
> > On Thu, Jun 7, 2012 at 7:52 PM, Shawn Hermans <shawnhermans at gmail.com
> > >wrote:
> >
> > > I am going to start off by saying I am probably guessing on what you
> are
> > > trying to do.  From the sounds of it you have a bunch of flash cards
> that
> > > are stored in a single or multiple SQLite databases.  You want to make
> > > these flash cards web accessible.  What I am not clear on is whether
> you
> > > want users to be able to customize flash cards or you want them to be
> > able
> > > to use pre-canned flash card sets.
> > >
> > > All that being said, one idea is to store the flash cards on disk and
> > use a
> > > Django filefield to access the stored database.  Below is a quick
> example
> > > of a Django model.  I am going off memory and so I wouldn't trust this
> > > code. Plus the indentation probably is off too.
> > >
> > > class FlashCard(models.Model):
> > >   sqlitedb = FileField()
> > >   name = CharField()
> > >   description = CharField()
> > >   user = ForeignKey(User)
> > >
> > > Alternatively, you could store the SQLite database as a binary object
> > > within your database. Here is a recipe for creating a blob field using
> > > base64 encoding
> > > http://djangosnippets.org/snippets/1597/.
> > >
> > > I am not saying these are the most elegant solutions out there, but
> they
> > > have a chance of working.  If that doesn't work, try duct tape. That
> > always
> > > seems to work for me.
> > >
> > > Cheers,
> > > Shawn
> > >
> > > I can only guess to the impacts to performance using this type of
> > approach.
> > > My guess is you would be okay as long as the SQLite databases are small
> > and
> > > change infrequently.  If they are relatively static, you should be able
> > to
> > > use Django's cache framework to cache the files and not need to hit the
> > DB
> > > or the file system.
> > >
> > >
> > >
> > > On Thu, Jun 7, 2012 at 4:30 PM, Mike Hostetler <mike at hostetlerhome.com
> > > >wrote:
> > >
> > > >
> > > > You don't have a choice -- you have to use Django models to access
> the
> > > > database.  There may be a way around it (use Django for views and
> Anki
> > db
> > > > for database access) but then you are using heavy-weight Django with
> a
> > > > by-pass. So you may have to deal with Django's problems and not reap
> > any
> > > > of it's benefits.
> > > >
> > > > Maybe try web.py or Flask. They are supposed to be lighter-weight.
> They
> > > > could handle the web portion and you could roll our own (or use
> Anki's)
> > > db
> > > > backend.
> > > >
> > > >
> > > >
> > > > Steve Young wrote:
> > > > > Hey Guys,
> > > > >
> > > > > I am trying to create a flashcard web app.  There is a python open
> > > source
> > > > > flashcard program called Anki that the author has made available,
> and
> > > has
> > > > > also conveniently separated the backend code. I would like to use
> > it's
> > > > > library code if possible.  It loads the flashcard decks, has spaced
> > > > > repetition, stats, and many other nice features. But it was created
> > > for a
> > > > > desktop app - not a web app with many users connecting at the same
> > time
> > > > > (although no deck will be accessed concurrently).
> > > > >
> > > > > I have been able to create a django project, load the anki code
> into
> > it
> > > > as
> > > > > app, and create a page loading a sample deck.  I am not sure the
> best
> > > way
> > > > > to proceed in making this work for multi-users, or even if this is
> a
> > > good
> > > > > idea.  Since I am not using django models (the anki code uses
> sqlite3
> > > > > db's,
> > > > > one for each deck) I am not sure how to proceed (all the books and
> > > docs I
> > > > > have studied have been using django models).
> > > > >
> > > > > Any ideas, tips, links??
> > > > >
> > > > > Thanks.
> > > > >
> > > > > Steve
> > > > > _______________________________________________
> > > > > Omaha Python Users Group mailing list
> > > > > Omaha at python.org
> > > > > http://mail.python.org/mailman/listinfo/omaha
> > > > > http://www.OmahaPython.org
> > > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > Omaha Python Users Group mailing list
> > > > Omaha at python.org
> > > > http://mail.python.org/mailman/listinfo/omaha
> > > > http://www.OmahaPython.org
> > > >
> > > _______________________________________________
> > > Omaha Python Users Group mailing list
> > > Omaha at python.org
> > > http://mail.python.org/mailman/listinfo/omaha
> > > http://www.OmahaPython.org
> > >
> > _______________________________________________
> > Omaha Python Users Group mailing list
> > Omaha at python.org
> > http://mail.python.org/mailman/listinfo/omaha
> > http://www.OmahaPython.org
> >
> _______________________________________________
> Omaha Python Users Group mailing list
> Omaha at python.org
> http://mail.python.org/mailman/listinfo/omaha
> http://www.OmahaPython.org
>



-- 
Best,

Jeff Hinrichs
402.218.1473


More information about the Omaha mailing list