Source

django-mongodb / djmongo / views.py

Full commit
from djmongo.db import db, db_name
from django.shortcuts import render, redirect
from bson.objectid import ObjectId
from django.core.urlresolvers import reverse
from djmongo.utils import json_dump
from utils import json_load
import datetime

def collection_list(req):
    collection_list = []
    for collection in db.collection_names():
        if collection != 'system.indexes':
            index_count = db.system.indexes.find({'ns':'%s.%s' % (db_name, collection)}).count()
            collection_list.append({'name':collection, 'count':db[collection].count(), 'indexes':index_count})
    return render(req, 'djmongo/collection_list.html', {'collections':collection_list})

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_load(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_dump(doc)
        action = reverse('json_update', args=(collection, object_id))
    else:
        if req.method == 'POST':
            doc = json_load(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,))
        doc = None
    return render(req, 'djmongo/json_form.html', {'collection':collection,
                                                  'document':doc,
                                                  'object_id':object_id,
                                                  '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], datetime.datetime):
                doc[x] = datetime.datetime.strptime(req.POST.get(x,''), "%Y-%m-%d %H:%M:%S.%f")
            elif isinstance(doc[x], list):
                doc[x] = req.POST.getlist(x,[])
            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,
                                                        'object_id':object_id,
                                                        'action':action,
                                                        'document':doc})

def new_collection(req):
    return redirect(view_collection, req.POST.get('collection'))

def view_indexes(req, collection):
    return render(req, 'djmongo/view_indexes.html', {'collection':collection,
                                                     'index_list':db.system.indexes.find({'ns':'%s.%s' % (db_name, collection)})})