views:

38

answers:

3

My site allows users to post things on the site with an expiry date. Once the item has expired, it will no longer be displayed in the listings. Posts can also be closed, canceled, or completed. I think it would be be nicest just to be able to check for one attribute or status ("is active") rather than having to check for [is not expired, is not completed, is not closed, is not canceled]. Handling the rest of those is easy because I can just have one "status" field which is essentially an enum, but AFAIK, it's impossible to set the status to "expired" as soon as that time occurs. How do people typically handle this?

Edit: I'm not asking how to write a query to find expired items; I'm asking how I can find the "active" (unexpired items that meet a few other boolean conditions) without having to use a big nasty query every time I want to find them.

+1  A: 

Make the item have birth and death (type:date) columns and a status column (completed, removed, to be expired...).

Update/fill the death column when you want to logically end the lifecycle of an item (for whatever reason: expiry, completed, ...). Update the status column accordingly.

Querying for active items (in pseudo-SQL):

select * from mytable where birth <= todays_date <= death or death is null
ChristopheD
Yeah....querying for un-expired items is easy, that's not the problem. Are you suggesting I use a cronjob to update the status or what? Where/how would that occur?
Mark
Maybe I misunderstand the question but setting the `death` column to the `expiry_date` and the status to `to_be_expired` should do the trick, no?
ChristopheD
Yes, I think you misunderstand the question. Finding out whether or not the item is expired is easy. What I'm asking is how do I find all "active" items without having to use a big conditional mess every time I want to query for it? i.e., without having to check a half dozen statuses, plus a date range, every time I run this query.
Mark
A: 

It sounds like the expiration date/time is the field that you need to actually store and make decisions based off of. IsActive sounds like something you would calculate on the fly based on the expiration date and possibly other fields (even though its a pain)

IsActive as a field would probably work better if it wasn't a product of some other information like the expiration date, but was valid on its own, such as if a user manually set the status to "active" or "not active".

DutrowLLC
Yeah... but it *is* a product of a number of factors. I could make it a Python property, but that only works for individual items, I couldn't run a query for "active items" then.
Mark
+1  A: 

I think that can be managed with cronjob and django custom management command, is just an idea.

eos87
Oh...I've been doing this by manually setting up the Django environment from my python scripts, but I guess it makes more sense to stuff it into a `manage.py` command. Good suggestion!
Mark