开发者

django check which users approved document

I have two models

class Document(models.Model):
    name = models.CharField(max_length=250, blank=True)

class Approval(models.Model):
    document = models.ForeignKey(Document)
    user = models.ForeignKey(User, related_name='approval user')
    status = models.IntegerField(choices=APPROVELS, default=1, help_text="Approved")

i want to list all document with current login user approval status, when a user approve/Abstain/Not Approved a document i am recording in Approval table otherwise there is no record about current 开发者_C百科user approval status in Approval table

Please help me with the view and template.


On view:

userApprovals = Approval.objects.filter( user = request.user )
or
userApprovals = request.user.approval_user_set.all()
forYourApproval = Document.objects.exclude( pk__in = [ a.document.pk for a in userApprovals ] )

and don't forget to include userApprovals on render_to_response:

return render_to_response(
                  "yourPage.html", 
                  {
                   "userApprovals": userApprovals,
                   "forYourApproval": forYourApproval,
                   },
                  context_instance=RequestContext(request))

On template:

{% for approval in userApprovals %}
   {{ approval.document.name }} status {{ approval.get_status_display }}
{% endfor %}

{% for document in forYourApproval %}
   {{ document.name }} waiting for your approval.
{% endfor %}

Note: change related name to 'approval_user'.


It's not clear whether your requirements allow any user to approve a document, but I'm inferring that this is the case based on your models. If a Document can only be approved by a specific user, or set of users, or if a document is approved by multiple users, you will need to make some substantial changes to your model design.

If not, I would write a view like this:

from django.db.models import Q

def approval_view(request):
    documents = Document.objects.filter(
         Q(approval__id__isnull=True) | Q(approval__user=request.user))
    return render_to_response(template_name, {'documents': documents})

This will return a context with documents that have no Approval record in the approval table OR who have been approved by request.user.

You will likely need some additional code to display the appropriate information for each document in documents. For example, a custom template filter that displays a document status ("For your Approval" or "Approved" or whatever) might be necessary. An example template fragment:

{% for document in documents %}
    <li>{{ document.name }} - {{ document|approval_status }}</li> 
{% endfor %}

As I said at the beginning, your requirements are not clear. Is every user to be given an opportunity to approve a document? Or do all documents get a single opportunity to be approved by any user? There is a big difference and this code reflects the latter assumption.

If every user is to be given an opportunity to approve a document and you need to display the current user's approval decision, then the above code will work with slight modification. I would probably modify the view to display all documents in some order:

documents = Document.objects.all()

And then use a custom template filter as above to display the approval status for the current user (passing in the user as an argument):

<li>{{ document.name }} - {{ document|approval_status:request.user }}</li>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