【发布时间】:2017-04-03 06:15:50
【问题描述】:
我有一个小问题,我想在管理面板中将客户喜欢的数量转换为百分比,最大浮点数高达 5.0,所以他得到 5 星,如果没有,则向下告诉 0.0喜欢已经成立。
这是我的 model.py:
appointment_upvotes = db.Table('appointment_upvotes',
db.Column('appointment_id', db.Integer, db.ForeignKey('appointment.id')),
db.Column('rate_appo_id', db.Integer, db.ForeignKey('rate_appo.id'))
)
class Appointment(db.Model):
id = db.Column(db.Integer(), primary_key=True)
start_time = db.Column(db.Integer(), nullable=False)
end_time = db.Column(db.Integer(), nullable=False)
timezone = db.Column(db.String(TIMEZONE_LEN_MAX), nullable=False)
note = db.Column(db.String())
status = db.Column(db.Boolean(), default=False)
is_accepted = db.Column(db.Boolean(), default=False)
is_not_accepted = db.Column(db.Boolean(), default=True)
client_id = db.Column(db.Integer(), db.ForeignKey('client.id'))
user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
service_id = db.Column(db.Integer(), db.ForeignKey('service.id'))
rates = db.relationship('RateAppo', backref='appointment', lazy='dynamic')
class RateAppo(db.Model):
id = db.Column(db.Integer(), primary_key=True)
rate_punctuality = db.Column(db.Integer(), default=0)
rate_nonconf = db.Column(db.Integer(), default=0)
rate_solvency = db.Column(db.Integer(), default=0)
text = db.Column(db.Text(), default=None)
appo_id = db.Column(db.Integer(), db.ForeignKey('appointment.id'))
def __repr__(self):
return "Comment: {}".format(self.text)[0:15]
def has_voted(self, appointment_id):
select_votes = appointment_upvotes.select(
db.and_(
appointment_upvotes.c.appointment_id == appointment_id,
appointment_upvotes.c.rate_appo_id == self.id
)
)
rs = db.engine.execute(select_votes)
return False if rs.rowcount == 0 else True
def vote(self, appointment_id, typevote):
already_voted = self.has_voted(appointment_id)
vote_status = None
if not already_voted:
if typevote == 'rate_punctuality':
db.engine.execute(
appointment_upvotes.insert(),
appointment_id = appointment_id,
rate_appo_id = self.id
)
self.rate_punctuality = self.rate_punctuality + 1
vote_status = True
elif typevote == 'rate_nonconf':
db.engine.execute(
appointment_upvotes.insert(),
appointment_id = appointment_id,
rate_appo_id = self.id
)
self.rate_nonconf = self.rate_nonconf + 1
vote_status = True
else:
db.engine.execute(
appointment_upvotes.insert(),
appointment_id = appointment_id,
rate_appo_id = self.id
)
self.rate_solvency = self.rate_solvency + 1
vote_status = True
db.session.commit()
return vote_status
在管理面板中,费率将如下所示:
平均评分:4.5
不管他有多少赞,假设他有 100 个赞,我不想按计数显示,只需要使值达到 5.0 分
我忘了提到的另一件事,也许在我的jinja 模板中我可以让它工作??
这是一个例子:
准时性:100 个赞,百分比可能是 4.52
偿付能力:63百分比可能是 3.23
所以在 Punctuality 中会有 4 星,而在 Solvency 中会是 3 星,依此类推。
最后一件事是在一行中显示所有这些喜欢的完整统计数据,例如在我的面板中,我会得到这样的东西:
所以在我的模板中,我可以输入如下内容:
<div class="col-sm-8"><h2>Average Rating: <strong> {{ "%.2f" % user.rate_appo.count }} </strong></h2>
<div class="table-responsive">
<table class="table table-striped text-center">
<thead>
<tr>
<th class="text-center">Punctuality</th>
<th class="text-center">Non-Conflict</th>
<th class="text-center">Solvency</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ "%.2f" % user.rate_appo.rate_punctuality.count}} </td>
<td>{{ "%.2f" % user.rate_appo.rate_nonconf.count}} </td>
<td>{{ "%.2f" % user.rate_appo.rate_solvency.count}} </td>
</tr>
</tbody>
</table>
</div>
我真的不确定我在上面输入了什么,我使用 %.2f 只是为了简化示例以便大家更清楚。
最终,这是一个正在进行的项目,如果有任何建议,我们将不胜感激:)。
【问题讨论】: