예제 #1
0
def story_json(request):
    """ A GET request for a given ID will return a story's data in JSON.
        Do an AJAX POST to this bad boy to validate and store
        a new user story. Returns JSON with 'result' = 'OK'
        or an error message to show to the user.
    """
    
    print request.POST
    
    if request.method == 'POST':
        # TODO: Add validation; I should only be allowed to update stories
        #       in products I have access to as a user.        
        try:
            # Updating an existing story
            current = story.objects.get(pk=request.POST['story_id'])
            form = story_form(request.POST, instance=current, initial={'is_release': False})
        except:
            # Creating a new story
            form = story_form(request.POST)

        if form.is_valid():
            form.save()
            
        return HttpResponse(str(form))

    else:   # GET request
        try:
            model_text = '"model": "website.story", '
            data = story.objects.get(pk=request.GET['story_id'])
            # TODO: The Django serializer sucks, should be more explicit and provide
            # real data, not just a bunch of ID numbers
            data = serializers.serialize("json", [data]).replace('"pk":','"id":').replace(model_text, '')
            return HttpResponse(data, mimetype='application/json')
        except:
            # Either the story_id was not POSTed or did not exist
            raise Http404
예제 #2
0
def main_page(request, use_product=None):
    product_list = request.user.products.all()
    backlog_list = story_list = []
    current_product = None

    if product_list:
        current_product = request.session.get('current_product', None)

        if use_product:
            current_product = product.objects.get(name=use_product)
            request.session['current_product'] = current_product
            
        if not current_product:
            current_product = product_list[0]
            request.session['current_product'] = current_product

        backlog_list = backlog.objects.filter(product=current_product)
        backlog_list = _order_backlogs(backlog_list)
        story_list = story.objects.filter(backlog__in=backlog_list).order_by('order')
        story_modelformset = modelformset_factory(story)
    
    for s in story_list:
        s.next_action = _get_next_action(s)
    
    context = {
        'product_list': product_list,
        'current_product': current_product,
        'backlog_list': backlog_list,
        'story_list': story_list,
        'user': request.user,
        'details_formset': {},
    }

    # Add a form for each story pre-populated with the current data
    for s in story_list:
        context['details_formset'][s.id] = story_form(instance=s)
  
    context.update(csrf(request))
    return render_to_response('main.html', context)