CourseAdmin   A
last analyzed

Size/Duplication

Total Lines 14
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A get_queryset() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# Course admin interface
2
import django.contrib.admin
3
from django.db.models import Q
4
from django.shortcuts import redirect
5
from django.core.urlresolvers import reverse
6
from django.utils.html import format_html
7
8
def assignments(course):
9
    return ",\n".join([str(ass) for ass in course.assignments.all()])
10
11
def actions(course):
12
    allow_tags=True
13
    result  = format_html('<a href="%s" style="white-space: nowrap">Download course archive</a><br/>'%reverse('coursearchive', args=(course.pk,)))
14
    result += format_html('<a href="%s" style="white-space: nowrap">Show grading table</a><br/>'%reverse('gradingtable', args=(course.pk,)))
15
    result += format_html('<a href="%s" style="white-space: nowrap">eMail to students</a>'%reverse('mailcourse', args=(course.pk,)))
16
    return result
17
18 View Code Duplication
class CourseAdmin(django.contrib.admin.ModelAdmin):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
    list_display = ['__str__', 'active', 'owner', assignments, actions]
20
    filter_horizontal = ['tutors']
21
22
    class Media:
23
        css = {'all': ('css/teacher.css',)}
24
25
    def get_queryset(self, request):
26
        ''' Restrict the listed courses for the current user.'''
27
        qs = super(CourseAdmin, self).get_queryset(request)
28
        if request.user.is_superuser:
29
            return qs
30
        else:
31
            return qs.filter(Q(tutors__pk=request.user.pk) | Q(owner=request.user)).distinct()
32