Commits

funkybob  committed b198c10

Reorganised for shared namespace

  • Participants
  • Parent commits 2be910a

Comments (0)

Files changed (15)

File docs/INSTALL

 
     INSTALLED_APPS = [
         ...
-        'gnocchi_ask',
+        'gnocchi.ask',
         'django-taggit',
     ]
 

File gnocchi/__init__.py

+__import__('pkg_resources').declare_namespace(__name__)
+

File gnocchi/ask/__init__.py

Empty file added.

File gnocchi/ask/admin.py

+from django.contrib import admin
+
+from gnocchi.ask import models
+
+class QuestionAdmin(admin.ModelAdmin):
+    list_display = ('question', 'user', 'created', 'accepted', 'visible',)
+    list_filter = ('user', 'visible',)
+    search_fields = ('question', 'content',)
+
+admin.site.register(models.Question, QuestionAdmin)
+admin.site.register(models.Response)
+
+class VoteAdmin(admin.ModelAdmin):
+    list_display = ('response', 'user', 'weight',)
+    list_filter = ('user',)
+admin.site.register(models.Vote, VoteAdmin)

File gnocchi/ask/forms.py

+from django import forms
+from gnocchi.ask import models
+
+class AskQuestionForm(forms.ModelForm):
+
+    class Meta:
+        model = models.Question
+        exclude = ('user', 'created', 'accepted', 'visible',)
+
+class AddResponseForm(forms.ModelForm):
+
+    class Meta:
+        model = models.Response
+        fields = ('content',)

File gnocchi/ask/models.py

+from django.db import models
+from django.core.urlresolvers import reverse
+
+from taggit.managers import TaggableManager
+
+from datetime import datetime
+
+class VisibleManager(models.Manager):
+    def visible(self):
+        return self.get_query_set().filter(visible=True)
+
+class Question(models.Model):
+    user = models.ForeignKey('auth.User', related_name='questions_asked')
+    question = models.CharField(max_length=1024)
+    content = models.TextField()
+
+    created = models.DateTimeField(default=datetime.now)
+    accepted = models.ForeignKey('Response', null=True, blank=True, related_name='accepted_for')
+
+    visible = models.BooleanField(default=True)
+
+    tags = TaggableManager(blank=True)
+
+    objects = VisibleManager()
+
+    class Meta:
+        ordering = ('-created',)
+    def __unicode__(self):
+        return self.question
+    def get_absolute_url(self):
+        return reverse('question-detail', kwargs={'question_id': self.pk})
+    def votes(self):
+        return Vote.objects.filter(response__question=self).count()
+
+class ResponseManager(VisibleManager):
+    def get_query_set(self):
+        qset = super(ResponseManager, self).get_query_set()
+        return qset.annotate(
+            score=models.Sum('votes__weight')
+        ).order_by('-score', 'created')
+
+class Response(models.Model):
+    user = models.ForeignKey('auth.User', related_name='question_responses')
+    question = models.ForeignKey(Question, related_name='responses')
+    created = models.DateTimeField(default=datetime.now)
+
+    content = models.TextField()
+    visible = models.BooleanField(default=True)
+    objects = ResponseManager()
+
+    class Meta:
+        permissions = (
+            ('can_vote', 'Can vote on responses'),
+        )
+    def vote_up_url(self):
+        return reverse('question-vote', kwargs={
+            'question_id': self.question.id,
+            'response_id': self.id,
+            'direction': 'up',
+        })
+    def vote_down_url(self):
+        return reverse('question-vote', kwargs={
+            'question_id': self.question.id,
+            'response_id': self.id,
+            'direction': 'down',
+        })
+    def get_accept_url(self):
+        return reverse('response-accept', kwargs={
+            'question_id': self.question.id,
+            'response_id': self.id,
+        })
+    def score(self):
+        return self.vote_set.aggregate(score=models.Sum('weight'))['score']
+
+class Vote(models.Model):
+    user = models.ForeignKey('auth.User', related_name='response_votes')
+    response = models.ForeignKey(Response, related_name='votes')
+    weight = models.IntegerField(default=1)
+
+    class Meta:
+        unique_together = (
+            ('user', 'response'),
+        )

