# Path: teamwork/shortcuts.py
# def get_object_or_404_or_403(perm_name, user, model_cls, **kwargs):
#     """
#     Wrapper for get_object_or_404 that also tests a permission and throws a
#     PermissionDenied if the user doesn't have the permission.
#     """
#     obj = get_object_or_404(model_cls, **kwargs)
#     parts = perm_name.split('.', 1)
#     if len(parts) == 1:
#         ct = ContentType.objects.get_for_model(obj)
#         perm_name = '%s.%s' % (ct.app_label, perm_name)
#     if not user.has_perm(perm_name, obj):
#         raise PermissionDenied
#     return obj
# 
# Path: teamwork_example/wiki/models.py
# class Document(models.Model):
#     name = models.CharField(max_length=80, unique=True)
#     content = models.TextField(blank=True, null=True)
#     site = models.ForeignKey(Site, blank=True, null=True)
#     team = models.ForeignKey(Team, blank=True, null=True)
#     parent = models.ForeignKey('self', blank=True, null=True)
#     creator = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
# 
#     objects = DocumentManager()
# 
#     class Meta:
#         permissions = (
#             ('view_document', 'Can view document'),
#             ('add_document_child', 'Can add child document'),
#             ('frob', 'Can frobulate documents'),
#             ('xyzzy', 'Can xyzzy documents'),
#             ('hello', 'Can hello documents'),
#             ('quux', 'Can quuxify documents'),
#         )
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         """Build the absolute URL to this document from its full path"""
#         return reverse('wiki.views.view', args=[self.name])
# 
#     def get_owner_user(self):
#         return self.creator
# 
#     def filter_permissions(self, user, permissions):
#         """Filter permissions with custom logic"""
#         if ('quux' in user.username):
#             if permissions is None:
#                 permissions = set()
#             permissions.add('wiki.quux')
#         return permissions
# 
#     def get_permission_parents(self):
#         """Build a list of parents from self to root"""
#         curr, parents = self, []
#         while curr.parent:
#             curr = curr.parent
#             parents.append(curr)
#         return parents
# 
#     def get_children(self):
#         return Document.objects.filter(parent=self).all()
# 
# Path: teamwork_example/wiki/forms.py
# class DocumentCreateForm(forms.ModelForm):
#     class Meta:
#         model = Document
#         fields = ['name', 'team', 'content',] 
# 
# class DocumentEditForm(forms.ModelForm):
#     class Meta:
#         model = Document
#         fields = ['content',] 


from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect
from django.core.exceptions import PermissionDenied
from teamwork.shortcuts import get_object_or_404_or_403
from teamwork_example.wiki.models import Document
from teamwork_example.wiki.forms import DocumentCreateForm, DocumentEditForm




def view(request, name):
    document = get_object_or_404_or_403('view_document', request.user,
                                        Document, name=name)

    perms = request.user.get_all_permissions(document)

    return render(request, 'wiki/view.html', dict(
        document=document, perms=perms
    ))


def create(request):
    """Create a new wiki document"""
    base_perms = request.user.get_all_permissions()
    if 'wiki.add_document' not in base_perms:
        raise PermissionDenied()

    parent_pk = request.GET.get('parent', None)
    if not parent_pk:
        parent = None
    else:
        parent = get_object_or_404_or_403(

# Path: teamwork/shortcuts.py
# def get_object_or_404_or_403(perm_name, user, model_cls, **kwargs):
#     """
#     Wrapper for get_object_or_404 that also tests a permission and throws a
#     PermissionDenied if the user doesn't have the permission.
#     """
#     obj = get_object_or_404(model_cls, **kwargs)
#     parts = perm_name.split('.', 1)
#     if len(parts) == 1:
#         ct = ContentType.objects.get_for_model(obj)
#         perm_name = '%s.%s' % (ct.app_label, perm_name)
#     if not user.has_perm(perm_name, obj):
#         raise PermissionDenied
#     return obj
# 
# Path: teamwork_example/wiki/models.py
# class Document(models.Model):
#     name = models.CharField(max_length=80, unique=True)
#     content = models.TextField(blank=True, null=True)
#     site = models.ForeignKey(Site, blank=True, null=True)
#     team = models.ForeignKey(Team, blank=True, null=True)
#     parent = models.ForeignKey('self', blank=True, null=True)
#     creator = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
# 
#     objects = DocumentManager()
# 
#     class Meta:
#         permissions = (
#             ('view_document', 'Can view document'),
#             ('add_document_child', 'Can add child document'),
#             ('frob', 'Can frobulate documents'),
#             ('xyzzy', 'Can xyzzy documents'),
#             ('hello', 'Can hello documents'),
#             ('quux', 'Can quuxify documents'),
#         )
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         """Build the absolute URL to this document from its full path"""
#         return reverse('wiki.views.view', args=[self.name])
# 
#     def get_owner_user(self):
#         return self.creator
# 
#     def filter_permissions(self, user, permissions):
#         """Filter permissions with custom logic"""
#         if ('quux' in user.username):
#             if permissions is None:
#                 permissions = set()
#             permissions.add('wiki.quux')
#         return permissions
# 
#     def get_permission_parents(self):
#         """Build a list of parents from self to root"""
#         curr, parents = self, []
#         while curr.parent:
#             curr = curr.parent
#             parents.append(curr)
#         return parents
# 
#     def get_children(self):
#         return Document.objects.filter(parent=self).all()
# 
# Path: teamwork_example/wiki/forms.py
# class DocumentCreateForm(forms.ModelForm):
#     class Meta:
#         model = Document
#         fields = ['name', 'team', 'content',] 
# 
# class DocumentEditForm(forms.ModelForm):
#     class Meta:
#         model = Document
#         fields = ['content',] 


# Path: teamwork_example/wiki/views.py
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect
from django.core.exceptions import PermissionDenied
from teamwork.shortcuts import get_object_or_404_or_403
from teamwork_example.wiki.models import Document
from teamwork_example.wiki.forms import DocumentCreateForm, DocumentEditForm





def view(request, name):
    document = get_object_or_404_or_403('view_document', request.user,
                                        Document, name=name)

    perms = request.user.get_all_permissions(document)

    return render(request, 'wiki/view.html', dict(
        document=document, perms=perms
    ))


def create(request):
    """Create a new wiki document"""
    base_perms = request.user.get_all_permissions()
    if 'wiki.add_document' not in base_perms:
        raise PermissionDenied()

    parent_pk = request.GET.get('parent', None)
    if not parent_pk:
        parent = None
    else:
        parent = get_object_or_404_or_403(

            'add_document_child', request.user, Document, pk=parent_pk)