Conditions | 15 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 GradingTableView.get_context_data() 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 | ''' |
||
60 | def get_context_data(self, **kwargs): |
||
61 | course = self.object |
||
62 | assignments = course.assignments.all().order_by('title') |
||
63 | |||
64 | # find all gradings per author and assignment |
||
65 | author_submissions = {} |
||
66 | for assignment in assignments: |
||
67 | for submission in assignment.submissions.all().filter(state=Submission.CLOSED): |
||
68 | for author in submission.authors.all(): |
||
69 | # author_submissions is a dict mapping authors to another dict |
||
70 | # This second dict maps assignments to submissions (for this author) |
||
71 | # A tuple as dict key does not help here, since we want to iterate over the assignments later |
||
72 | if author not in list(author_submissions.keys()): |
||
73 | author_submissions[author] = {assignment.pk: submission} |
||
74 | else: |
||
75 | author_submissions[author][assignment.pk] = submission |
||
76 | resulttable = [] |
||
77 | for author, ass2sub in list(author_submissions.items()): |
||
78 | columns = [] |
||
79 | numpassed = 0 |
||
80 | numgraded = 0 |
||
81 | pointsum = 0 |
||
82 | columns.append(author.last_name if author.last_name else '') |
||
83 | columns.append(author.first_name if author.first_name else '') |
||
84 | columns.append( |
||
85 | author.profile.student_id if author.profile.student_id else '') |
||
86 | columns.append( |
||
87 | author.profile.study_program if author.profile.study_program else '') |
||
88 | # Process all assignments in the table order, once per author (loop above) |
||
89 | for assignment in assignments: |
||
90 | if assignment.pk in ass2sub: |
||
91 | # Ok, we have a submission for this author in this assignment |
||
92 | submission = ass2sub[assignment.pk] |
||
93 | if assignment.is_graded(): |
||
94 | # is graded, make part of statistics |
||
95 | numgraded += 1 |
||
96 | if submission.grading_means_passed(): |
||
97 | numpassed += 1 |
||
98 | try: |
||
99 | pointsum += int(str(submission.grading)) |
||
100 | except Exception: |
||
101 | pass |
||
102 | # considers both graded and ungraded assignments |
||
103 | columns.append(submission.grading_value_text()) |
||
104 | else: |
||
105 | # No submission for this author in this assignment |
||
106 | # This may or may not be bad, so we keep it neutral here |
||
107 | columns.append('-') |
||
108 | columns.append("%s / %s" % (numpassed, numgraded)) |
||
109 | columns.append("%u" % pointsum) |
||
110 | resulttable.append(columns) |
||
111 | |||
112 | context = super().get_context_data(**kwargs) |
||
113 | context['assignments'] = assignments |
||
114 | context['resulttable'] = sorted(resulttable) |
||
115 | return context |
||
116 | |||
176 |