Skip to content

Commit

Permalink
Create Proposal.is_accepted columnproperty to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
russss committed Feb 4, 2024
1 parent 313ba8b commit 9c1ebef
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 42 deletions.
4 changes: 1 addition & 3 deletions apps/admin/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def get_users(dest: str) -> list[User]:
elif dest == "purchasers":
query = query.join(User.payments).filter(Payment.state == "paid")
elif dest == "cfp":
query = query.join(User.proposals).filter(
Proposal.state.in_(("accepted", "finished"))
)
query = query.join(User.proposals).filter(Proposal.is_accepted)
elif dest == "villages":
query = query.join(User.village_membership).filter(VillageMember.admin)

Expand Down
2 changes: 1 addition & 1 deletion apps/api/installations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Installations(Resource):
def get(self):
result = []
proposals = InstallationProposal.query.filter(
InstallationProposal.state.in_(["accepted", "finished"])
InstallationProposal.is_accepted
).all()
for proposal in proposals:
result.append(render_installation(proposal))
Expand Down
2 changes: 1 addition & 1 deletion apps/cfp/schedule_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def apply_potential_schedule(email, type):
| (Proposal.potential_time != None) # noqa: E711
)
.filter(Proposal.scheduled_duration.isnot(None))
.filter(Proposal.state.in_(["accepted", "finished"]))
.filter(Proposal.is_accepted)
.all()
)

Expand Down
4 changes: 2 additions & 2 deletions apps/cfp/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Scheduler(object):
def set_rough_durations(self):
proposals = (
Proposal.query.filter_by(scheduled_duration=None, type="talk")
.filter(Proposal.state.in_(["accepted", "finished"]))
.filter(Proposal.is_accepted)
.all()
)

