Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f76cd54e2e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 430 lines (378 sloc) 13.956 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
from django.db import models
from django.contrib.auth.models import User, Group
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _

from djtracker.utils import check_perms

class UserProfile(models.Model):
    """
Extension of User to allow for more detailed information about a
user
"""
    instant_messanger = models.CharField(
        _("instant messanger"),
        max_length=256,
        blank=True,
        null=True
    )
    user = models.OneToOneField(User, unique=True, verbose_name=_("user"))
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.user.username

    @models.permalink
    def get_absolute_url(self):
        return ("project_user", (), {'username': self.user.username})

def get_allowed_project_ids(request, user=None, permission='view'):
    """
returns a list of project ids which request.user is allowed to see
"""
    project_ids = []
    perm_index = ['view', 'edit', 'comment'].index(permission)
    # need to reference Project indirectly to avoid circular import reference
    for project in models.get_model('djtracker','project').objects.all():
        perms = check_perms(request, project, user)
        if perms[perm_index]:
            project_ids.append(project.id)
    return project_ids

class PermissionFilterManager(models.Manager):
    """
a manager which adds a special permission filter. usage:
in queries, replace
model_class.objects.all()
with
model_class.objects.filter_allowed(request)
"""
    def __init__(self, by='project__id__in', *args, **kwargs):
        """
``by`` determines on which attribute to filter (default: project)
"""
        self._by = by
        super(PermissionFilterManager, self).__init__(*args, **kwargs)
        
    def filter_allowed(self, request, permission='view'):
        project_ids = get_allowed_project_ids(request, permission)
        if self._by:
            return self.filter(**{self._by: project_ids})

class Project(models.Model):
    """
Projects are the central location of where everything comes together. An
issue is attached to a project, which has components, and so on and so
forth. Consider it a grouping mechanism for all the moving parts.
"""
    ## Project descriptions
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)
    description = models.TextField(_("description"), blank=True, null=True)
    git_repo_path = models.CharField(
        _("git repo path"),
        max_length=1024,
        blank=True,
        null=True
    )
    git_repo_commit = models.TextField(
        _("git repo commit"),
        max_length=512,
        blank=True,
        null=True
    )
    svn_repo_url = models.CharField(
        _("svn repo url"),
        max_length=1024,
        blank=True,
        null=True
    )
    svn_repo_commit = models.TextField(
        _("svn repo commit"),
        max_length=512,
        blank=True,
        null=True
    )
    svn_repo_username = models.CharField(
        _("svn repo username"),
        max_length=256,
        blank=True,
        null=True
    )
    svn_repo_password = models.TextField(
        _("svn repo password"),
        max_length=256,
        blank=True,
        null=True
    )

    ## Project level permissions
    allow_anon_viewing = models.BooleanField(_("allow anon viewing"), default=False)
    allow_anon_editing = models.BooleanField(_("allow anon editing"), default=False)
    allow_anon_comment = models.BooleanField(_("allow anon comment"), default=False)

    allow_authed_viewing = models.BooleanField(_("allow authed viewing"), default=False)
    allow_authed_editing = models.BooleanField(_("allow authed editing"), default=False)
    allow_authed_comment = models.BooleanField(_("allow authed comment"), default=False)

    groups_can_view = models.ManyToManyField(Group,
        blank=True,
        null=True,
        related_name='view_group_set',
        verbose_name=_("groups can view")
    )
    groups_can_edit = models.ManyToManyField(Group,
        blank=True,
        null=True,
        related_name='edit_group_set',
        verbose_name=_("groups can edit")
    )
    groups_can_comment = models.ManyToManyField(Group,
        blank=True,
        null=True,
        related_name='comment_group_set',
        verbose_name=_("groups can comment")
    )

    users_can_view = models.ManyToManyField(User,
        blank=True,
        null=True,
        related_name='view_user_set',
        verbose_name=_("users can view")
    )
    users_can_edit = models.ManyToManyField(User,
        blank=True,
        null=True,
        related_name='edit_user_set',
        verbose_name=_("users can edit")
    )
    users_can_comment = models.ManyToManyField(User,
        blank=True,
        null=True,
        related_name='comment_user_set',
        verbose_name=_("users can comment")
    )
    watched_by = models.ManyToManyField(UserProfile,
        blank=True,
        null=True,
        verbose_name=_("watched by")
        
    )

    active = models.BooleanField(_("active"), default=True)
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_index", (), {'project_slug': self.slug})

    objects = PermissionFilterManager(by='id__in')

class Milestone(models.Model):
    """
Goal to be reached
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)
    project = models.ForeignKey(Project, verbose_name=_("project"))
    description = models.TextField(_("description"), blank=True, null=True)
    goal_date = models.DateTimeField(_("goal date"), blank=True, null=True)
    active = models.BooleanField(_("active"), default=True)
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_milestone", (), {'project_slug': self.project.slug,
            'modi_slug': self.slug})

    objects = PermissionFilterManager()

class Component(models.Model):
    """
