Refactored MotionPoll.

Use new getVotes function for vote value and percent value.
Remove 'to_representation' function in motion serializer.py to get
special values in numbers (-1,-2) instead of translated strings.
This commit is contained in:
Emanuel Schuetze 2016-02-08 23:25:42 +01:00
parent 25a7f59b70
commit 31102bd9d7
3 changed files with 68 additions and 116 deletions

View File

@ -116,21 +116,6 @@ class MotionPollSerializer(ModelSerializer):
'votes', 'votes',
'has_votes') 'has_votes')
def to_representation(self, obj):
"""
Overrides the output of this serializer. Replaces vote values -1
through the translated string 'majority' and -2 through the
translated string 'undocumented'.
"""
result = super().to_representation(obj)
for key in result:
if key in ('yes', 'no', 'abstain', 'votesvalid', 'votesinvalid', 'votescast'):
if result[key] == -1:
result[key] = _('majority')
elif result[key] == -2:
result[key] = _('undocumented')
return result
def get_yes(self, obj): def get_yes(self, obj):
try: try:
result = obj.get_votes().get(value='Yes').weight result = obj.get_votes().get(value='Yes').weight

View File

@ -52,8 +52,9 @@ angular.module('OpenSlidesApp.motions', ['OpenSlidesApp.users'])
.factory('MotionPoll', [ .factory('MotionPoll', [
'DS', 'DS',
'gettextCatalog',
'Config', 'Config',
function (DS, Config) { function (DS, gettextCatalog, Config) {
return DS.defineResource({ return DS.defineResource({
name: 'motions/motionpoll', name: 'motions/motionpoll',
relations: { relations: {
@ -65,92 +66,40 @@ angular.module('OpenSlidesApp.motions', ['OpenSlidesApp.users'])
} }
}, },
methods: { methods: {
getYesPercent: function (valueOnly) { // returns object with value and percent
getVote: function (vote) {
if (!this.has_votes || !vote) {
return;
}
var value = '';
switch (vote) {
case -1:
value = gettextCatalog.getString('majority');
break;
case -2:
value = gettextCatalog.getString('undocumented');
break;
default:
value = vote;
break;
}
// calculate percent value
var config = Config.get('motions_poll_100_percent_base').value; var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue; var percentStr, percentNumber;
if (config == "WITHOUT_INVALID" && this.votesvalid > 0 && this.yes >= 0) { if (config == "WITHOUT_INVALID" && this.votesvalid > 0 && vote >= 0) {
returnvalue = Math.round(this.yes * 100 / this.votesvalid * 10) / 10; percentNumber = Math.round(vote * 100 / this.votesvalid * 10) / 10;
} else if (config == "WITH_INVALID" && this.votescast > 0 && this.yes >= 0) { } else if (config == "WITH_INVALID" && this.votescast > 0 && vote >= 0) {
returnvalue = Math.round(this.yes * 100 / (this.votescast) * 10) / 10; percentNumber = Math.round(vote * 100 / (this.votescast) * 10) / 10;
} else {
returnvalue = null;
} }
if (!valueOnly && returnvalue != null) { if (percentNumber) {
returnvalue = "(" + returnvalue + "%)"; percentStr = "(" + percentNumber + "%)";
} }
return returnvalue; return {
'value': value,
'percentStr': percentStr,
'percentNumber': percentNumber
};
}, },
getNoPercent: function (valueOnly) {
var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue;
if (config == "WITHOUT_INVALID" && this.votesvalid > 0 && this.no >= 0) {
returnvalue = Math.round(this.no * 100 / this.votesvalid * 10) / 10;
} else if (config == "WITH_INVALID" && this.votescast > 0 && this.no >= 0) {
returnvalue = Math.round(this.no * 100 / (this.votescast) * 10) / 10;
} else {
returnvalue = null;
}
if (!valueOnly && returnvalue != null) {
returnvalue = "(" + returnvalue + "%)";
}
return returnvalue;
},
getAbstainPercent: function (valueOnly) {
var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue;
if (config == "WITHOUT_INVALID" && this.votesvalid > 0 && this.abstain >= 0) {
returnvalue = Math.round(this.abstain * 100 / this.votesvalid * 10) / 10;
} else if (config == "WITH_INVALID" && this.votescast > 0 && this.abstain >= 0) {
returnvalue = Math.round(this.abstain * 100 / (this.votescast) * 10) / 10;
} else {
returnvalue = null;
}
if (!valueOnly && returnvalue != null) {
returnvalue = "(" + returnvalue + "%)";
}
return returnvalue;
},
getVotesValidPercent: function (valueOnly) {
var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue;
if (config == "WITHOUT_INVALID" && this.votevalid >= 0) {
returnvalue = 100;
} else if (config == "WITH_INVALID" && this.votevalid >= 0) {
returnvalue = Math.round(this.votesvalid * 100 / (this.votescast) * 10) / 10;
} else {
returnvalue = null;
}
if (!valueOnly && returnvalue != null) {
returnvalue = "(" + returnvalue + "%)";
}
return returnvalue;
},
getVotesInvalidPercent: function (valueOnly) {
var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue;
if (config == "WITH_INVALID" && this.voteinvalid >= 0) {
returnvalue = Math.round(this.votesinvalid * 100 / (this.votescast) * 10) / 10;
} else {
returnvalue = null;
}
if (!valueOnly && returnvalue != null) {
returnvalue = "(" + returnvalue + "%)";
}
return returnvalue;
},
getVotesCastPercent: function (valueOnly) {
var config = Config.get('motions_poll_100_percent_base').value;
var returnvalue;
if (config == "WITH_INVALID" && this.votecast >= 0) {
returnvalue = 100;
} else {
returnvalue = null;
}
if (!valueOnly && returnvalue != null) {
returnvalue = "(" + returnvalue + "%)";
}
return returnvalue;
}
} }
}); });
} }

View File

@ -138,53 +138,71 @@
<tr> <tr>
<td class="icon"> <td class="icon">
<i class="fa fa-thumbs-up fa-2x"></i> <i class="fa fa-thumbs-up fa-2x"></i>
<td> <td ng-init="voteYes = poll.getVote(poll.yes)">
<span class="result_label"><translate>Yes</translate>:</span> <span class="result_label"><translate>Yes</translate>:</span>
<span class="result_value">{{ poll.yes }} {{ poll.getYesPercent() }}</span> <span class="result_value">
<div ng-if="poll.getYesPercent(true)"> {{ voteYes.value }} {{ voteYes.percentStr }}
<uib-progressbar value="poll.getYesPercent(true)" type="success"></uib-progressbar> </span>
<div ng-if="voteYes.percentNumber">
<uib-progressbar value="voteYes.percentNumber" type="success"></uib-progressbar>
</div> </div>
<!-- no --> <!-- no -->
<tr> <tr>
<td class="icon"> <td class="icon">
<i class="fa fa-thumbs-down fa-2x"></i> <i class="fa fa-thumbs-down fa-2x"></i>
<td> <td ng-init="voteNo = poll.getVote(poll.no)">
<span class="result_label"><translate>No</translate>:</span> <span class="result_label"><translate>No</translate>:</span>
<span class="result_value">{{ poll.no }} {{ poll.getNoPercent() }}</span> <span class="result_value" >
<div ng-if="poll.getNoPercent(true)"> {{ voteNo.value }} {{ voteNo.percentStr }}
<uib-progressbar value="poll.getNoPercent(true)" type="danger"></uib-progressbar> </span>
<div ng-if="voteNo.percentNumber">
<uib-progressbar value="voteNo.percentNumber" type="danger"></uib-progressbar>
</div> </div>
<!-- abstain --> <!-- abstain -->
<tr> <tr>
<td class="icon"> <td class="icon">
<strong style="font-size: 26px">&empty;</strong> <strong style="font-size: 26px">&empty;</strong>
<td> <td ng-init="voteAbstain = poll.getVote(poll.abstain)">
<span class="result_label"><translate>Abstain</translate>:</span> <span class="result_label"><translate>Abstain</translate>:</span>
<span class="result_value">{{ poll.abstain }} {{ poll.getAbstainPercent() }}</span> <span class="result_value">
<div ng-if="poll.getAbstainPercent(true)"> {{ voteAbstain.value }} {{ voteAbstain.percentStr }}
<uib-progressbar value="poll.getAbstainPercent(true)" type="warning"></uib-progressbar> </span>
<div ng-if="voteAbstain.percentNumber">
<uib-progressbar value="voteAbstain.percentNumber" type="warning"></uib-progressbar>
</div> </div>
<!-- valid votes --> <!-- valid votes -->
<tr> <tr>
<td class="icon"> <td class="icon">
<i class="fa fa-check fa-lg"></i> <i class="fa fa-check fa-lg"></i>
<td> <td ng-init="votesValid = poll.getVote(poll.votesvalid)">
<span class="result_label"><translate>Valid votes</translate>:</span> <span class="result_label"><translate>Valid votes</translate>:</span>
<span class="result_value">{{ poll.votesvalid }} {{ poll.getVotesValidPercent() }}</span> <span class="result_value">
{{ votesValid.value }} {{ votesValid.percentStr }}
</span>
<!-- invalid votes --> <!-- invalid votes -->
<tr> <tr>
<td class="icon"> <td class="icon">
<i class="fa fa-ban fa-lg"></i> <i class="fa fa-ban fa-lg"></i>
<td> <td ng-init="votesInvalid = poll.getVote(poll.votesinvalid)">
<span class="result_label"><translate>Invalid votes</translate>:</span> <span class="result_label"><translate>Invalid votes</translate>:</span>
<span class="result_value">{{ poll.votesinvalid }} {{ poll.getVotesInvalidPercent() }}</span> <span class="result_value">
{{ votesInvalid.value }}
<span ng-if="config('motions_poll_100_percent_base') == 'WITH_INVALID'">
{{ votesInvalid.percentStr }}
</span>
</span>
<!-- votes cast --> <!-- votes cast -->
<tr class="total"> <tr class="total">
<td class="icon"> <td class="icon">
<strong style="font-size: 16px">&sum;</strong> <strong style="font-size: 16px">&sum;</strong>
<td> <td ng-init="votesCast = poll.getVote(poll.votescast)">
<span class="result_label"><translate>Votes cast</translate>:</span> <span class="result_label"><translate>Votes cast</translate>:</span>
<span class="result_value">{{ poll.votescast }} {{ poll.getVotesCastPercent() }}</span> <span class="result_value">
{{ votesCast.value }}
<span ng-if="config('motions_poll_100_percent_base') == 'WITH_INVALID'">
{{ votesCast.percentStr }}
</span>
</span>
</table> </table>
</ol> </ol>
<button ng-if="motion.isAllowed('create_poll')" ng-click="create_poll()" class="btn btn-default btn-sm"> <button ng-if="motion.isAllowed('create_poll')" ng-click="create_poll()" class="btn btn-default btn-sm">