Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 346513328ba7f9f3ea4f1480c165c195c037ca93 @pydanny committed Sep 8, 2009
No changes.
@@ -0,0 +1,30 @@
+from django import forms
+from django.utils.translation import ugettext as _
+
+from wall.models import Post
+
+class WallForm(forms.ModelForm):
+ """Form for adding a new sample object
+
+ """
+ def __init__(self, user, group, *args, **kwargs):
+ self.user = user
+ self.group = group
+
+ super(SampleForm, self).__init__(*args, **kwargs)
+
+ def save(self, commit = True):
+ return super(SampleForm, self).save(commit)
+
+ class Meta:
+ model = Post
+ fields = ('title')
+
+ def clean(self):
+ self.check_group_membership()
+ return self.cleaned_data
+
+ def check_group_membership(self):
+ group = self.group
+ if group and not self.group.user_is_member(self.user):
+ raise forms.ValidationError(_("You must be a member to create wall entries"))
@@ -0,0 +1,33 @@
+""" Sample model for group aware projects """
+
+from django.db import models
+
+from django.contrib.contenttypes import generic
+from django.contrib.contenttypes.models import ContentType
+
+
+class Post(models.Model):
+
+ # Sample text fields below
+ title = models.CharField(max_length=140)
+
+ # The following three fields are required for being group aware
+ object_id = models.IntegerField(null=True)
+ content_type = models.ForeignKey(ContentType, null=True)
+ group = generic.GenericForeignKey("object_id", "content_type")
+
+ class Meta:
+ verbose_name_plural = "Post"
+
+ def __unicode__(self):
+ return self.title
+
+ # permalink/get_absolute_url
+ @models.permalink
+ def get_absolute_url(self, group = None):
+ kwargs = {"id": self.id}
+ if group:
+ return group.content_bridge.reverse("post_detail", group, kwargs)
+ return reverse("post_detail", kwargs = kwargs)
+
+
No changes.
No changes.
No changes.
17 urls.py
@@ -0,0 +1,17 @@
+""" Sample urls file for group aware projects """
+
+from django.conf.urls.defaults import *
+
+from wall.models import post
+
+urlpatterns = patterns("",
+ url(r'^create/$', 'wall.views.add', name="wall_add"),
+ url(r"^$", "wall.views.list", name="wall_list"),
+ url(r"^(?P<id>[-\w]+)/$", "wall.views.detail", name="wall_detail"),
+)
+
+
+# stick this into your groups/tribes urls.py:
+
+urlpatterns += bridge.include_urls('sample_group_aware_app.urls',
+ r'^tribe/(?P<group_slug>[-\w]+)/samples/')
129 views.py
@@ -0,0 +1,129 @@
+""" Sample view for group aware projects """
+
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+from django.contrib.auth.decorators import login_required
+from django.http import Http404, HttpResponseRedirect
+from django.shortcuts import render_to_response, get_object_or_404
+from django.template import RequestContext
+
+from uni_form.helpers import FormHelper, Submit, Reset
+
+from wall.models import Post
+from wall.forms import WallForm
+
+@login_required
+def add(request, group_slug = None, bridge = None,
+ form_class=WallForm,
+ template_name='sample_group_aware_app/sample_add.html'):
+
+ """Add a new repository to a group or to the whole list"""
+ if bridge:
+ try:
+ group = bridge.get_group(group_slug)
+ except ObjectDoesNotExist:
+ raise Http404
+ else:
+ group = None
+
+ if group:
+ group_base = bridge.group_base_template()
+ else:
+ group_base = None
+
+ if not request.user.is_authenticated():
+ is_member = False
+ else:
+ if group:
+ is_member = group.user_is_member(request.user)
+ else:
+ is_member = True
+
+ if request.method == "POST":
+ if request.user.is_authenticated():
+ form = form_class(request.user, group, request.POST)
+ if form.is_valid():
+ sample = form.save(commit = False)
+ if group:
+ group.associate(sample)
+ sample.save()
+ if group:
+ redirect_to = bridge.reverse("wall_list", group)
+ else:
+ redirect_to = reverse("wall_list")
+ return HttpResponseRedirect(redirect_to)
+ else:
+ form = form_class(request.user, group)
+
+ helper = FormHelper()
+ submit = Submit('submit','submit')
+ helper.add_input(submit)
+
+ return render_to_response(template_name, {
+ "group": group,
+ "is_member": is_member,
+ "form": form,
+ "group_base": group_base,
+ "helper": helper
+ }, context_instance = RequestContext(request))
+
+
+def sample_list(request, group_slug=None, bridge=None):
+
+ # If there is a bridge then get the group
+ if bridge is not None:
+ try:
+ group = bridge.get_group(group_slug)
+ except ObjectDoesNotExist:
+ raise Http404
+ else:
+ group = None
+
+ # If we have a group we fetch the sample from the group
+ if group:
+ samples = group.content_objects(SampleModel)
+ else:
+ samples = SampleModel.objects.all()
+
+ # check on user authentication or if user is member of a group
+ if not request.user.is_authenticated():
+ is_member = False
+ else:
+ is_member = group.user_is_member(request.user)
+
+ return render_to_response("sample_group_aware_app/sample_list.html", {
+ "group": group,
+ "samples": samples,
+ "is_member": is_member
+ }, context_instance=RequestContext(request))
+
+def sample_detail(request, slug, group_slug=None, bridge=None):
+
+ # If there is a bridge then get the group
+ if bridge is not None:
+ try:
+ group = bridge.get_group(group_slug)
+ except ObjectDoesNotExist:
+ raise Http404
+ else:
+ group = None
+
+ # If we have a group we fetch the sample from the group
+ #if group:
+ # samples = group.content_objects(SampleModel)
+ #else:
+ sample = get_object_or_404(SampleModel, slug=slug)
+
+ # check on user authentication or if user is member of a group
+ if not request.user.is_authenticated():
+ is_member = False
+ else:
+ is_member = group.user_is_member(request.user)
+
+ return render_to_response("sample_group_aware_app/sample_detail.html", {
+ "group": group,
+ "sample": sample,
+ "is_member": is_member
+ }, context_instance=RequestContext(request))
+
+

0 comments on commit 3465133

Please sign in to comment.