コード例 #1
0
ファイル: views.py プロジェクト: nickpegg/home
def beer_new(request):
    if not request.user.is_superuser:
        return redirect('brewhouse-display')

    if request.method == 'POST':
        form = AddBeerForm(request.POST)
        if form.is_valid():
            b = Beer()
            b.name = form.cleaned_data['name']
            b.style = form.cleaned_data['style']
            b.recipe_url = form.cleaned_data['recipe_url']
            b.save()

            # Create some basic events based on the brewed-on date
            # tuple is of (event_type, days_since_brew_date)
            brew_date = form.cleaned_data['brew_date']

            events = (
                (7, 0),     # brewing
                (1, 0),     # brewed
                (2, 0),     # primary fermentation
                (3, 7),     # secondary fermentation
                (4, 21),    # kegged
                (0, 28),    # ready!
            )

            for etype, num_days in events:
                event = Event()
                event.beer = b
                event.event_type = etype
                event.date = brew_date + datetime.timedelta(days=num_days)
                event.save()

            return redirect('beer-show', b.id)
        else:
            pass    # TODO set an error
    else:
        form = AddBeerForm()

    return render(request, 'brewhouse/new_beer.html', locals())
コード例 #2
0
ファイル: views.py プロジェクト: durden/django_saucer
def update(request, start=None, fetch=None):
    """Update DB with beers from saucer site starting with index (start) and
    update no more than (fetch) number of beers"""

    if request.method == 'GET':
        ids = []
        ii = 0
        added = 0
        skip = 10
        saucer = Saucer()
        all_beers = saucer.get_all_beers()
        num_beers = len(all_beers)

        if fetch is not None:
            num_beers = int(fetch)

        if start is not None:
            ii = int(start)

        # Don't skip by more than requested
        if num_beers < skip:
            skip = num_beers

        while added < num_beers:
            beers = all_beers[ii:ii + skip]
            for beer in beers:
                ids.append(beer['id'])

            details = saucer.get_beer_details(ids)
            num_details = len(details)
            if not num_details:
                break

            jj = 0
            for det in details:
                _type = beers[jj]['type']
                _name = beers[jj]['name']

                # Maybe we don't find it
                try:
                    curr_beer = Beer.objects.filter(type=_type, name=_name)[0]
                except IndexError:
                    curr_beer = None

                # Beer already in db, but maybe it's newly available
                if curr_beer:
                    if not curr_beer.avail:
                        curr_beer.avail = True
                        curr_beer.date = datetime.date.today()
                        curr_beer.save()
                        added += 1
                else:
                    b = Beer(name=_name, type=_type, avail=True,
                                style=det['Style:'], descr=det['Description:'])
                    b.save()
                    added += 1
                jj += 1

            ii += skip
            ids = []

        template_values = {'fetch': saucer.fetch, 'san': saucer.san,
                            'details': saucer.create_details, 'added': added,
                            'start': start, 'requested': fetch}
        return render_to_response('update.html', template_values)