def new_poll(request, group=None): group = get_object_or_404(Group, name=group) if request.method == 'POST': form = NewPollForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'New poll added') logger.info('%s created a new poll for %s.' % (request.user, group)) return redirect('feedme:feedme_index_new', group) else: messages.error(request, 'Form not validated') else: form = NewPollForm() form.fields['question'].initial = "Hvor skal %s spise?" % group.name form.fields['due_date'].initial = get_next_tuesday() form.fields['group'].initial = group r = dict() r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def orderview(request, order_id=None, group=None): if order_id: order = get_object_or_404(Order, pk=order_id) else: order = Order() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderForm(request.POST, instance=order) if form.is_valid(): form = form.save(commit=False) form.creator = request.user form.order_line = get_order(group) form.save() messages.success(request, 'Orderline added') return redirect('feedme:feedme_index_new', group) form = OrderForm(request.POST) else: form = OrderForm(instance=order) r = dict() r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def admin(request, group=None): # Admin index - defaults to new order page r = dict() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = NewOrderForm(request.POST) if form.is_valid(): form.save() poll = Poll.get_active() if poll: poll.deactivate() messages.success(request, 'New order added') logger.info('%s created a new order for %s: "%s".' % (request.user, group, get_order(group))) return redirect('feedme:feedme_index_new', group) else: form = NewOrderForm() if Poll.objects.count() > 0: poll = Poll.get_active() if poll: form.fields['restaurant'].initial = poll.get_winner() form.fields["date"].initial = get_next_wednesday() # r['form'] = form r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def new_restaurant(request, restaurant_id=None, group=None): if restaurant_id is None: restaurant = Restaurant() else: restaurant = get_object_or_404(Restaurant, pk=restaurant_id) if request.method == 'POST': form = NewRestaurantForm(request.POST, instance=restaurant) if form.is_valid(): form.save() messages.success(request, "Restaurant added") return redirect('feedme:new_order', group) else: form = NewRestaurantForm(request.POST) else: form = NewRestaurantForm(instance=restaurant) r = dict() group = get_object_or_404(Group, name=group) r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def new_restaurant(request, restaurant_id=None, group=None): if restaurant_id is None: restaurant = Restaurant() else: restaurant = get_object_or_404(Restaurant, pk=restaurant_id) if request.method == 'POST': form = NewRestaurantForm(request.POST, instance=restaurant) if form.is_valid(): form.save() messages.success(request, "Restaurant added") return redirect('feedme:new_order', group) else: form = NewRestaurantForm(request.POST) else: form = NewRestaurantForm(instance=restaurant) r = dict() group = get_object_or_404(Group, name=group) r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def admin(request, group=None): # Admin index - defaults to new order page r = dict() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = NewOrderForm(request.POST) if form.is_valid(): form.save() poll = Poll.get_active() if poll: poll.deactivate() messages.success(request, 'New order added') logger.info('%s created a new order for %s: "%s".' % (request.user, group, get_order(group))) return redirect('feedme:feedme_index_new', group) else: form = NewOrderForm() if Poll.objects.count() > 0: poll = Poll.get_active() if poll: form.fields['restaurant'].initial = poll.get_winner() form.fields["date"].initial = get_next_wednesday() # r['form'] = form r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def create_orderline(request, group=None): r = dict() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderLineForm(request.POST) if form.is_valid(): new_orderline = form.save(commit=False) new_orderline.creator = request.user new_orderline.order = get_order(group) users = manually_parse_users(form) if check_orderline(group, new_orderline.creator, new_orderline.price, buddies=users): new_orderline.save() form.save_m2m() # Manually save the m2m relations when using commit=False new_orderline.users.add(new_orderline.creator) messages.success(request, 'Added orderline') return redirect('feedme:feedme_index_new', group) else: messages.error(request, 'Failed to add orderline') else: form = OrderLineForm() form.fields["users"].queryset = get_order(group).available_users().exclude(id=request.user.id) r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def orderview(request, order_id=None, group=None): if order_id: order = get_object_or_404(Order, pk=order_id) else: order = Order() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderForm(request.POST, instance=order) if form.is_valid(): form = form.save(commit=False) form.creator = request.user form.order_line = get_order(group) form.save() messages.success(request, 'Orderline added') return redirect('feedme:feedme_index_new', group) form = OrderForm(request.POST) else: form = OrderForm(instance=order) r = dict() r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def index(request): r = dict( is_admin=is_admin(request), feedme_groups=[ g for g in get_feedme_groups() if request.user in g.user_set.all() ], ) return render(request, 'feedme/index.html', r)
def orderlineview(request, orderline_id=None, group=None): new_or_existing_orderline = 'new' if orderline_id is None: orderline = OrderLine() creator = None else: orderline = get_object_or_404(OrderLine, pk=orderline_id) new_or_existing_orderline = 'existing' creator = orderline.creator group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderLineForm(request.POST, instance=orderline) if form.is_valid(): new_orderline = form.save(commit=False) if creator is not None: new_orderline.creator = creator else: new_orderline.creator = request.user new_orderline.order = get_order(group) users = manually_parse_users(form) if check_orderline(group, creator, orderline.price, buddies=users): new_orderline.save() form.save_m2m( ) # Manually save the m2m relations when using commit=False if new_or_existing_orderline == 'new': messages.success(request, "Orderline added") else: messages.success(request, "Orderline edited") new_orderline.users.add(new_orderline.creator) return redirect('feedme:feedme_index_new', group) else: messages.error( request, "Orderline validation failed, please verify your data and try again." ) else: form = OrderLineForm(instance=orderline) form.fields["users"].queryset = get_order( group).available_users().exclude(id=request.user.id) r = dict() r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def orderlineview(request, orderline_id=None, group=None): new_or_existing_orderline = 'new' if orderline_id is None: orderline = OrderLine() creator = None else: orderline = get_object_or_404(OrderLine, pk=orderline_id) new_or_existing_orderline = 'existing' creator = orderline.creator group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderLineForm(request.POST, instance=orderline) if form.is_valid(): new_orderline = form.save(commit=False) if creator is not None: new_orderline.creator = creator else: new_orderline.creator = request.user new_orderline.order = get_order(group) users = manually_parse_users(form) if check_orderline(group, creator, orderline.price, buddies=users): new_orderline.save() form.save_m2m() # Manually save the m2m relations when using commit=False if new_or_existing_orderline == 'new': messages.success(request, "Orderline added") else: messages.success(request, "Orderline edited") new_orderline.users.add(new_orderline.creator) return redirect('feedme:feedme_index_new', group) else: messages.error(request, "Orderline validation failed, please verify your data and try again.") else: form = OrderLineForm(instance=orderline) form.fields["users"].queryset = get_order(group).available_users().exclude(id=request.user.id) r = dict() r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def index_new(request, group=None): group = get_object_or_404(Group, name=group) if \ request.user in get_object_or_404(Group, name=group).user_set.all() else None order = get_order(group) if group else None poll = get_poll(group) if group else None r = dict() r['group'] = group r['order'] = order r['restaurants'] = Restaurant.objects.all() r['is_admin'] = is_admin(request) r['can_join'] = not in_other_orderline(order, request.user) r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] a_id = None if str(request.user) != 'AnonymousUser': if Answer.objects.filter(poll=poll, user=request.user).count() == 1: a_id = Answer.objects.get(poll=poll, user=request.user) if request.method == 'POST': if request.POST['act'] == 'vote': if a_id is not None: form = PollAnswerForm(request.POST, instance=a_id) else: form = PollAnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.user = request.user answer.poll = poll answer.save() messages.success(request, 'Voted for %s' % answer.answer) return redirect('feedme:feedme_index_new', group) if poll is not None: r['poll'] = poll if a_id is None: r['answer'] = PollAnswerForm() else: r['answer'] = PollAnswerForm(instance=a_id) r['results'] = poll.get_result() return render(request, 'feedme/index.html', r)
def index_new(request, group=None): group = get_object_or_404(Group, name=group) if \ request.user in get_object_or_404(Group, name=group).user_set.all() else None order = get_order(group) if group else None poll = get_poll(group) if group else None r = dict() r['group'] = group r['order'] = order r['restaurants'] = Restaurant.objects.all() r['is_admin'] = is_admin(request) r['can_join'] = not in_other_orderline(order, request.user) r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] a_id = None if str(request.user) != 'AnonymousUser': if Answer.objects.filter(poll=poll, user=request.user).count() == 1: a_id = Answer.objects.get(poll=poll, user=request.user) if request.method == 'POST': if request.POST['act'] == 'vote': if a_id is not None: form = PollAnswerForm(request.POST, instance=a_id) else: form = PollAnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.user = request.user answer.poll = poll answer.save() messages.success(request, 'Voted for %s' % answer.answer) return redirect('feedme:feedme_index_new', group) if poll is not None: r['poll'] = poll if a_id is None: r['answer'] = PollAnswerForm() else: r['answer'] = PollAnswerForm(instance=a_id) r['results'] = poll.get_result() return render(request, 'feedme/index.html', r)
def new_poll(request, group=None): group = get_object_or_404(Group, name=group) if request.method == 'POST': form = NewPollForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'New poll added') logger.info('%s created a new poll for %s.' % (request.user, group)) return redirect('feedme:feedme_index_new', group) else: messages.error(request, 'Form not validated') else: form = NewPollForm() form.fields['question'].initial = "Hvor skal %s spise?" % group.name form.fields['due_date'].initial = get_next_tuesday() form.fields['group'].initial = group r = dict() r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/admin.html', r)
def create_orderline(request, group=None): r = dict() group = get_object_or_404(Group, name=group) if request.method == 'POST': form = OrderLineForm(request.POST) if form.is_valid(): new_orderline = form.save(commit=False) new_orderline.creator = request.user new_orderline.order = get_order(group) users = manually_parse_users(form) if check_orderline(group, new_orderline.creator, new_orderline.price, buddies=users): new_orderline.save() form.save_m2m( ) # Manually save the m2m relations when using commit=False new_orderline.users.add(new_orderline.creator) messages.success(request, 'Added orderline') return redirect('feedme:feedme_index_new', group) else: messages.error(request, 'Failed to add orderline') else: form = OrderLineForm() form.fields["users"].queryset = get_order( group).available_users().exclude(id=request.user.id) r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group r['form'] = form r['is_admin'] = is_admin(request) return render(request, 'feedme/orderview.html', r)
def manage_order(request, group=None): r = dict() group = get_object_or_404(Group, name=group) r['feedme_groups'] = [ g for g in get_feedme_groups() if request.user in g.user_set.all() ] r['group'] = group if request.method == 'POST': form = ManageOrderForm(request.POST) if form.is_valid(): data = form.cleaned_data order = get_object_or_404(Order, pk=data['orders'].id) if 'active_order_submit' in request.POST: order.active = request.POST[ 'active_order_submit'] == 'Activate' order.save() return redirect('feedme:manage_order', group) orderlines = order.orderline_set.all() total_price = order.extra_costs for orderline in orderlines: orderline.users.add(orderline.creator) orderline.each = orderline.get_price_to_pay() total_price += orderline.price r['form'] = form r['order'] = order r['orderlines'] = orderlines r['total_price'] = total_price if request.POST['act'] == 'Load': return render(request, 'feedme/manage_order.html', r) elif request.POST['act'] == 'Edit': for rq in request.POST: if 'edit_orderline_price' in rq: i = rq.split('-')[1] ol = orderlines.get(id=i) change = int(request.POST[rq]) if ol.price != change: ol.price = change ol.save() messages.success( request, 'Changed price for %(ol)s to %(price)s' % { 'ol': ol, 'price': ol.price }) logger.info( '%s changed price of orderline#%i for %s from %.2f to %.2f.' % (request.user, orderline.id, orderline.creator, (ol.price - change), ol.price)) return redirect('feedme:manage_order', group=group) elif request.POST['act'] == 'Pay': paid, existing, negatives = handle_payment(request, order) if len(paid) > 0: messages.success( request, 'Paid orderlines for %s.' % ', '.join(paid)) if len(existing) > 0: messages.error( request, 'Already paid orderlines for %s.' % ', '.join(existing)) if len(negatives) > 0: messages.error( request, 'These users now have negative balances: %s' % ', '.join(negatives)) return redirect('feedme:manage_order', group=group) else: form = ManageOrderForm(request.POST) else: form = ManageOrderForm() active_orders = Order.objects.filter(active=True) inactive_orders = Order.objects.exclude(active=True) orders = active_orders | inactive_orders orders = orders.order_by('-active', '-date') form.fields["orders"].queryset = orders r['form'] = form r['is_admin'] = is_admin(request) r['orders'] = orders return render(request, 'feedme/manage_order.html', r)
def manage_order(request, group=None): r = dict() group = get_object_or_404(Group, name=group) r['feedme_groups'] = [g for g in get_feedme_groups() if request.user in g.user_set.all()] r['group'] = group if request.method == 'POST': form = ManageOrderForm(request.POST) if form.is_valid(): data = form.cleaned_data order = get_object_or_404(Order, pk=data['orders'].id) if 'active_order_submit' in request.POST: order.active = request.POST['active_order_submit'] == 'Activate' order.save() return redirect('feedme:manage_order', group) orderlines = order.orderline_set.all() total_price = order.extra_costs for orderline in orderlines: orderline.users.add(orderline.creator) orderline.each = orderline.get_price_to_pay() total_price += orderline.price r['form'] = form r['order'] = order r['orderlines'] = orderlines r['total_price'] = total_price if request.POST['act'] == 'Load': return render(request, 'feedme/manage_order.html', r) elif request.POST['act'] == 'Edit': for rq in request.POST: if 'edit_orderline_price' in rq: i = rq.split('-')[1] ol = orderlines.get(id=i) change = int(request.POST[rq]) if ol.price != change: ol.price = change ol.save() messages.success( request, 'Changed price for %(ol)s to %(price)s' % {'ol': ol, 'price': ol.price}) logger.info('%s changed price of orderline#%i for %s from %.2f to %.2f.' % (request.user, orderline.id, orderline.creator, (ol.price - change), ol.price)) return redirect('feedme:manage_order', group=group) elif request.POST['act'] == 'Pay': paid, existing, negatives = handle_payment(request, order) if len(paid) > 0: messages.success(request, 'Paid orderlines for %s.' % ', '.join(paid)) if len(existing) > 0: messages.error(request, 'Already paid orderlines for %s.' % ', '.join(existing)) if len(negatives) > 0: messages.error(request, 'These users now have negative balances: %s' % ', '.join(negatives)) return redirect('feedme:manage_order', group=group) else: form = ManageOrderForm(request.POST) else: form = ManageOrderForm() active_orders = Order.objects.filter(active=True) inactive_orders = Order.objects.exclude(active=True) orders = active_orders | inactive_orders orders = orders.order_by('-active', '-date') form.fields["orders"].queryset = orders r['form'] = form r['is_admin'] = is_admin(request) r['orders'] = orders return render(request, 'feedme/manage_order.html', r)
def index(request): r = dict( is_admin=is_admin(request), feedme_groups=[g for g in get_feedme_groups() if request.user in g.user_set.all()], ) return render(request, 'feedme/index.html', r)