Commits

Kenneth Love committed 0e5b148

added views for adding talk and viewing talks. yay for understanding more on class-based views

  • Participants
  • Parent commits d9dd2e5

Comments (0)

Files changed (9)

File lightning/common/forms.py

 
 from django.contrib.auth.models import User
 
-from uni_form.helpers import FormHelper, Submit, Reset
+from uni_form.helpers import FormHelper, Submit
+
+from common.models import Talk
 
 class ProfileForm(forms.ModelForm):
     class Meta:
         helper.form_action = ''
         helper.form_method = 'POST'
         return helper
+
+class TalkForm(forms.ModelForm):
+    class Meta:
+        model = Talk
+        exclude = ('user',)
+
+    def __init__(self, *args, **kwargs):
+        super(TalkForm, self).__init__(*args, **kwargs)
+        self.fields['url'].label = 'URL'
+
+    @property
+    def helper(self):
+        helper = FormHelper()
+        submit = Submit('save', 'Save', **{'css_class': 'primary'})
+        helper.add_input(submit)
+        helper.form_action = ''
+        helper.form_method = 'POST'
+        return helper

File lightning/common/migrations/0001_initial.py

+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding model 'Talk'
+        db.create_table('common_talk', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('created_at', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
+            ('updated_at', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
+            ('url', self.gf('django.db.models.fields.URLField')(max_length=200)),
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=1024)),
+            ('description', self.gf('django.db.models.fields.TextField')()),
+            ('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='talks', to=orm['auth.User'])),
+        ))
+        db.send_create_signal('common', ['Talk'])
+
+
+    def backwards(self, orm):
+        
+        # Deleting model 'Talk'
+        db.delete_table('common_talk')
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'common.talk': {
+            'Meta': {'object_name': 'Talk'},
+            'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'description': ('django.db.models.fields.TextField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'updated_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'url': ('django.db.models.fields.URLField', [], {'max_length': '200'}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'talks'", 'to': "orm['auth.User']"})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        }
+    }
+
+    complete_apps = ['common']

File lightning/common/migrations/__init__.py

Empty file added.

File lightning/common/views.py

 from django.contrib.auth import logout
 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
-from django.views.generic import TemplateView
+from django.views.generic import TemplateView, CreateView
+from django.views.generic import ListView
 from django.contrib.auth.decorators import login_required
 from django.utils.decorators import method_decorator
 
-from common.forms import ProfileForm
+from common.forms import ProfileForm, TalkForm
+from common.models import Talk
 
 def logout_view(request):
     logout(request)
             return HttpResponseRedirect(reverse('profile'))
 
         return self.render_to_response({'form': form})
+
+
+class TalksView(ListView):
+    template_name = 'common/talks.html'
+    model = Talk
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(TalksView, self).dispatch(*args, **kwargs)
+
+    def get_queryset(self):
+        queryset = super(TalksView, self).get_queryset()
+        queryset = queryset.filter(user=self.request.user)
+        return queryset
+
+
+class CreateTalkView(CreateView):
+    template_name = 'common/create_talk.html'
+    form_class = TalkForm
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(CreateTalkView, self).dispatch(*args, **kwargs)
+
+    def post(self, request):
+        form = TalkForm(request.POST)
+
+        if form.is_valid():
+            talk = form.save(commit=False)
+            talk.user = request.user
+            talk.save()
+
+            return HttpResponseRedirect(reverse('profile'))
+
+        return super(CreateTalkView, self).post(request)
+

File lightning/settings/base.py

 )
 
 MY_APPS = (
+    'common',
 )
 
 INSTALLED_APPS = THIRD_PARTY_APPS + MY_APPS

File lightning/templates/_layouts/base.html

 		<div class="topbar-inner">
 			<div class="container">
 				<h3><a href="{% url home %}">Lightning Talks</a></h3>
-				<ul class="nav">
-					<li class="active"><a href="#" class=" pinpointTarget">Home</a></li>
-					<li><a href="#">Link</a></li>
-					<li><a href="#">Link</a></li>
-					<li><a href="#">Link</a></li>
-				</ul>
 				<form action="">
 					<input type="text" placeholder="Search">
 				</form>
 						<ul class="dropdown-menu">
 							{% if request.user.is_authenticated %}
 							<li><a href="{% url profile %}">Your Profile</a></li>
+							<li><a href="{% url create_talk %}">Create a talk</a></li>
 							<li class="divider"></li>
 							<li><a href="{% url logout %}">Logout</a></li>
 							{% else %}

File lightning/templates/common/create_talk.html

+{% extends "_layouts/base.html" %}
+
+{% block page_title %}Create a talk | {% endblock %}
+
+{% block page_content %}
+<h2>Create a new talk</h2>
+<p>All fields are required.</p>
+
+{% load uni_form_tags %}
+{% uni_form form form.helper %}
+{% endblock %}

File lightning/templates/common/talks.html

+{% extends "_layouts/base.html" %}
+
+{% block page_title %}Your talks | {% endblock %}
+
+{% block page_content %}
+{{ object_list }}
+{% endblock %}

File lightning/urls.py

 from django.contrib import admin
 admin.autodiscover()
 
-from common.views import CreateUpdateProfileView
+from common.views import CreateUpdateProfileView, CreateTalkView, TalksView
 
 urlpatterns = patterns('',
     # Examples:
 
     url(r'^logout/$', 'common.views.logout_view', name='logout'),
     url(r'^profile/$', CreateUpdateProfileView.as_view(), name='profile'),
+    url(r'^talks/create/$', CreateTalkView.as_view(), name='create_talk'),
+    url(r'^talks/$', TalksView.as_view(), name='talks'),
     url(r'^admin/', include(admin.site.urls)),
     url(r'', include('social_auth.urls')),
     url(r'^$', 'django.views.generic.simple.direct_to_template', {