Login

"Approved" field with timestamp

Author:
miracle2k
Posted:
July 17, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

I wanted to make the objects of a particular model approvable and store the timestamp of when that happened. In other frameworks/languages, I used to combined those in one "approved_at" field, which would be NULL if an object was currently unapproved.

I tried different approaches to implement this in django, and this is the best I came up with so far. Basically, the code in setattr makes sure that the field, once set, will not be updated again.

Overriding setattr__() could also be a solution to determining if a field value has changed in save(), a question that seems come up from time to time in #django.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class ApprovedStateField(forms.fields.Field):
    widget = forms.CheckboxInput

    def clean(self, value):
        super(ApprovedStateField, self).clean(value)
        if bool(value):
            return datetime.datetime.now()
        else:
            return None  

class Keyword(models.Model):
    name = models.CharField(maxlength=30)    
    approved_at = ApprovedStateField()
    
    def __setattr__(self, name, value):
        # don't change the approved_at attribute if it is already set
        if (name <> "approved_at") or (not hasattr(self, 'approved_at')) or (self.approved_at is None):
                super(Keyword, self).__setattr__(name, value)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

pjanderson (on July 17, 2007):

I'm working on a project where all user-contributed content needs to be approved.

What I came to realize is that this is not a trivial problem. If you model is, let's say, a blog Entry, and to keep things simple :), you decide to store Entry options (enable_comments, is_public, is_shared, is_published, etc) in the same model as Entry content, what will happen if the user decides to change some of the options or content after the object has been approved. How will you decide if the object should remain approved, or should be sent back for approval?

I'm not sure that setting a date as the Model field to denote approval state is the best solution. Perhaps there should be a separate table with options and approval state. Someone could even take it further by logging each approval state change.

I guess it depends on the needs of the program and the approval process, but you might soon realize that your approach has limitations, though it's probably sufficient for a quick and simple representation.

I haven't found the best solution yet, but these are issues I'm dealing with.

#

miracle2k (on July 17, 2007):

pjanderson: I know that depending on one's needs, this can be a complex issue.

However, in my case, I usually want to implement an option to withold a piece of content temporarily before it gets finally published (basically a simple draft machanism), and just use that opportunity to store a timestamp as well, only because it can't hurt ;)

There's usually only a limited group of trusted administrative users, so that's good enough for me most of the time.

#

pjanderson (on July 18, 2007):

Yeah, you're right. If that's the case, simple is probably good enough for you.

For me it's a little bit more complicated, as in an international site, I'll have to deal with more complex, region-based approval process.

My models has been evolving into a more complex solution to accommodate the needs of my clients, but I haven't yet found something that I'm truly happy with.

#

Please login first before commenting.