Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds the ability to remove selected reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Apr 19, 2016
1 parent 5de3178 commit ed66bad
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
16 changes: 14 additions & 2 deletions onegov/town/assets/js/reservationcalendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ rc.setupReservationSelect = function(fcOptions) {
});

$(window).on('rc-reservation-error', function() {
console.log('error');
});

$(window).on('rc-reservations-changed', function() {
Expand Down Expand Up @@ -488,7 +487,19 @@ rc.sumPartitions = function(partitions) {
};

ReservationSelection = React.createClass({
handleClick: function(reservation) {
$.ajax({
url: reservation.delete,
type: 'DELETE',
success: function() {
$(window).trigger('rc-reservations-changed');
}
});

return false;
},
render: function() {
var self = this;
return (
<div className="reservation-selection-inner">
<h3>{locale("Reservations")}</h3>
Expand All @@ -500,11 +511,12 @@ ReservationSelection = React.createClass({
this.props.reservations.length > 0 &&
<ul>{
_.map(this.props.reservations, function(r, ix) {
var boundClick = self.handleClick.bind(this, r);
return (
<li key={ix} className={r.className + " reservation"}>
<span className="reservation-date">{r.date}</span>
<span className="reservation-time">{r.time}</span>
<a href="#">{locale('Remove')}</a>
<a className="delete" onClick={boundClick}>{locale('Remove')}</a>
</li>
);
})
Expand Down
36 changes: 36 additions & 0 deletions onegov/town/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from onegov.town import _
from onegov.town.elements import DeleteLink, Link
from operator import attrgetter
from purl import URL
from uuid import uuid4


Expand Down Expand Up @@ -105,6 +106,41 @@ def parse_fullcalendar_request(request, timezone):
return None, None


class ReservationInfo(object):

__slots__ = ['resource', 'reservation', 'request', 'translate']

def __init__(self, reservation, request):
self.reservation = reservation
self.request = request
self.translate = request.translate

@property
def date(self):
return '{:%d.%m.%Y}'.format(self.reservation.display_start())

@property
def time(self):
if sedate.is_whole_day(
self.reservation.start,
self.reservation.end,
self.reservation.timezone
):
return self.translate(_("Whole day"))
else:
return '{:%H:%M} - {:%H:%M}'.format(
self.reservation.display_start(),
self.reservation.display_end()
)

@property
def delete_link(self):
url = self.request.link(self.reservation)
url = URL(url).query_param('csrf-token', self.request.new_csrf_token())

return url.as_string()


class AllocationEventInfo(object):

__slots__ = ['allocation', 'availability', 'request', 'translate']
Expand Down
36 changes: 36 additions & 0 deletions onegov/town/views/reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ def trigger_calendar_update(response):
}


@TownApp.json(model=Reservation, request_method='DELETE', permission=Public)
def delete_reservation(self, request):

# this view is public, but only for a limited time
assert_anonymous_access_only_temporary(self, request)

collection = ResourceCollection(request.app.libres_context)
resource = collection.by_id(self.resource)

scheduler = resource.get_scheduler(request.app.libres_context)

try:
scheduler.remove_reservation(self.token, self.id)
except LibresError as e:
message = {
'message': utils.get_libres_error(e, request),
'success': False
}

@request.after
def trigger_calendar_update(response):
response.headers.add('X-IC-Trigger', 'rc-reservation-error')
response.headers.add('X-IC-Trigger-Data', json.dumps(message))

return message
else:

@request.after
def trigger_calendar_update(response):
response.headers.add('X-IC-Trigger', 'rc-reservations-changed')

return {
'success': True
}


@TownApp.form(model=Reservation, name='bearbeiten', template='reservation.pt',
permission=Public, form=get_reservation_form_class)
def handle_edit_reservation(self, request, form):
Expand Down
28 changes: 12 additions & 16 deletions onegov/town/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
from itertools import groupby
from libres.db.models import Allocation, Reservation
from libres.db.models import Reservation
from onegov.core.security import Public, Private
from onegov.libres import ResourceCollection
from onegov.libres.models import Resource
Expand Down Expand Up @@ -187,23 +187,19 @@ def get_reservations(self, request):
scheduler = resource.get_scheduler(request.app.libres_context)
session = utils.get_libres_session_id(request)

uuids = scheduler.queries.reservations_by_session(session)
uuids = uuids.with_entities(Reservation.target)
uuids = uuids.filter(Reservation.target_type == 'allocation')
uuids = uuids.subquery()
reservations = scheduler.queries.reservations_by_session(session)
reservations = reservations.order_by(Reservation.start)
reservations = reservations.all()

allocations = scheduler.managed_allocations()
allocations = allocations.filter(Allocation.group.in_(uuids))
allocations = allocations.all()

events = utils.AllocationEventInfo.from_allocations(
request, scheduler, allocations
)
info = [
utils.ReservationInfo(reservation, request)
for reservation in reservations
]

return [
{
'date': '{:%d.%m.%Y}'.format(e.allocation.display_start()),
'time': e.event_time,
'className': e.event_class
} for e in events
'date': i.date,
'time': i.time,
'delete': i.delete_link,
} for i in info
]

0 comments on commit ed66bad

Please sign in to comment.