Source

fat-po-editor / fat / fat_po_editor / views / repository.py

Full commit
# -*- coding: utf-8 -*-

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.template import RequestContext

from fat_po_editor.util.json import json_http_response_fail
from fat_po_editor.util.json import json_http_response

from fat_po_editor import bl

def index(request):
    repositories = bl.repository.get_all()
    return (
        render_to_response(
            'fat_po_editor/index.html', {
                'repositories': repositories,
                },
            context_instance=RequestContext(request)))

def add(request):
    from django.contrib import messages
    from fat_po_editor.forms import RepositoryForm
    
    if request.method == 'POST':
        form = RepositoryForm(request.POST)
        if form.is_valid():
            try:
                bl.repository.add(form.cleaned_data["path"])
            except bl.repository.UserAlertError as e:
                messages.add_message(request, messages.ERROR, e.message)
                return HttpResponseRedirect(reverse(index))
        
        messages.add_message(request,
                             messages.SUCCESS,
                             (_(u'Repository path %(repository_path)s was added') % dict(
                                 repository_path=form.cleaned_data["path"])))
    return HttpResponseRedirect(reverse(index))

def path_autocomplete(request):
    from fat_po_editor.util.path import autocomplete
    
    path = request.GET.get('term', '')
    try:
        paths = autocomplete(path)
    except bl.repository.UserAlertError as e:
        return json_http_response_fail({
                    'message': e.message})
    return json_http_response(paths)