Example #1
0
def user_items(request, username, template='inventory/user_list.html'):
    """List all the items for the username 
        - list the items
        - give the possibilities to add a new item
    """

    user = User.objects.filter(username=username)
    items = Item.objects.filter(
        user__username=username).order_by('-expire_date')[:]
    locations = []
    for item in items:
        locations.append(item.location)

    center_obj = calculate_center(locations)
    data = {'items': items, 'username': username, 'center': center_obj}
    if user:
        if request.user.is_authenticated():
            if username == request.user.username:
                data['same_user'] = True
            else:
                # User logged in but not owner of the url.
                data['same_user'] = False
        return render_to_response('inventory/user_list.html',
                                  data,
                                  context_instance=RequestContext(request))
    else:
        # User does not exist.
        return render_to_response('inventory/user_does_not_exist.html',
                                  {'username': username},
                                  context_instance=RequestContext(request))
Example #2
0
def new(request,
        username,
        form_class=ItemForm,
        template_name="inventory/new_item.html"):
    "Add a new item to the inventory."
    locations = Location.objects.filter(user=request.user.id)
    map_center = calculate_center(locations)
    serialezed_locations = serializers.serialize("geojson", locations)
    if request.method == 'POST':  # If the form has been submitted...
        form = form_class(request.user, request.POST)
        if form.is_valid():
            logger.info('Saving the item in the db.')
            item = form.save(commit=False)
            item.user = request.user
            item.save()
            #            request.user.message_set.create(
            #                    message=_("%(name)s has been added to the SustainableSouk") %{'name': item.name})
            return HttpResponseRedirect(
                reverse('item_detail', args=(
                    request.user,
                    item.pk,
                )))

    else:
        # A dynamically loaded form
        form = form_class(initial={'user': request.user})
        # Restricted to the location which belongs to the user

        form.fields['location'].queryset = locations

    return render_to_response(template_name, {
        "form": form,
        "username": username,
        "locations": locations,
        "serialed_locations": serialezed_locations,
        "map_center": map_center
    },
                              context_instance=RequestContext(request))