Exemplo n.º 1
0
def add_resource(request):
    if request.method == 'POST':
        form = ResourceForm(request.POST)
        if form.is_valid():
            r = CalendarResources(
                 resource_common_name=request.POST['resource_common_name'],
                 resource_description=request.POST['resource_description'],
                 resource_type=request.POST['resource_type'])
            r.save()
            r = CalendarResources.objects.latest('updated')
            r.resource_id = 'r%s' % r.id
            r.save()
            client = GetClient()
            try:
                client.CreateResource(
                     resource_id=r.resource_id,
                     resource_common_name=request.POST['resource_common_name'],
                     resource_description=request.POST['resource_description'],
                     resource_type=request.POST['resource_type'])
            except:
                client.UpdateResource(
                     resource_id=r.resource_id,
                     resource_common_name=request.POST['resource_common_name'],
                     resource_description=request.POST['resource_description'],
                     resource_type=request.POST['resource_type'])
            return HttpResponseRedirect('/resource/%s' % r.resource_id)
    else:
        form = ResourceForm()

    return render(request, 'resources/edit_form.html', {
        'title': 'Tambah Sumber Daya',
        'form': form,
    })
Exemplo n.º 2
0
def edit_resource(request, resource_id):
    try:
        r = CalendarResources.objects.get(resource_id=resource_id)
    except:
        return HttpResponseRedirect('/resources/')
    try:
        q = request.GET['q']
    except:
        q = ''
    if request.method == 'POST':
        form = ResourceForm(request.POST)
        if form.is_valid():
            client = GetClient()
            try:
                client.UpdateResource(
                     resource_id=resource_id,
                     resource_common_name=request.POST['resource_common_name'],
                     resource_description=request.POST['resource_description'],
                     resource_type=request.POST['resource_type'])
            except:
                client.CreateResource(
                     resource_id=resource_id,
                     resource_common_name=request.POST['resource_common_name'],
                     resource_description=request.POST['resource_description'],
                     resource_type=request.POST['resource_type'])
            resource = client.GetResource(resource_id=resource_id)
            r.resource_common_name = request.POST['resource_common_name']
            r.resource_description = request.POST['resource_description']
            r.resource_email = resource.resource_email
            r.resource_type = request.POST['resource_type']
            r.save()
            return HttpResponseRedirect('/resources/?q=%s' % q)
    else:
        initial = {
            'resource_common_name': r.resource_common_name,
            'resource_description': r.resource_description,
            'resource_type': r.resource_type,
        }
        form = ResourceForm(initial=initial)

    title = 'Edit Sumber Daya'
    return render(request, 'resources/edit_form.html', {
        'title': title,
        'resource_common_name': r.resource_common_name,
        'form': form,
    })