# Path: folivora/models.py
# class Project(models.Model):
#     name = models.CharField(_('name'), max_length=255)
#     slug = models.SlugField(_('slug'), unique=True)
#     members = models.ManyToManyField(User, through=ProjectMember,
#           verbose_name=_('members'))
# 
#     class Meta:
#         verbose_name = _('project')
#         verbose_name_plural = _('projects')
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'folivora_project_detail', (), {'slug': self.slug}
# 
#     def create_logentry(self, type, action, user=None, **kwargs):
#         Log.objects.create(project=self, type=type,
#                            action=action, data=kwargs, user=user)
# 
#     @property
#     def requirements(self):
#         query = ProjectDependency.objects.filter(project=self) \
#                                          .select_related('package') \
#                                          .order_by('package__name')
#         return "\n".join([d.dependency_string for d in query])
# 
#     @property
#     def requirement_dict(self):
#         query = ProjectDependency.objects.filter(project=self) \
#                                          .select_related('package')
#         return dict((d.package.name, d.version) for d in query)
# 
#     def process_changes(self, user, remove=None, change=None, add=None):
#         log_entries = []
# 
#         remove = remove if remove else []
#         change = change if change else []
#         add = add if add else []
# 
#         for package_id, version in add:
#             log_entries.append(Log(type='project_dependency', action='add',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': version}))
#         for package_id, version in remove:
#             log_entries.append(Log(type='project_dependency', action='remove',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': version}))
#         for package_id, old_version, new_version in change:
#             log_entries.append(Log(type='project_dependency', action='update',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': new_version,
#                                          'old_version': old_version}))
# 
#         Log.objects.bulk_create(log_entries)
#         from .tasks import sync_project
#         # give the request time to finish before syncing
#         sync_project.apply_async(args=[self.pk], countdown=1)
# 
#     @property
#     def owners(self):
#         return self.members.filter(projectmember__state=ProjectMember.OWNER)
# 
# class ProjectMember(models.Model):
#     OWNER = 0
#     MEMBER = 1
#     STATE_CHOICES = (
#         (OWNER, _('Owner')),
#         (MEMBER, _('Member'))
#     )
# 
#     project = models.ForeignKey('Project', verbose_name=_('project'))
#     user = models.ForeignKey(User, verbose_name=_('user'))
#     state = models.IntegerField(_('state'), choices=STATE_CHOICES)
#     mail = models.EmailField(_('Email'), max_length=255, blank=True)
#     jabber = models.CharField(_('Jabber'), max_length=255, blank=True)
# 
#     class Meta:
#         verbose_name = _('project member')
#         verbose_name_plural = _('project members')
#         unique_together = ('project', 'user')


from django.http import HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from ..models import Project, ProjectMember
                qs = qs.order_by(*self.default_order)
                self.sort_field = self.default_order[0]
        self.sort_order = 'desc' if self.sort_field[0] == '-' else 'asc'
        self.sort_field = self.sort_field.strip('-')
        return qs


class ProjectMixin(object):
    """Mixin which checks if the user has access to the project in question.

    It is written for this project and won't work anywhere else ;)

    Access is granted by checking if the user is a member of a project, the
    project is taken from the slug in the URL.

    Additionally it checks if the user is authenticated.

    Set allow_only_owner True, to grant access only to owners

    Also polulates self.project and context['project']
    """
    allow_only_owner = False

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        slug = kwargs['slug']
        self.project = Project.objects.get(slug=slug)
        query = ProjectMember.objects.filter(
            user=request.user, project=self.project)
        if self.allow_only_owner:

# Path: folivora/models.py
# class Project(models.Model):
#     name = models.CharField(_('name'), max_length=255)
#     slug = models.SlugField(_('slug'), unique=True)
#     members = models.ManyToManyField(User, through=ProjectMember,
#           verbose_name=_('members'))
# 
#     class Meta:
#         verbose_name = _('project')
#         verbose_name_plural = _('projects')
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'folivora_project_detail', (), {'slug': self.slug}
# 
#     def create_logentry(self, type, action, user=None, **kwargs):
#         Log.objects.create(project=self, type=type,
#                            action=action, data=kwargs, user=user)
# 
#     @property
#     def requirements(self):
#         query = ProjectDependency.objects.filter(project=self) \
#                                          .select_related('package') \
#                                          .order_by('package__name')
#         return "\n".join([d.dependency_string for d in query])
# 
#     @property
#     def requirement_dict(self):
#         query = ProjectDependency.objects.filter(project=self) \
#                                          .select_related('package')
#         return dict((d.package.name, d.version) for d in query)
# 
#     def process_changes(self, user, remove=None, change=None, add=None):
#         log_entries = []
# 
#         remove = remove if remove else []
#         change = change if change else []
#         add = add if add else []
# 
#         for package_id, version in add:
#             log_entries.append(Log(type='project_dependency', action='add',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': version}))
#         for package_id, version in remove:
#             log_entries.append(Log(type='project_dependency', action='remove',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': version}))
#         for package_id, old_version, new_version in change:
#             log_entries.append(Log(type='project_dependency', action='update',
#                                    project_id=self.pk, package_id=package_id,
#                                    user=user, data={'version': new_version,
#                                          'old_version': old_version}))
# 
#         Log.objects.bulk_create(log_entries)
#         from .tasks import sync_project
#         # give the request time to finish before syncing
#         sync_project.apply_async(args=[self.pk], countdown=1)
# 
#     @property
#     def owners(self):
#         return self.members.filter(projectmember__state=ProjectMember.OWNER)
# 
# class ProjectMember(models.Model):
#     OWNER = 0
#     MEMBER = 1
#     STATE_CHOICES = (
#         (OWNER, _('Owner')),
#         (MEMBER, _('Member'))
#     )
# 
#     project = models.ForeignKey('Project', verbose_name=_('project'))
#     user = models.ForeignKey(User, verbose_name=_('user'))
#     state = models.IntegerField(_('state'), choices=STATE_CHOICES)
#     mail = models.EmailField(_('Email'), max_length=255, blank=True)
#     jabber = models.CharField(_('Jabber'), max_length=255, blank=True)
# 
#     class Meta:
#         verbose_name = _('project member')
#         verbose_name_plural = _('project members')
#         unique_together = ('project', 'user')


# Path: folivora/utils/views.py
from django.http import HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from ..models import Project, ProjectMember

                qs = qs.order_by(*self.default_order)
                self.sort_field = self.default_order[0]
        self.sort_order = 'desc' if self.sort_field[0] == '-' else 'asc'
        self.sort_field = self.sort_field.strip('-')
        return qs


class ProjectMixin(object):
    """Mixin which checks if the user has access to the project in question.

    It is written for this project and won't work anywhere else ;)

    Access is granted by checking if the user is a member of a project, the
    project is taken from the slug in the URL.

    Additionally it checks if the user is authenticated.

    Set allow_only_owner True, to grant access only to owners

    Also polulates self.project and context['project']
    """
    allow_only_owner = False

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        slug = kwargs['slug']
        self.project = Project.objects.get(slug=slug)
        query = ProjectMember.objects.filter(
            user=request.user, project=self.project)
        if self.allow_only_owner:

            query = query.filter(state=ProjectMember.OWNER)