<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2012 <NAME> <<EMAIL>>                  
# License: MIT (see LICENSE.TXT file) 


from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_http_methods
from django.contrib.auth.decorators import login_required

from apps.common.shortcuts import render_response
from apps.account.models import Account
from apps.team.models import Team
from apps.bike.models import Bike
from apps.team.utils import render_team_response as rtr
from apps.team.utils import assert_member
from apps.bike import forms


def _get_bike_filters(request, form):
    # filters 
    #  date from and to
    #  active (only members)
    #  reserve (only members)
    #  kind (default all)
    #  gender (default all)
    #  size (default all)
    #  lights (default all)
    #  fenders (default all)
    #  rack (default all)
    #  basket (default all)
    return {}


@login_required
@require_http_methods(["GET"])
def view_my(request, bike_id):
    account = get_object_or_404(Account, user=request.user)
    bike = get_object_or_404(Bike, id=bike_id, owner=account)
    return render_response(request, "bike/view_my.html", { "bike" : bike })


@require_http_methods(["GET"])
def view_team(request, team_link, bike_id):
    team = get_object_or_404(Team, link=team_link)
    bike = get_object_or_404(Bike, id=bike_id, team=team)
    return rtr(team, "bikes", request, "bike/view_team.html", { "bike" : bike })


@require_http_methods(["GET"])
def list_team(request, team_link):
    team = get_object_or_404(Team, link=team_link)
    filters = _get_bike_filters(request, None)
    args = { "bikes" :  team.bikes.filter(**filters) }
    return rtr(team, "bikes", request, "bike/list_team.html", args)


@login_required
@require_http_methods(["GET"])
def list_my(request):
    account = get_object_or_404(Account, user=request.user)
    args = { "bikes" :  Bike.objects.filter(owner=account) }
    return render_response(request, "bike/list_my.html", args)


@login_required
@require_http_methods(["GET", "POST"])
def create(request, team_link):
    team = get_object_or_404(Team, link=team_link)
    account = get_object_or_404(Account, user=request.user)
    assert_member(account, team)
    if request.method == "POST":
        form = forms.Create(request.POST, team=team, account=account)
        if form.is_valid():
            bike = Bike()
            bike.team = team
            bike.owner = form.cleaned_data["owner"]
            bike.name = form.cleaned_data["name"].strip()
            bike.description = form.cleaned_data["description"].strip()
            bike.active = form.cleaned_data["active"]
            bike.reserve = form.cleaned_data["reserve"]
            bike.station = form.cleaned_data["station"]
            bike.lockcode = form.cleaned_data["lockcode"]
            bike.keycode = form.cleaned_data["keycode"]
            bike.kind = form.cleaned_data["kind"]
            bike.gender = form.cleaned_data["gender"]
            bike.size = form.cleaned_data["size"]
            bike.lights = form.cleaned_data["lights"]
            bike.fenders = form.cleaned_data["fenders"]
            bike.basket = form.cleaned_data["rack"]
            bike.save()
            return HttpResponseRedirect("/%s/bikes" % team.link)
    else:
        form = forms.Create(team=team, account=account)
    args = { "form" : form }
    return rtr(team, "bikes", request, "bike/create.html", args)

<commit_msg>added filter for inactive and reserve bikes for non team members<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2012 <NAME> <<EMAIL>>                  
# License: MIT (see LICENSE.TXT file) 


from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_http_methods
from django.contrib.auth.decorators import login_required

from apps.common.shortcuts import render_response
from apps.account.models import Account
from apps.team.models import Team
from apps.bike.models import Bike
from apps.team.utils import render_team_response as rtr
from apps.team.utils import assert_member
from apps.bike import forms


def _get_bike_filters(request, form, team):
    filters = {}
    logged_in = request.user.is_authenticated()
    account = logged_in and get_object_or_404(Account, user=request.user)
    if not logged_in or account not in team.members.all():
        filters.update({ "reserve" : False, "active" : True, })

    # filters 
    #  date from and to
    #  active (only members)
    #  reserve (only members)
    #  kind (default all)
    #  gender (default all)
    #  size (default all)
    #  lights (default all)
    #  fenders (default all)
    #  rack (default all)
    #  basket (default all)
    return filters


@login_required
@require_http_methods(["GET"])
def view_my(request, bike_id):
    account = get_object_or_404(Account, user=request.user)
    bike = get_object_or_404(Bike, id=bike_id, owner=account)
    return render_response(request, "bike/view_my.html", { "bike" : bike })


@require_http_methods(["GET"])
def view_team(request, team_link, bike_id):
    team = get_object_or_404(Team, link=team_link)
    bike = get_object_or_404(Bike, id=bike_id, team=team)
    return rtr(team, "bikes", request, "bike/view_team.html", { "bike" : bike })


@require_http_methods(["GET"])
def list_team(request, team_link):
    team = get_object_or_404(Team, link=team_link)
    filters = _get_bike_filters(request, None, team)
    args = { "bikes" :  team.bikes.filter(**filters) }
    return rtr(team, "bikes", request, "bike/list_team.html", args)


@login_required
@require_http_methods(["GET"])
def list_my(request):
    account = get_object_or_404(Account, user=request.user)
    args = { "bikes" :  Bike.objects.filter(owner=account) }
    return render_response(request, "bike/list_my.html", args)


@login_required
@require_http_methods(["GET", "POST"])
def create(request, team_link):
    team = get_object_or_404(Team, link=team_link)
    account = get_object_or_404(Account, user=request.user)
    assert_member(account, team)
    if request.method == "POST":
        form = forms.Create(request.POST, team=team, account=account)
        if form.is_valid():
            bike = Bike()
            bike.team = team
            bike.owner = form.cleaned_data["owner"]
            bike.name = form.cleaned_data["name"].strip()
            bike.description = form.cleaned_data["description"].strip()
            bike.active = form.cleaned_data["active"]
            bike.reserve = form.cleaned_data["reserve"]
            bike.station = form.cleaned_data["station"]
            bike.lockcode = form.cleaned_data["lockcode"]
            bike.keycode = form.cleaned_data["keycode"]
            bike.kind = form.cleaned_data["kind"]
            bike.gender = form.cleaned_data["gender"]
            bike.size = form.cleaned_data["size"]
            bike.lights = form.cleaned_data["lights"]
            bike.fenders = form.cleaned_data["fenders"]
            bike.basket = form.cleaned_data["rack"]
            bike.save()
            url = "/%s/bike/view/%s" % (team.link, bike.id)
            return HttpResponseRedirect(url)
    else:
        form = forms.Create(team=team, account=account)
    args = { "form" : form }
    return rtr(team, "bikes", request, "bike/create.html", args)


{'fraction_non_alphanumeric': 0.12163059354667374, 'fraction_numerical': 0.008896560881689018, 'mean_word_length': 3.4175953079178885, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6372468', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2443, 'n_words': 519}