Commits

Kevin Veroneau committed 6721549 Draft

Initial project import.

  • Participants

Comments (0)

Files changed (17)

+   Copyright 2013 Kevin Veroneau and Twitter Bootstrap team
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.

File djmongo/__init__.py

Empty file added.

File djmongo/db.py

+from pymongo.mongo_client import MongoClient
+from django.conf import settings
+
+client = MongoClient()
+
+try:
+    db_name = settings.MONGO_DATABASE
+except AttributeError:
+    db_name = 'django'
+
+db = client[db_name]

File djmongo/models.py

+from django.db import models
+
+# Create your models here.

File djmongo/templates/djmongo/base.html

+<html>
+  <head>
+    <title>Django Mongo test web Admin</title>
+  </head>
+  <body>
+    <h3>Django MongoDB Test Web Admin</h3><hr/>
+    {% block content %}{% endblock %}
+  </body>
+</html>

File djmongo/templates/djmongo/collection_list.html

+{% extends "djmongo/base.html" %}
+
+{% block content %}
+<strong>Collection List</strong><ul>
+  {% for collection in collection_list %}
+  {% if collection != 'system.indexes' %}
+  <li><a href="{% url 'collection_details' collection %}">{{collection}}</a></li>
+  {% endif %}
+  {% empty %}
+  <li>No collections available in database selected.</li>
+  {% endfor %}
+</ul>
+<form action="{% url 'collection_create' %}" method="post">{% csrf_token %}
+<input type="text" name="collection"/>
+<input type="submit" value="Create Collection"/></form>
+{% endblock %}

File djmongo/templates/djmongo/fragments/document_form.html

+{% load mongo_tags %}{% for field in field_list %}
+<tr><th>{{field.label}}</th><td>{{field|form_field}}</td></tr>
+{% endfor %}

File djmongo/templates/djmongo/fragments/document_table.html

+{% for field in field_list %}
+<tr><th>{{field.label}}</th><td>{{field.value}}</td></tr>
+{% endfor %}

File djmongo/templates/djmongo/json_form.html

+{% extends "djmongo/base.html" %}
+
+{% block content %}
+<strong>JSON Object Form</strong><br/>
+<form action="{{action}}" method="post">{% csrf_token %}
+<textarea name="json_data" rows="20" cols="80">{{json_data}}</textarea><br/>
+<input type="submit" value="Save"/>
+</form>
+{% endblock %}

File djmongo/templates/djmongo/update_document.html

+{% extends "djmongo/base.html" %}
+{% load mongo_tags %}
+
+{% block content %}
+<form action="{{action}}" method="post">{% csrf_token %}
+<table class="table">
+{% as_form document %}
+</table>
+<input type="submit" value="Save Document"/></form>
+{% endblock %}

File djmongo/templates/djmongo/view_collection.html

+{% extends "djmongo/base.html" %}
+{% load mongo_tags %}
+
+{% block content %}
+<strong>Viewing collection: {{collection}}:</strong><br/>
+<a href="{% url 'json_create' collection %}">Create Document</a><ul>
+{% for doc in document_list %}
+<li><a href="{% url 'document_details' collection doc|object_id %}">{{doc|object_name}}</a></li>
+{% endfor %}
+</ul>
+{% endblock %}

File djmongo/templates/djmongo/view_document.html

+{% extends "djmongo/base.html" %}
+{% load mongo_tags %}
+
+{% block content %}
+<a href="{% url 'collection_details' collection %}">Back to collection...</a>
+<table class="table">
+{% as_table document %}
+</table>
+{% with document|object_id as object_id %}
+<a href="{% url 'json_update' collection object_id %}">Edit JSON</a> | 
+<a href="{% url 'document_update' collection object_id %}">Update Document</a>
+{% endwith %} 
+{% endblock %}

File djmongo/templatetags/__init__.py

Empty file added.

File djmongo/templatetags/mongo_tags.py