File gnocchi/ask/urls.py

+from django.conf.urls.defaults import *
+
+question_patterns = patterns('gnocchi.ask.views',
+    url(r'^$', 'question_detail', name='question-detail'),
+    url(r'^response/$', 'question_response', name='question-response'),
+    url(r'^accept/(?P<response_id>\d+)/$', 'response_accept', name='response-accept'),
+    url(r'^vote/(?P<response_id>\d+)/(?P<direction>up|down)/$', 'question_vote', name='question-vote'),
+)
+
+urlpatterns = patterns('gnocchi.ask.views',
+    (r'^$', 'question_list'),
+    url(r'^ask/$', 'question_ask', name='question-ask'),
+    (r'^question/(?P<question_id>\d)/', include(question_patterns)),
+)

File gnocchi/ask/views.py

+from django.views.generic.simple import direct_to_template
+from django.views.generic.list_detail import object_list, object_detail
+from django.shortcuts import get_object_or_404
+from django.http import HttpResponse, HttpResponseRedirect
+from django.conf import settings
+from django.contrib import messages
+from django.contrib.auth.decorators import permission_required
+
+from gnocchi.ask import models, forms
+
+QUESTION_PAGE = getattr(settings, 'QUESTION_PAGE', 10)
+
+def question_list(request):
+    return object_list(request,
+        queryset=models.Question.objects.all(),
+        paginate_by=QUESTION_PAGE,
+    )
+
+def question_detail(request, question_id):
+    return object_detail(request,
+        queryset=models.Question.objects.filter(visible=True),
+        object_id=question_id,
+        extra_context={
+            'form': forms.AddResponseForm(),
+        }
+    )
+
+@permission_required('question.can_add')
+def question_ask(request):
+    if request.method == 'POST':
+        form = forms.AskQuestionForm(request.POST)
+        if form.is_valid():
+            obj = form.save(commit=False)
+            obj.user = request.user
+            obj.save()
+            form.save_m2m()
+            return HttpResponseRedirect(obj.get_absolute_url())
+    else:
+        form = forms.AskQuestionForm()
+    return direct_to_template(request,
+        'gnocchi_ask/question_ask.html',
+        extra_context={
+            'form': form,
+        }
+    )
+
+@permission_required('response.can_add')
+def question_response(request, question_id):
+    question = get_object_or_404(models.Question, pk=question_id)
+
+    if request.method == 'POST':
+        form = forms.AddResponseForm(request.POST)
+        if form.is_valid():
+            obj = form.save(commit=False)
+            obj.user = request.user
+            obj.question = question
+            obj.save()
+            return HttpResponseRedirect(question.get_absolute_url())
+    else:
+        form = forms.AddResponseForm()
+    # Verify question is still 'open'
+    return direct_to_template(request,
+        'gnocchi_ask/response.html',
+        extra_context={
+            'question': question,
+            'form': form,
+        }
+    )
+
+DIRECTION_WEIGHT = {
+    'up': 1,
+    'down': -1,
+}
+
+@permission_required('response.can_vote')
+def question_vote(request, question_id, response_id, direction, weight=1):
+    response = get_object_or_404(models.Response, pk=response_id,
+        question__id=question_id)
+
+    weight = DIRECTION_WEIGHT[direction] * weight
+
+    vote, created = models.Vote.objects.get_or_create(user=request.user,
+        response=response,
+        defaults={'weight': weight}
+    )
+
+    if not created:
+        if vote.weight == weight:
+            # Voted the same
+            pass
+        elif abs(vote.weight) == abs(weight):
+            # Voted opposite
+            vote.delete()
+        else:
+            vote.weight = weight
+            vote.save()
+
+    return HttpResponseRedirect(response.question.get_absolute_url())
+
+def response_accept(request, question_id, response_id):
+    response = get_object_or_404(models.Response, pk=response_id,
+        question__id=question_id)
+
+    if response.question.user == request.user:
+        response.question.accepted = response
+        response.question.save()
+    else:
+        messages.add_message(request, messages.WARNING,
+            "You may not accept a response for a question you didn't ask."
+        )
+    return HttpResponseRedirect(response.question.get_absolute_url())

File gnocchi_ask/__init__.py

Empty file removed.

