Permalink
Browse files

first example project added (blog example); some tiny changes in mong…

…oforms (styling, added asserts). added .gitignore.
  • Loading branch information...
flosch authored and stephrdev committed May 1, 2010
1 parent bc65868 commit 1e58f6354a50b0fc9bbf06dfe8423cb8a94b33e3
View
@@ -0,0 +1,4 @@
+secret.txt
+*.db
+*.pyc
+.DS_Store
No changes.
No changes.
No changes.
@@ -0,0 +1,9 @@
+# -*- coding: utf-8 -*-
+
+from mongoforms import MongoForm
+from models import BlogPost
+
+class BlogPostForm(MongoForm):
+ class Meta:
+ document = BlogPost
+ fields = ('author', 'title', 'content', 'published')
@@ -0,0 +1,43 @@
+import datetime
+
+from django.template.defaultfilters import slugify
+from django.core.urlresolvers import reverse
+
+from mongoengine import *
+
+class BlogPost(Document):
+ published = BooleanField(default=False)
+ author = StringField(required=True)
+ title = StringField(required=True)
+ slug = StringField()
+ content = StringField(required=True)
+
+ datetime_added = DateTimeField(default=datetime.datetime.now)
+
+ def save(self):
+ if self.slug is None:
+ slug = slugify(self.title)
+ new_slug = slug
+ c = 1
+ while True:
+ try:
+ BlogPost.objects.get(slug=new_slug)
+ except BlogPost.DoesNotExist:
+ break
+ else:
+ c += 1
+ new_slug = '%s-%s' % (slug, c)
+ self.slug = new_slug
+ return super(BlogPost, self).save()
+
+ def get_absolute_url(self):
+ #return u'%s/' % self.slug
+ return reverse('apps.blog.views.show', kwargs={'slug': self.slug})
+
+ @queryset_manager
+ def published_posts(doc_cls, queryset):
+ return queryset(published=True)
+
+ meta = {
+ 'ordering': ['-datetime_added']
+ }
@@ -0,0 +1 @@
+{% extends "base.html" %}
@@ -0,0 +1,9 @@
+{% extends "blog/base.html" %}
+
+{% block body %}
+ <a href="/new/">New post</a>
+
+ {% for post in posts %}
+ {% include "blog/post.html" %}
+ {% endfor %}
+{% endblock %}
@@ -0,0 +1,20 @@
+{% extends "blog/base.html" %}
+
+{% block header %}
+ <style type="text/css" media="screen">
+ form label {
+ display: block;
+ font-weight: bold;
+ }
+ </style>
+{% endblock %}
+
+{% block body %}
+ <a href="/">Back to index</a>
+
+ <form method="post" action=".">
+ {{ form.as_p }}
+
+ <input type="submit" value="Submit" id="" />
+ </form>
+{% endblock %}
@@ -0,0 +1,17 @@
+<div class="post
+ {% if not post.published %}
+ not_published
+ {% endif %}">
+ <h1 class="title">
+ <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
+ <span style="font-size: 50%">
+ {% if not post.published %}
+ <em>(unpublished)</em>
+ {% endif %}
+ <a href="{{ post.get_absolute_url }}edit/">edit</a>
+ <a href="{{ post.get_absolute_url }}delete/">delete</a>
+ </h1>
+ <span class="author">by {{ post.author }}</span>
+ <p class="content">{{ post.content }}</p>
+ <span class="added">{{ post.datetime_added }}</span>
+</div>
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+
+{% block body %}
+ <a href="/">Back to index</a>
+
+ {% include "blog/post.html" %}
+{% endblock %}
+
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+
+from django.conf.urls.defaults import *
+from django.views.generic.simple import redirect_to, direct_to_template
+
+entry_pattern = patterns('apps.blog.views',
+ (r'^$', 'show'),
+ (r'^edit/$', 'edit'),
+ (r'^delete/$', 'delete'),
+)
+
+urlpatterns = patterns('apps.blog.views',
+ (r'^$', 'index'),
+ (r'^new/$', 'new'),
+ (r'^(?P<slug>[\w\-]+)/', include(entry_pattern)),
+)
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.http import HttpResponseRedirect
+
+from models import BlogPost
+from forms import BlogPostForm
+
+def index(request, slug=None, template_name='blog/index.html'):
+ posts = BlogPost.objects[:5]
+ template_context = {'posts': posts}
+ #print posts[0].get_absolute_url()
+ return render_to_response(
+ template_name,
+ template_context,
+ RequestContext(request)
+ )
+
+def show(request, slug, template_name='blog/show.html'):
+ post = BlogPost.objects.get(slug=slug)
+ template_context = {'post': post}
+
+ return render_to_response(
+ template_name,
+ template_context,
+ RequestContext(request)
+ )
+
+def new(request, template_name='blog/new_or_edit.html'):
+ if request.method == 'POST':
+ form = BlogPostForm(request.POST)
+ if form.is_valid():
+ form.save()
+ return HttpResponseRedirect("/")
+ else:
+ form = BlogPostForm()
+
+ template_context = {'form': form}
+
+ return render_to_response(
+ template_name,
+ template_context,
+ RequestContext(request)
+ )
+
+def delete(request, slug):
+ post = BlogPost.objects(slug=slug)
+ post.delete()
+ return HttpResponseRedirect("/")
+
+def edit(request, slug, template_name='blog/new_or_edit.html'):
+
+ post = BlogPost.objects.get(slug=slug)
+ if request.method == 'POST':
+ form = BlogPostForm(request.POST, instance=post)
+ if form.is_valid():
+ form.save()
+ return HttpResponseRedirect(post.get_absolute_url())
+ else:
+ form = BlogPostForm(instance=post)
+
+ template_context = {'form': form}
+
+ return render_to_response(
+ template_name,
+ template_context,
+ RequestContext(request)
+ )
View
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+from django.core.management import execute_manager
+try:
+ import settings # Assumed to be in the same directory.
+except ImportError:
+ import sys
+ sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+ sys.exit(1)
+
+if __name__ == "__main__":
+ execute_manager(settings)
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+
+import platform
+import sys
+import os
+
+PROJECT_ROOT = os.path.dirname(__file__)
+sys.path.append(os.path.join(PROJECT_ROOT, '../../../'))
+from mongoengine import connect
+
+connect('mongoforms_test')
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+sys.path.append(os.path.join(PROJECT_ROOT, '../../'))
+
+MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'site_media')
+TEMPLATE_DIRS = [os.path.join(PROJECT_ROOT, 'templates')]
+ADMIN_MEDIA_PREFIX = '/media/'
+ROOT_URLCONF = 'blogprj.urls'
+TIME_ZONE = 'Europe/Berlin'
+LANGUAGE_CODE = 'de-de'
+
+TEMPLATE_LOADERS = (
+ 'django.template.loaders.filesystem.Loader',
+ 'django.template.loaders.app_directories.Loader',
+)
+
+INSTALLED_APPS = (
+ 'apps.blog',
+)
+
+MIDDLEWARE_CLASSES = (
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+)
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+ "django.core.context_processors.request",
+ "django.core.context_processors.auth",
+ "django.core.context_processors.media",
+ "django.core.context_processors.debug",
+)
+
+try:
+ SECRET_KEY
+except NameError:
+ SECRET_FILE = os.path.join(PROJECT_ROOT, 'secret.txt')
+ try:
+ SECRET_KEY = open(SECRET_FILE).read().strip()
+ except IOError:
+ try:
+ from random import choice
+ SECRET_KEY = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])
+ secret = file(SECRET_FILE, 'w')
+ secret.write(SECRET_KEY)
+ secret.close()
+ except IOError:
+ Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE)
+
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+<head>
+ <title>My little blog</title>
+
+ {% block header %}{% endblock %}
+ <style type="text/css" media="screen">
+ .not_published {
+ color: #B4B4B4 !important;
+ }
+ </style>
+</head>
+<body>
+ {% block body %}Content goes here{% endblock %}
+</body>
+</html>
View
@@ -0,0 +1,5 @@
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+ (r'^', include('apps.blog.urls')),
+)
View
@@ -4,15 +4,16 @@ class MongoFormFieldGenerator(object):
"""This class generates Django form-fields for mongoengine-fields."""
def generate(self, field_name, field):
- """
- Tries to lookup a matching formfield generator (lowercase
+ """Tries to lookup a matching formfield generator (lowercase
field-classname) and raises a NotImplementedError of no generator
can be found.
"""
if hasattr(self, 'generate_%s' % field.__class__.__name__.lower()):
- return getattr(self, 'generate_%s' % field.__class__.__name__.lower())(field_name, field)
+ return getattr(self, 'generate_%s' % \
+ field.__class__.__name__.lower())(field_name, field)
else:
- raise NotImplementedError('%s is not supported by MongoForm' % field.__class__.__name__)
+ raise NotImplementedError('%s is not supported by MongoForm' % \
+ field.__class__.__name__)
def generate_stringfield(self, field_name, field):
if field.regex:
Oops, something went wrong.

0 comments on commit 1e58f63

Please sign in to comment.