def update_request(request):
    context_instance = RequestContext(request)
    current_date = datetime.datetime.now()
    current_date = current_date.strftime("%d-%m-%Y")
    
    modified_date = datetime.datetime.now()
    modified_date = modified_date.strftime("%a %b %d %Y %H:%M:%S")
    
    extra_assignees = request.POST.getlist('user_added')
    total_users = request.POST.getlist('database_users')

    extra_assignees = list(extra_assignees)
    
    
    post_data = request.POST.copy()
    primary_assignees = post_data['ticketowner']
    attachment =  post_data['attach_file']
    #extra_assignees.append(primary_assignees)
    request_id = post_data['request_id']
    update_request = new_request.objects.get( id=request_id )
    update_request.title = post_data['title']
    update_request.creator = post_data['creator']
    #update_request.request_owner = extra_assignees
    update_request.primary_assignee = primary_assignees
    update_request.department = post_data['department']
    #update_request.status = post_data['status']
    update_request.priority = post_data['priority']
    update_request.severity = post_data['severity']
    update_request.description = post_data['description']
    #update_request.creation_date = post_data['creation_date']
    #update_request.last_modified_date = current_date  
    update_request.save()
    
    
    request_owner_names = request_owners.objects.filter( new_request = update_request.id ).delete()
    
    for names in extra_assignees:
        request_owner_names = request_owners()
        request_owner_names.new_request_id = update_request.id
        request_owner_names.request_owner = names
        request_owner_names.save()
    
    
    update_attachment = update_request.request_attachment_set.all()
  
    template_values = { 'MEDIA_URL': media_url, 'request':context_instance, 'request_id': request_id, 'update_attachment': update_attachment }
    #return render_to_response("new_request_submitted.html", template_values)
    
    if str(attachment) == 'True':
        return render_to_response("attachment_update.html", template_values)
    else:
        return render_to_response("new_request_submitted.html", template_values)
def create_new_request(request):
    context_instance = RequestContext(request)
    current_date = datetime.datetime.now()
    current_date = current_date.strftime("%d-%m-%Y")
    
    modified_date = datetime.datetime.now()
    modified_date = modified_date.strftime("%a %b %d %Y %H:%M:%S")

    post_data = request.POST.copy()
    extra_assignees = request.POST.getlist('user_added')
    total_users = request.POST.getlist('database_users')

    
    primary_assignees = post_data['ticketowner']
    attachment =  post_data['attach_file']
    
    create_new_request = new_request()
    create_new_request.title = post_data['title']
    create_new_request.creator = request.user
    create_new_request.primary_assignee = primary_assignees
    create_new_request.department = post_data['department']
    create_new_request.status = post_data['status']
    create_new_request.priority = post_data['pri_radio']
    create_new_request.severity = post_data['sev_radio']
    
    upper_case_user = str(request.user).capitalize()
    posted_decsription = "<b>"+ upper_case_user + " has requested on " + modified_date +":<br /></b>" +post_data['description']
    create_new_request.description = posted_decsription
    create_new_request.creation_date = current_date
    create_new_request.last_modified_date = current_date
    create_new_request.attachment = attachment
    create_new_request.save()
    
    
    for names in extra_assignees:
        request_owner_names = request_owners()
        request_owner_names.new_request_id = create_new_request.id
        request_owner_names.request_owner = names
        request_owner_names.save()
    
    template_values = { 'MEDIA_URL': media_url,'request':context_instance,'request_id': create_new_request.id }
  
    if str(attachment) == 'True':
        return render_to_response("file_upload.html", template_values)
    else:
        return render_to_response("new_request_submitted.html", template_values)