| Conditions | 12 |
| Total Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like TeacherDashboard.init_with_context() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from grappelli.dashboard import modules, Dashboard |
||
| 14 | def init_with_context(self, context): |
||
| 15 | general = [] |
||
| 16 | if context.request.user.has_perm('opensubmit.change_course'): |
||
| 17 | general.append(['Manage courses', reverse('teacher:opensubmit_course_changelist'), False]) |
||
| 18 | if context.request.user.has_perm('opensubmit.change_gradingscheme'): |
||
| 19 | general.append(['Manage grading schemes', reverse('teacher:opensubmit_gradingscheme_changelist'), False]) |
||
| 20 | if context.request.user.has_perm('opensubmit.change_studyprogram'): |
||
| 21 | general.append(['Manage study programs', reverse('teacher:opensubmit_studyprogram_changelist'), False]) |
||
| 22 | if context.request.user.has_perm('opensubmit.change_user'): |
||
| 23 | general.append(['Manage users', reverse('admin:auth_user_changelist'), False]) |
||
| 24 | if context.request.user.has_perm('opensubmit.change_user'): |
||
| 25 | general.append(['Manage user groups', reverse('admin:auth_group_changelist'), False]) |
||
| 26 | if context.request.user.has_perm('opensubmit.change_permission'): |
||
| 27 | general.append(['Manage permissions', reverse('admin:auth_permission_changelist'), False]) |
||
| 28 | if context.request.user.has_perm('opensubmit.change_testmachine'): |
||
| 29 | general.append(['Manage test machines', reverse('teacher:opensubmit_testmachine_changelist'), False]) |
||
| 30 | self.children.append(modules.Group( |
||
| 31 | title="System", |
||
| 32 | column=1, |
||
| 33 | children=[ |
||
| 34 | modules.LinkList(title="Actions",children=(general)), |
||
| 35 | modules.DashboardModule(title="Info",pre_content= |
||
| 36 | '<table class="teacher_dashboard_info">'+ |
||
| 37 | '<tr><td>OpenSubmit release</td><td><a target="_new" href="http://docs.open-submit.org/en/latest/changelog.html">v{0}</a></td></tr>'.format(settings.VERSION) + |
||
| 38 | '<tr><td>Administrator</td><td><a href="mailto:%s">%s</a></td></tr>' % (settings.ADMINS[0][1], settings.ADMINS[0][0]) + |
||
| 39 | '<tr><td>Registered users</td><td>%u</td></tr>' % (User.objects.count()) + |
||
| 40 | '<tr><td>Submitted solutions</td><td>%u</td></tr>' % (Submission.objects.count()) + |
||
| 41 | '<tr><td>Test machines</td><td>%u enabled, %u disabled</td></tr>' % (TestMachine.objects.filter(enabled=True).count(), TestMachine.objects.filter(enabled=False).count()) + |
||
| 42 | "</table>" |
||
| 43 | ) |
||
| 44 | ] |
||
| 45 | )) |
||
| 46 | |||
| 47 | |||
| 48 | # Put course action boxes in column |
||
| 49 | try: |
||
| 50 | courses = context.request.user.profile.tutor_courses().all() |
||
| 51 | except: |
||
| 52 | courses = [] |
||
| 53 | |||
| 54 | column=2 |
||
| 55 | for course in courses: |
||
| 56 | # Prepare course-related links |
||
| 57 | links=[] |
||
| 58 | links.append(['Manage submissions',course.grading_url(), False]) |
||
| 59 | ass_url="%s?course__id__exact=%u"%( |
||
| 60 | reverse('teacher:opensubmit_assignment_changelist'), |
||
| 61 | course.pk |
||
| 62 | ) |
||
| 63 | if context.request.user.has_perm('opensubmit.change_assignment'): |
||
| 64 | links.append(['Manage assignments',ass_url, False]) |
||
| 65 | links.append(['Show grading table',reverse('gradingtable', args=[course.pk]), False]) |
||
| 66 | links.append(['Create eMail for students',reverse('mailcourse', args=[course.pk]), False]) |
||
| 67 | if context.request.user.has_perm('opensubmit.change_course'): |
||
| 68 | links.append(['Edit course details',reverse('teacher:opensubmit_course_change', args=[course.pk]), False]) |
||
| 69 | links.append(['Download course archive',reverse('coursearchive', args=[course.pk]), False]) |
||
| 70 | |||
| 71 | # Add course group box to dashboard |
||
| 72 | self.children.append(modules.Group( |
||
| 73 | title="Course '{0}'".format(course.title), |
||
| 74 | column=column, |
||
| 75 | children=[ |
||
| 76 | modules.LinkList(title="Actions",children=(links)), |
||
| 77 | modules.DashboardModule(title="Info",pre_content= |
||
| 78 | '<table class="teacher_dashboard_info">'+ |
||
| 79 | '<tr><td>Course URL for students</td><td>%s/?course=%u</td></tr>' % (settings.MAIN_URL, course.pk) + |
||
| 80 | '<tr><td>Course owner</td><td><a href="mailto:%s">%s</a></td></tr>' % (course.owner.email,course.owner.get_full_name()) + |
||
| 81 | "<tr><td>Open assignments</td><td>%u</td></tr>" % course.open_assignments().count() + |
||
| 82 | "<tr><td>Submissions to be graded</td><td>%u</td></tr>" % course.gradable_submissions().count() + |
||
| 83 | "<tr><td>Grading finished, not notified</td><td>%u</td></tr>" % course.graded_submissions().count() + |
||
| 84 | "<tr><td>Registered students</td><td>%u</td></tr>" % course.participants.count() + |
||
| 85 | "<tr><td>Authoring students</td><td>%u</td></tr>" % course.authors().count() + |
||
| 86 | "</table>" |
||
| 87 | ) |
||
| 88 | ] |
||
| 89 | )) |
||
| 90 | column+=1 |
||
| 91 | |||
| 92 |