Commits

Javier Novoa C. committed cbd235f

added basic functionality for document management: add new documents, ready but still lacks validations

Comments (0)

Files changed (4)

+from markitup.widgets import MarkItUpWidget
+from django import forms
+
+# Docman form
+class DocManForm(forms.Form):
+    title = forms.CharField(label="Titulo")
+    text = forms.CharField(label="Texto", widget=MarkItUpWidget())

templates/docman/docmanpage_form.html

+<html>
+<head>
+  {{ form.media }}
+</head>
+<body>
+  {% if form.errors %}
+    <p class="errors">
+      Error en los datos! Por favor corrígelos...
+    </p>
+  {% endif %}
+
+  <form action="" method="post">
+    <table>
+      {{ form.as_table }}
+    </table>
+    <input type="submit" value="Enviar" />
+  </form>
+</body>
+</html>
     url(r'^_dump_fixtures/$', views.dump_relevent_fixtures, name='ucomment-dump-fixtures'),
 )
 
+# DocManager
+urlpatterns += patterns('',
+                        url(r'^_docman/$', views.docmanpage, name="ucomment-docman"),
+                        )
+
 if settings.DEBUG:
 
     urlpatterns += patterns('',
 # Settings for the ucomment application
 from conf import settings as conf
 
+from forms import DocManForm
+
 # Django and Jinja import imports
 from django import forms, template
 from django.shortcuts import render_to_response
                '<li><a href="%s">Table of Contents for your document</a>'
                '<li><a href="%s">The Django admin page for your site</a>'
                '<li><a href="%s">Publish or update the document</a>'
+               '<li><a href="%s">Add document</a>'
                '<li>Backup your application by <a href="%s">dumping objects '
                'to fixtures</a>') % \
                (django_reverse('ucomment-root'),
                 django_reverse('admin:index'),
                 django_reverse('ucomment-publish-update-document'),
+                django_reverse('ucomment-docman'),
                 django_reverse('ucomment-dump-fixtures'))
         return HttpResponse(msg, status=200)
     elif request.method == 'GET':
             # TODO(KGD): respond that user does not exist, or that password
             #            is incorrect;  redirect to log in page again.
             return HttpResponseRedirect(request.path)
+
+
+
+# Docman functions
+# ---------------------------------
+def docmanpage(request):
+    if request.method == "POST":
+        form = DocManForm(request.POST)
+        if form.is_valid():
+
+            # TODO: mejorar UI de formulario (y de paso de interfaz en _admin)
+
+            cd = form.cleaned_data
+            title = cd['title']
+            text = cd['text']
+
+            # Validar documento (que no haya errores en el RST)
+
+            # Sanitizar titulo
+            sanitized_title = title.replace(" ", "_")
+
+            # Verificar que no exista ya el archivo
+
+            # Agregar titulo a texto como titulo subrayado
+            text = title + "\n"+ "".join(["*" for i in range(len(title))]) + "\n\n" + text
+
+            # Guardar el documento en document_compile_area
+            added_fname = sanitized_title+".rst"
+            f = open(conf.local_repo_physical_dir + added_fname, "w")
+            f.write(text)
+            f.close()
+
+            # Agregar documento al contenido de document_compile_area/conf.master_doc
+            app = Sphinx(srcdir=conf.local_repo_physical_dir,
+                         confdir=conf.local_repo_physical_dir,
+                         outdir = '/tmp', doctreedir = '/tmp',
+                         buildername = 'pickle')
+            master_doc = app.env.config.master_doc + ".rst"
+
+            f = open(conf.local_repo_physical_dir + master_doc, "r")
+            lines = f.read()
+            f.close
+
+            lines = lines.split('\n')
+            idx = lines.index('.. toctree::') + 3
+            while idx < len(lines) and lines[idx] is not "":
+                idx = idx + 1
+            lines.insert(idx, '   ' + sanitized_title)
+            
+            f = open(conf.local_repo_physical_dir + master_doc, "w")
+            f.write("\n".join(lines))
+            f.close()
+
+            # Mandar cambios al repositorio
+            dvcs.add('', added_fname)
+            commit_to_repo_and_push("Documento agregado: " + sanitized_title)
+
+            # Publicar automaticamente
+            return HttpResponseRedirect(django_reverse('ucomment-publish-update-document'))
+    else:
+        form = DocManForm()
+
+    return render_to_response('docman/docmanpage_form.html', {'form': form})