# -*- coding: utf-8 -*-
# vim: set ts=4

from django.core.exceptions import PermissionDenied
from django.forms import ModelForm
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt

from Artifactorial.models import Artifact, Directory


def index(request):
    artifacts = Artifact.objects.all()

    return render_to_response('Artifactorial/index.html',
                              {'artifacts': artifacts},
                              context_instance=RequestContext(request))


class ArtifactForm(ModelForm):
    class Meta:
        model = Artifact
        fields = ('path', 'directory', 'is_permanent')


@csrf_exempt
def post(request):
    if request.method == 'POST':
        # Find the directory by name
        directory_path = request.POST.get('directory', '')
        directory_id = get_object_or_404(Directory, path=directory_path)
        request.POST['directory'] = directory_id.id

        # TODO: validate the user, group or public rights
        # Validate the updated form
        form = ArtifactForm(request.POST, request.FILES)
        if form.is_valid():
            artifact = form.save()
            return HttpResponse(artifact.path.url, content_type='text/plain')
        else:
            raise PermissionDenied
    else:
        raise PermissionDenied

@@ -34,16 +34,47 @@
 t ts=4%0A%0A
+from django.db.models import Q%0A
 from dja
@@ -175,16 +175,25 @@
 p import
+ Http404,
  HttpRes
@@ -416,16 +416,27 @@
 ectory%0A%0A
+import os%0A%0A
 %0Adef ind
@@ -1454,28 +1454,1586 @@
      raise PermissionDenied%0A
+%0A%0Adef get(request, filename):%0A    data=''%0A    # Is it a file or a path%0A    if filename%5B-1%5D == '/':%0A        dirname = os.path.dirname(filename)%0A        directories = Directory.objects.filter(Q(path__startswith=%22%25s%22 %25 (dirname)) %7C Q(path=dirname))%0A        if not directories:%0A            # TODO: Look for pseudo directories with files%0A            raise Http404%0A%0A        dirname_length = len(dirname)%0A        dir_set = set()%0A        for directory in directories:%0A            if directory.path != dirname:%0A                # Sub directory =%3E print the next elements in the path%0A                full_dir_name = directory.path%5Bdirname_length+1:%5D%0A                try:%0A                    index = full_dir_name.index('/')%0A                    dir_set.add(full_dir_name%5B:index%5D)%0A                except Exception:%0A                    dir_set.add(full_dir_name)%0A%0A        artifacts = Artifact.objects.filter(path__startswith=filename.lstrip('/'))%0A        art_set = set()%0A        for artifact in artifacts:%0A            # TODO: handle pseudo directories%0A            art_set.add(artifact.path.name%5Bdirname_length:%5D)%0A%0A        return render_to_response('Artifactorial/list.html',%0A                                  %7B'directory': dirname, 'directories': dir_set,%0A                                   'files': art_set%7D,%0A                                  context_instance=RequestContext(request))%0A    else:%0A        artifact = get_object_or_404(Artifact, path=filename.lstrip('/'))%0A        data = %22%25s (%25d)%22 %25 (artifact.path.name, artifact.path.size)%0A        return HttpResponse(data)%0A

Add directory listing