Expand All @@ -49,7 +49,7 @@ def get_scheduler_data(
):
proposals = (
Proposal.query.filter(Proposal.scheduled_duration.isnot(None))
.filter(Proposal.state.in_(["finished", "accepted"]))
.filter(Proposal.is_accepted)
.filter(Proposal.type.in_(type))
.order_by(Proposal.favourite_count.desc())
.all()
Expand Down
2 changes: 1 addition & 1 deletion apps/cfp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def email_check():
"""Email speakers about their slot"""
proposals = (
Proposal.query.filter(Proposal.scheduled_duration.isnot(None))
.filter(Proposal.state.in_(["accepted", "finished"]))
.filter(Proposal.is_accepted)
.filter(Proposal.type.in_(["talk", "workshop", "youthworkshop", "performance"]))
.all()
)
Expand Down
5 changes: 2 additions & 3 deletions apps/cfp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class LightningTalkProposalForm(ProposalForm):

def set_session_choices(self, remaining_lightning_slots):
self.session.choices = []
for (day_id, day_count) in remaining_lightning_slots.items():
for day_id, day_count in remaining_lightning_slots.items():
if day_count <= 0:
continue
self.session.choices.append(
Expand Down Expand Up @@ -533,7 +533,6 @@ def withdraw_proposal(proposal_id):
form = WithdrawalForm()
if form.validate_on_submit():
if form.confirm_withdrawal.data:

app.logger.info("Proposal %s is being withdrawn.", proposal_id)
proposal.set_state("withdrawn")

Expand Down Expand Up @@ -653,7 +652,7 @@ def finalise_proposal(proposal_id):
if proposal.user != current_user:
abort(404)

if proposal.state not in ("accepted", "finished"):
if not proposal.is_accepted:
return redirect(url_for(".edit_proposal", proposal_id=proposal_id))

# This is horrendous, but is a lot cleaner than having shitloads of classes and fields
Expand Down
19 changes: 7 additions & 12 deletions apps/cfp_review/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def close_round():
if form.validate_on_submit():
if form.confirm.data:
min_votes = session["min_votes"]
for (prop, vote_count) in proposals:
for prop, vote_count in proposals:
if vote_count >= min_votes and prop.state != "reviewed":
prop.set_state("reviewed")

Expand All @@ -896,7 +896,7 @@ def close_round():
# Find proposals where the submitter has already had an accepted proposal
# or another proposal in this list
duplicates = {}
for (prop, _) in proposals:
for prop, _ in proposals:
if prop.user.proposals.count() > 1:
# Only add each proposal once
if prop.user not in duplicates:
Expand Down Expand Up @@ -937,8 +937,7 @@ def rank():
if form.confirm.data:
min_score = session["min_score"]
count = 0
for (prop, score) in scored_proposals:

for prop, score in scored_proposals:
if score >= min_score:
count += 1
prop.set_state("accepted")
Expand Down Expand Up @@ -975,19 +974,15 @@ def rank():
elif form.cancel.data and "min_score" in session:
del session["min_score"]

accepted_proposals = Proposal.query.filter(
Proposal.state.in_(["accepted", "finished"])
)
accepted_proposals = Proposal.query.filter(Proposal.is_accepted)
accepted_counts = defaultdict(int)
for proposal in accepted_proposals:
accepted_counts[proposal.type] += 1

allocated_minutes = defaultdict(int)
unknown_lengths = defaultdict(int)

accepted_proposals = Proposal.query.filter(
Proposal.state.in_(["accepted", "finished"])
).all()
accepted_proposals = Proposal.query.filter(Proposal.is_accepted).all()
for proposal in accepted_proposals:
length = None
if proposal.scheduled_duration:
Expand Down Expand Up @@ -1060,7 +1055,7 @@ def potential_schedule_changes():
def scheduler():
proposals = (
Proposal.query.filter(Proposal.scheduled_duration.isnot(None))
.filter(Proposal.state.in_(["finished", "accepted"]))
.filter(Proposal.state.is_accepted)
.filter(Proposal.type.in_(["talk", "workshop", "youthworkshop", "performance"]))
.all()
)
Expand Down Expand Up @@ -1168,7 +1163,7 @@ def clashfinder():

clashes = []
offset = 0
for ((id1, id2), count) in popularity.most_common()[:1000]:
for (id1, id2), count in popularity.most_common()[:1000]:
offset += 1
prop1 = Proposal.query.get(id1)
prop2 = Proposal.query.get(id2)
Expand Down
3 changes: 1 addition & 2 deletions apps/cfp_review/venues.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def edit_venue(venue_id):
if form.validate_on_submit():
if form.delete.data:
scheduled_content = Proposal.query.filter(
Proposal.scheduled_venue_id == venue.id,
Proposal.state.in_(["accepted", "finished"]),
Proposal.scheduled_venue_id == venue.id, Proposal.is_accepted
).count()

if scheduled_content > 0:
Expand Down
2 changes: 1 addition & 1 deletion apps/schedule/attendee_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def attendee_content():
or_(
Proposal.user_scheduled is True, Proposal.scheduled_venue_id.in_(venue_ids)
),
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
).all()

form = ContentForm()
Expand Down
8 changes: 4 additions & 4 deletions apps/schedule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def schedule_current():
def line_up():
proposals = Proposal.query.filter(
Proposal.scheduled_duration.isnot(None),
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
Proposal.type.in_(["talk", "workshop", "youthworkshop", "performance"]),
Proposal.hide_from_schedule.isnot(True),
).all()
Expand Down Expand Up @@ -172,7 +172,7 @@ def item(year, proposal_id, slug=None):
def item_current(year, proposal_id, slug=None):
"""Display a detail page for a talk from the current event"""
proposal = Proposal.query.get_or_404(proposal_id)
if proposal.state not in ("accepted", "finished") or proposal.hide_from_schedule:
if not proposal.is_accepted or proposal.hide_from_schedule:
abort(404)

if not current_user.is_anonymous:
Expand Down Expand Up @@ -308,7 +308,7 @@ def herald_message(message, proposal):
Proposal.query.join(Venue, Venue.id == Proposal.scheduled_venue_id)
.filter(
Venue.name == venue_name,
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
Proposal.scheduled_time > pendulum.now(event_tz),
Proposal.scheduled_duration.isnot(None),
Proposal.hide_from_schedule.isnot(True),
Expand Down Expand Up @@ -394,7 +394,7 @@ def greenroom_message(message):
upcoming = (
Proposal.query.filter(
Proposal.type.in_(["talk", "workshop", "youthworkshop", "performance"]),
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
Proposal.scheduled_time > pendulum.now(event_tz),
Proposal.scheduled_duration.isnot(None),
Proposal.hide_from_schedule.isnot(True),
Expand Down
2 changes: 1 addition & 1 deletion apps/schedule/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _get_scheduled_proposals(filter_obj={}, override_user=None):
external_favourites = [f.id for f in user.calendar_favourites]

schedule = Proposal.query.filter(
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
Proposal.scheduled_time.isnot(None),
Proposal.scheduled_venue_id.isnot(None),
Proposal.scheduled_duration.isnot(None),
Expand Down
4 changes: 2 additions & 2 deletions apps/schedule/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def schedule_frab(year):

schedule = (
Proposal.query.filter(
Proposal.state.in_(["accepted", "finished"]),
Proposal.is_accepted,
Proposal.scheduled_time.isnot(None),
Proposal.scheduled_venue_id.isnot(None),
Proposal.scheduled_duration.isnot(None),
Expand Down Expand Up @@ -169,7 +169,7 @@ def item_json(year, proposal_id, slug=None):
if year != event_year():
abort(404)
proposal = Proposal.query.get_or_404(proposal_id)
if proposal.state not in ("accepted", "finished"):
if not proposal.is_accepted:
abort(404)

if not current_user.is_anonymous:
Expand Down
2 changes: 1 addition & 1 deletion apps/volunteer/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run(self):

events = (
Proposal.query.join(Venue, Proposal.scheduled_venue_id == Venue.id)
.filter(Venue.name == venue.name, Proposal.state == "finished")
.filter(Venue.name == venue.name, Proposal.is_accepted)
.all()
)
for e in events:
Expand Down
10 changes: 6 additions & 4 deletions models/cfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ class Proposal(BaseModel):
state = db.Column(db.String, nullable=False, default="new")
type = db.Column(db.String, nullable=False) # talk, workshop or installation

is_accepted = column_property(state.in_(["accepted", "finished"]))

# Core information
title = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=False)
Expand Down Expand Up @@ -525,16 +527,16 @@ def get_export_data(cls):
Venue.name,
)
accepted_proposals = (
proposals.filter(cls.state.in_(["accepted", "finished"]))
proposals.filter(cls.is_accepted)
.outerjoin(cls.scheduled_venue)
.join(cls.user)
.add_columns(*accepted_columns)
)

other_proposals = proposals.filter(~cls.state.in_(["accepted", "finished"]))
other_proposals = proposals.filter(~cls.is_accepted)

user_favourites = (
cls.query.filter(cls.state.in_(["accepted", "finished"]))
cls.query.filter(cls.is_accepted)
.join(cls.favourites)
.with_entities(User.id.label("user_id"), cls.id)
.order_by(User.id)
Expand All @@ -556,7 +558,7 @@ def get_export_data(cls):
Venue.name.label("venue"),
)
accepted_public = (
cls.query.filter(cls.state.in_(["accepted", "finished"]))
cls.query.filter(cls.is_accepted)
.outerjoin(cls.scheduled_venue)
.with_entities(*public_columns)
)
Expand Down
2 changes: 1 addition & 1 deletion templates/cfp/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2>Proposal Detail</h2>
{% include "cfp/edit-form.html" %}
{% else %}
<div class="well"><div class="row">
{% if feature_enabled('CFP_FINALISE') and proposal.state in ['accepted', 'finished'] %}
{% if feature_enabled('CFP_FINALISE') and proposal.is_accepted %}
<div class="col-md-12">
Your {{ proposal.human_type }} has been accepted! If you haven't already, please
<a href="{{ url_for('.finalise_proposal', proposal_id=proposal.id ) }}">finalise your details</a>.
Expand Down
4 changes: 2 additions & 2 deletions templates/cfp/proposals.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ <h4><a href="{{ url_for('.edit_proposal', proposal_id=proposal.id) }}">{{ propos
{{ proposal.get_unread_count(current_user) }} unread of {{ proposal.messages | length }}
</a>
</dd>
{% if proposal.state != 'accepted' and proposal.state != 'finished' %}
{% if not proposal.is_accepted %}
<dt>Required notice</dt>
<dd>{{ proposal.notice_required }}</dd>
{% endif %}
{% if (proposal.type == 'talk' or proposal.type == 'workshop') %}
{% if (proposal.state == 'accepted' or proposal.state == 'finished') %}
{% if proposal.is_accepted %}
<dt>Slot length</dt><dd>{{proposal.scheduled_duration}} minutes</dd>
<dt>Slot time</dt><dd>{% if proposal.scheduled_time %}<b><font color="green">{{proposal.scheduled_time.strftime('%A at %-I:%M %p')}}</font></b>{% else %}Not yet scheduled{% endif %}</dd>
<dt>Venue</dt><dd>{{proposal.scheduled_venue_name or "Not yet scheduled"}}</dd>
Expand Down
2 changes: 1 addition & 1 deletion templates/cfp_review/update_proposal.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h4 class="panel-title">
</div>
</div>
</div>
{% if proposal.state in ('accepted', 'finished') %}
{% if proposal.is_accepted %}
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="publishedPanelHeading">
<h4 class="panel-title">
Expand Down

0 comments on commit 9c1ebef

Please sign in to comment.