I have a Model defined as below:
class ProblemVerificationModel(CreateUpdateDateModel):
problem = models.ForeignKey(Problem, related_name="problem_survey")
verifier = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="verified_problem")
is_problem_properly_formated = ... # BooleanField
is_this_done = ... # BooleanField
is_that_done = ... # BooleanField
# other BooleanFields
The above model serves like a voting system in my application. I need to calculate percentage of votes for each property such as is_problem_properly_formated, is_this_done, is_that_done etc. I am using classmethod for the same as defined below:
@classmethod
def percentage_problem_properly_formated(cls, problem):
votes = cls.objects.filter(problem=problem)
total_votes = votes.count()
yes_votes = votes.filter(is_problem_properly_formated=True).count()
percentage_yes = (yes_votes / total_votes) * 100
return percentage_yes
@classmethod
def percentage_is_this_done(cls, problem):
votes = cls.objects.filter(problem=problem)
total_votes = votes.count()
yes_votes = votes.filter(is_this_done=True).count()
percentage_yes = (yes_votes / total_votes) * 100
return percentage_yes
Now for each property I am using similar method with difference being parameter passed to filter method. This certainly is not a DRY way of doing things.
I just want a single method where I can pass the parameter to pass to filter method.
Can you help or provide hints to achieve the same results the DRY way?