+from django import template
+from djmongo.db import db
+from django.utils.safestring import mark_safe
+
+register = template.Library()
+
+@register.assignment_tag
+def mongo_json(collection):
+    data = db[collection]
+    return data
+
+@register.filter
+def object_id(obj):
+    try:
+        return u"%s" % obj['_id']
+    except KeyError:
+        return u"abc"
+
+@register.filter
+def object_name(obj):
+    if 'title' in obj:
+        return obj['title']
+    elif 'name' in obj:
+        return obj['name']
+    else:
+        return u"%s" % obj['_id']
+
+def get_field_list(obj):
+    field_list = []
+    for x in obj:
+        field_list.append({'label':x, 'value':obj[x]})
+    return field_list
+
+@register.inclusion_tag('djmongo/fragments/document_table.html')
+def as_table(obj):
+    return {'field_list':get_field_list(obj)}
+
+@register.inclusion_tag('djmongo/fragments/document_form.html')
+def as_form(obj):
+    return {'field_list':get_field_list(obj)}
+
+@register.filter
+def form_field(obj):
+    if isinstance(obj['value'], bool):
+        return mark_safe(u'<input type="checkbox" name="%s" value="true"%s/>' % (obj['label'], 'checked' if obj['value'] else ''))
+    if isinstance(obj['value'], int):
+        return mark_safe(u'<input type="text" name="%s" value="%s"/>' % (obj['label'], obj['value']))
+    else:
+        if len(obj['value']) > 40:
+            return mark_safe(u'<textarea name="%s" rows="20" cols="60">%s</textarea>' % (obj['label'], obj['value']))
+        else:
+            return mark_safe(u'<input type="text" name="%s" value="%s"/>' % (obj['label'], obj['value']))

File djmongo/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File djmongo/urls.py

+from django.conf.urls import patterns, url, include
+
+object_urls = patterns('djmongo.views',
+    url(r'^$', 'view_document', name='document_details'),
+    url(r'^JSON$', 'json_form', name='json_update'),
+    url(r'^Update$', 'update_document', name='document_update'),
+)
+
+collection_urls = patterns('djmongo.views',
+    url(r'^$', 'view_collection', name='collection_details'),
+    url(r'^NewDocument$', 'json_form', name='json_create'),
+    url(r'^(?P<object_id>[0-9a-f]+)/', include(object_urls)),
+)
+
+urlpatterns = patterns('djmongo.views',
+    url(r'^$', 'collection_list'),
+    url(r'^NewCollection$', 'new_collection', name='collection_create'),
+    url(r'^(?P<collection>[\w\.-]+)/', include(collection_urls)),
+)

File djmongo/views.py

+from djmongo.db import db
+from django.shortcuts import render, redirect
+from bson.objectid import ObjectId
+from django.core.urlresolvers import reverse
+import json
+
+def collection_list(req):
+    return render(req, 'djmongo/collection_list.html', {'collection_list':db.collection_names()})
+
+def view_collection(req, collection):
+    return render(req, 'djmongo/view_collection.html', {'collection':collection,
+                                                        'document_list':db[collection].find()})
+
+def view_document(req, collection, object_id):
+    oid = ObjectId(object_id)
+    return render(req, 'djmongo/view_document.html', {'collection':collection,
+                                                      'document':db[collection].find_one({'_id':oid})})
+def json_form(req, collection, object_id=None):
+    if object_id:
+        oid = ObjectId(object_id)
+        doc = db[collection].find_one({'_id':oid})
+        if req.method == 'POST':
+            doc = json.loads(req.POST.get('json_data'))
+            doc.update({'_id':oid})
+            db[collection].save(doc)
+            return redirect(view_document, collection, object_id)
+        doc.pop('_id')
+        json_data = json.dumps(doc)
+        action = reverse('json_update', args=(collection, object_id))
+    else:
+        if req.method == 'POST':
+            doc = json.loads(req.POST.get('json_data'))
+            oid = db[collection].insert(doc)
+            return redirect(view_document, collection, '%s' % oid)
+        oid = None
+        json_data = ''
+        action = reverse('json_create', args=(collection,))
+    return render(req, 'djmongo/json_form.html', {'json_data':json_data,
+                                                  'action':action})
+
+def update_document(req, collection, object_id):
+    oid = ObjectId(object_id)
+    doc = db[collection].find_one({'_id':oid})
+    if req.method == 'POST':
+        for x in doc:
+            if isinstance(doc[x], bool):
+                doc[x] = True if x in req.POST else False
+            elif isinstance(doc[x], int):
+                data = req.POST.get(x,0)
+                if data == '':
+                    data = 0
+                doc[x] = int(data)
+            elif isinstance(doc[x], str) or isinstance(doc[x], unicode):
+                doc[x] = u"%s" % req.POST.get(x,'')
+        db[collection].save(doc)
+        return redirect(view_document, collection, object_id)
+    doc.pop('_id')
+    action = reverse(update_document, args=(collection, object_id))
+    return render(req, 'djmongo/update_document.html', {'collection':collection,
+                                                        'action':action,
+                                                        'document':doc})
+
+def new_collection(req):
+    return redirect(view_collection, req.POST.get('collection'))