Components are items that are part of a project. For instance I have a
project called Cars. We then have tires that are part of cars
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)
    project = models.ForeignKey(Project, verbose_name=_("project"))
    active = models.BooleanField(_("active"), default=True)
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_component", (), {'project_slug': self.project.slug,
            'modi_slug': self.slug})

    objects = PermissionFilterManager()

class Version(models.Model):
    """
This will be FKed to projects. This will allow creation of issues
that point at specific versions
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)
    project = models.ForeignKey(Project, verbose_name=_("project"))
    active = models.BooleanField(_("active"), default=True)
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified_date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_version", (), {'project_slug': self.project.slug,
            'modi_slug': self.slug})

    objects = PermissionFilterManager()

class Status(models.Model):
    """
Status dictate what status an issue is in, such as open, closed, etc
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_status", (), {'project_slug': self.project.slug,
            'status_slug': self.slug})

class Priority(models.Model):
    """
Priority dictates the emphasis an issue carries. Urgent, critical, etc
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_priority", (), {'project_slug': self.project.slug,
            'priority_slug': self.slug})

    class Meta:
        verbose_name_plural = "Priorities"

class IssueType(models.Model):
    """
Bug, defect, feature request, etc
"""
    name = models.CharField(_("name"), max_length=256)
    slug = models.SlugField(_("slug"), unique=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_issue_type", (), {'project_slug': self.project.slug,
            'type_slug': self.slug})

class Issue(models.Model):
    """
This will be the root problem that is created. It will have a version,
component, and so on.
"""
    name = models.CharField(_("name"), max_length=256)
    project = models.ForeignKey(Project, verbose_name=_("project"))
    component = models.ForeignKey(Component,
        blank=True,
        null=True,
        verbose_name=_("component")
    )
    version = models.ForeignKey(Version,
        blank=True,
        null=True,
        verbose_name=_("version")
    )
    milestone = models.ForeignKey(Milestone,
        blank=True,
        null=True,
        verbose_name=_("milestone")
    )
    status = models.ForeignKey(Status,
        verbose_name=_("status")
    )
    priority = models.ForeignKey(Priority,
        verbose_name=_("priority")
    )
    issue_type = models.ForeignKey(IssueType,
        blank=True,
        null=True,
        verbose_name=_("issue type")
    )
    description = models.TextField(_("description"))
    created_by = models.ForeignKey(UserProfile,
        blank=True,
        null=True,
        related_name="issue_creator",
        verbose_name=_("created by"),
    )
    assigned_to = models.ForeignKey(UserProfile,
        blank=True,
        null=True,
        verbose_name=_("assigned to")
    )
    watched_by = models.ManyToManyField(UserProfile,
        blank=True,
        null=True,
        related_name="watched_set",
        verbose_name=_("watched by"),
    )
    active = models.BooleanField(_("active"), default=True)
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ("project_issue", (), {'project_slug': self.project.slug,
            'issue_id': self.id})

    objects = PermissionFilterManager()

class FileUpload(models.Model):
    """
File uploads will be FKed to specific issues. This will allow for
multiple files to be uploaded to a particular issue.
"""
    name = models.CharField(_("name"), max_length=256)
    file = models.FileField(_("file"), upload_to='attachments')
    issue = models.ForeignKey(Issue, verbose_name=_("issue"))
    created_date = models.DateTimeField(_("created date"), auto_now_add=True)
    modified_date = models.DateTimeField(_("modified date"), auto_now=True)

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
            return ("project_issue_file", (), {'project_slug': self.issue.project.slug,
                                               'file_id': self.id})

    objects = PermissionFilterManager(by='issue__project__id__in')

class IssueFilter(models.Model):
    """
This allows users to save their favorite view filters
"""
    project = models.ForeignKey(Project, verbose_name=_("project"))
    user = models.ForeignKey(UserProfile,
        blank=True,
        null=True,
        verbose_name=_("user")
    )
    name = models.CharField(_("name"), max_length=256)
    component = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("component"),
    )
    version = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("version")
    )
    milestone = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("milestone")
    )
    status = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("status")
    )
    type = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("type")
    )
    priority = models.CharField(max_length=256,
        blank=True,
        null=True,
        verbose_name=_("priority")
    )

    def __unicode__(self):
        return "Filter for %s: %s" % (self.user, self.name)

    def get_filtered_url(self):
        base_url = reverse("project_all_issues", kwargs={'project_slug': self.project.slug})
        get_vars = "?component=%s&version=%s&milestone=%s&status=%s&type=%s&priority=%s" % (
            self.component, self.version, self.milestone, self.status, self.type,
            self.priority)
        final_url = base_url + get_vars
        return final_url
Something went wrong with that request. Please try again.