File gnocchi_ask/admin.py

-from django.contrib import admin
-
-from gnocchi_ask import models
-
-class QuestionAdmin(admin.ModelAdmin):
-    list_display = ('question', 'user', 'created', 'accepted', 'visible',)
-    list_filter = ('user', 'visible',)
-    search_fields = ('question', 'content',)
-
-admin.site.register(models.Question, QuestionAdmin)
-admin.site.register(models.Response)
-
-class VoteAdmin(admin.ModelAdmin):
-    list_display = ('response', 'user', 'weight',)
-    list_filter = ('user',)
-admin.site.register(models.Vote, VoteAdmin)

File gnocchi_ask/forms.py

-from django import forms
-from gnocchi_ask import models
-
-class AskQuestionForm(forms.ModelForm):
-
-    class Meta:
-        model = models.Question
-        exclude = ('user', 'created', 'accepted', 'visible',)
-
-class AddResponseForm(forms.ModelForm):
-
-    class Meta:
-        model = models.Response
-        fields = ('content',)

File gnocchi_ask/models.py

-from django.db import models
-from django.core.urlresolvers import reverse
-
-from taggit.managers import TaggableManager
-
-from datetime import datetime
-
-class VisibleManager(models.Manager):
-    def visible(self):
-        return self.get_query_set().filter(visible=True)
-
-class Question(models.Model):
-    user = models.ForeignKey('auth.User', related_name='questions_asked')
-    question = models.CharField(max_length=1024)
-    content = models.TextField()
-
-    created = models.DateTimeField(default=datetime.now)
-    accepted = models.ForeignKey('Response', null=True, blank=True, related_name='accepted_for')
-
-    visible = models.BooleanField(default=True)
-
-    tags = TaggableManager(blank=True)
-
-    objects = VisibleManager()
-
-    class Meta:
-        ordering = ('-created',)
-    def __unicode__(self):
-        return self.question
-    def get_absolute_url(self):
-        return reverse('question-detail', kwargs={'question_id': self.pk})
-    def votes(self):
-        return Vote.objects.filter(response__question=self).count()
-
-class ResponseManager(VisibleManager):
-    def get_query_set(self):
-        qset = super(ResponseManager, self).get_query_set()
-        return qset.annotate(
-            score=models.Sum('votes__weight')
-        ).order_by('-score', 'created')
-
-class Response(models.Model):
-    user = models.ForeignKey('auth.User', related_name='question_responses')
-    question = models.ForeignKey(Question, related_name='responses')
-    created = models.DateTimeField(default=datetime.now)
-
-    content = models.TextField()
-    visible = models.BooleanField(default=True)
-    objects = ResponseManager()
-
-    class Meta:
-        permissions = (
-            ('can_vote', 'Can vote on responses'),
-        )
-    def vote_up_url(self):
-        return reverse('question-vote', kwargs={
-            'question_id': self.question.id,
-            'response_id': self.id,
-            'direction': 'up',
-        })
-    def vote_down_url(self):
-        return reverse('question-vote', kwargs={
-            'question_id': self.question.id,
-            'response_id': self.id,
-            'direction': 'down',
-        })
-    def get_accept_url(self):
-        return reverse('response-accept', kwargs={
-            'question_id': self.question.id,
-            'response_id': self.id,
-        })
-    def score(self):
-        return self.vote_set.aggregate(score=models.Sum('weight'))['score']
-
-class Vote(models.Model):
-    user = models.ForeignKey('auth.User', related_name='response_votes')
-    response = models.ForeignKey(Response, related_name='votes')
-    weight = models.IntegerField(default=1)
-
-    class Meta:
-        unique_together = (
-            ('user', 'response'),
-        )

File gnocchi_ask/urls.py

-from django.conf.urls.defaults import *
-
-question_patterns = patterns('gnocchi_ask.views',
-    url(r'^$', 'question_detail', name='question-detail'),
-    url(r'^response/$', 'question_response', name='question-response'),
-    url(r'^accept/(?P<response_id>\d+)/$', 'response_accept', name='response-accept'),
-    url(r'^vote/(?P<response_id>\d+)/(?P<direction>up|down)/$', 'question_vote', name='question-vote'),
-)
-
-urlpatterns = patterns('gnocchi_ask.views',
-    (r'^$', 'question_list'),
-    url(r'^ask/$', 'question_ask', name='question-ask'),
-    (r'^question/(?P<question_id>\d)/', include(question_patterns)),
-)

File gnocchi_ask/views.py

-from django.views.generic.simple import direct_to_template
-from django.views.generic.list_detail import object_list, object_detail
-from django.shortcuts import get_object_or_404
-from django.http import HttpResponse, HttpResponseRedirect
-from django.conf import settings
-from django.contrib import messages
-from django.contrib.auth.decorators import permission_required
-
-from gnocchi_ask import models, forms
-
-QUESTION_PAGE = getattr(settings, 'QUESTION_PAGE', 10)
-
-def question_list(request):
-    return object_list(request,
-        queryset=models.Question.objects.all(),
-        paginate_by=QUESTION_PAGE,
-    )
-
-def question_detail(request, question_id):
-    return object_detail(request,
-        queryset=models.Question.objects.filter(visible=True),
-        object_id=question_id,
-        extra_context={
-            'form': forms.AddResponseForm(),
-        }
-    )
-
-@permission_required('question.can_add')
-def question_ask(request):
-    if request.method == 'POST':
-        form = forms.AskQuestionForm(request.POST)
-        if form.is_valid():
-            obj = form.save(commit=False)
-            obj.user = request.user
-            obj.save()
-            form.save_m2m()
-            return HttpResponseRedirect(obj.get_absolute_url())
-    else:
-        form = forms.AskQuestionForm()
-    return direct_to_template(request,
-        'gnocchi_ask/question_ask.html',
-        extra_context={
-            'form': form,
-        }
-    )
-
-@permission_required('response.can_add')
-def question_response(request, question_id):
-    question = get_object_or_404(models.Question, pk=question_id)
-
-    if request.method == 'POST':
-        form = forms.AddResponseForm(request.POST)
-        if form.is_valid():
-            obj = form.save(commit=False)
-            obj.user = request.user
-            obj.question = question
-            obj.save()
-            return HttpResponseRedirect(question.get_absolute_url())
-    else:
-        form = forms.AddResponseForm()
-    # Verify question is still 'open'
-    return direct_to_template(request,
-        'gnocchi_ask/response.html',
-        extra_context={
-            'question': question,
-            'form': form,
-        }
-    )
-
-DIRECTION_WEIGHT = {
-    'up': 1,
-    'down': -1,
-}
-
-@permission_required('response.can_vote')
-def question_vote(request, question_id, response_id, direction, weight=1):
-    response = get_object_or_404(models.Response, pk=response_id,
-        question__id=question_id)
-
-    weight = DIRECTION_WEIGHT[direction] * weight
-
-    vote, created = models.Vote.objects.get_or_create(user=request.user,
-        response=response,
-        defaults={'weight': weight}
-    )
-
-    if not created:
-        if vote.weight == weight:
-            # Voted the same
-            pass
-        elif abs(vote.weight) == abs(weight):
-            # Voted opposite
-            vote.delete()
-        else:
-            vote.weight = weight
-            vote.save()
-
-    return HttpResponseRedirect(response.question.get_absolute_url())
-
-def response_accept(request, question_id, response_id):
-    response = get_object_or_404(models.Response, pk=response_id,
-        question__id=question_id)
-
-    if response.question.user == request.user:
-        response.question.accepted = response
-        response.question.save()
-    else:
-        messages.add_message(request, messages.WARNING,
-            "You may not accept a response for a question you didn't ask."
-        )
-    return HttpResponseRedirect(response.question.get_absolute_url())
 from setuptools import setup, find_packages
 
 setup( name='gnocchi-ask',
-    version = '0.1',
+    version = '0.2',
     description = 'A simple StackOverflow/AskBot style application',
     author = 'Curtis Maloney',
     author_email = 'curtis@tinbrain.net',
     url = 'http://bitbucket.org/funkybob/gnocchi-ask/',
     keywords = ['django',],
     packages = find_packages(),
+    namespace_packages = ['gnocchi',],
     zip_safe = False,
     classifiers = [
         'Environment :: Web Environment',