now a user can create an event if he is a staff
authornishanth
Fri, 09 Apr 2010 16:51:56 +0530
changeset 8 e2699e042129
parent 7 af9ab5ad2786
child 9 e29ecb7819e7
now a user can create an event if he is a staff
event_create.html
reg/events.py
reg/forms.py
reg/models.py
reg/site/urls.py
reg/views.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/event_create.html	Fri Apr 09 16:51:56 2010 +0530
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+{% block content %}
+<form action="" method="post">
+{{form.as_p}}
+<input type="submit" value="Create">
+</form>
+{% endblock %}
--- a/reg/events.py	Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/events.py	Fri Apr 09 16:51:56 2010 +0530
@@ -37,13 +37,13 @@
         except IntegrityError:
             pass
 
-def create_event(title, desc, start_date, stop_date, created_by=None):
+def create_event(title, description, start_date, stop_date, created_by=None):
     """ make an event and save it.
     """
 
     new_event = Event()
     new_event.title = title
-    new_event.description = desc
+    new_event.description = description
     new_event.start_date = start_date
     new_event.stop_date = stop_date
     new_event.save()
--- a/reg/forms.py	Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/forms.py	Fri Apr 09 16:51:56 2010 +0530
@@ -1,10 +1,11 @@
 import string
+from datetime import datetime
 
 from django import forms
 from django.contrib.auth.models import User
 from django.contrib.auth import authenticate
 
-from workshop.reg.models import Profile
+from workshop.reg.models import Profile, Event
 
 class LoginForm(forms.Form):
     """ a form to handle login.
@@ -73,3 +74,34 @@
 
         if not password == self.data['confirm_password']:
             raise forms.ValidationError("Passwords do not match")
+
+class EventCreateForm(forms.ModelForm):
+    """ A form to create an event.
+    """
+
+    class Meta:
+        model = Event
+        fields = ['title', 'description', 'start_date', 'stop_date']
+
+    def clean_start_date(self):
+        """ see if the start date is greater than today or not.
+        """
+
+        start_date = self.cleaned_data['start_date']
+        if start_date < datetime.now().date():
+            raise forms.ValidationError("The event must start at the latest today.")
+        return start_date
+
+    def clean_stop_date(self):
+        """ see that stop_date is not less than start_date.
+        """
+
+        stop_date = self.cleaned_data['stop_date']
+        try:
+            start_date = datetime.strptime(self.data['start_date'], "%Y-%m-%d").date()
+        except ValueError:
+            raise forms.ValidationError("Enter a valid date")
+
+        if start_date > stop_date:
+            raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.')
+        return stop_date
--- a/reg/models.py	Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/models.py	Fri Apr 09 16:51:56 2010 +0530
@@ -30,8 +30,8 @@
 
     title = models.CharField(max_length=100)
     description = models.TextField()
-    start_date = models.DateField()
-    stop_date = models.DateField()
+    start_date = models.DateField(help_text="YYYY-MM-DD")
+    stop_date = models.DateField(help_text="YYYY-MM-DD")
     attendees = models.ManyToManyField(User, related_name="%(class)s_attendees")
     organizers = models.ManyToManyField(User, related_name="%(class)s_organizers")
 
--- a/reg/site/urls.py	Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/site/urls.py	Fri Apr 09 16:51:56 2010 +0530
@@ -7,4 +7,6 @@
     ('^login/$', reg_views.user_login),
     ('^logout/$', reg_views.user_logout),
     ('^register/$', reg_views.user_register),
+    ('^event/create/$', reg_views.create_event),
+    ('^event/view/(\w+)/$', reg_views.view_event),
 )
--- a/reg/views.py	Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/views.py	Fri Apr 09 16:51:56 2010 +0530
@@ -75,3 +75,44 @@
     else:
         form = reg_forms.RegisterForm()
         return render_to_response('register.html', {'form':form})
+
+def create_event(request):
+    """ see if the user is a staff and only then let him do it.
+    """
+
+    user = request.user
+    if user.is_authenticated() and user.is_staff:
+        if request.method ==  "POST":
+            form = reg_forms.EventCreateForm(request.POST)
+            if form.is_valid():
+                data = form.cleaned_data
+                new_event = reg_events.create_event(title=data['title'],
+                                                    description=data['description'],
+                                                    start_date=data['start_date'],
+                                                    stop_date=data['stop_date'],
+                                                    created_by=user,
+                                                   )
+                event_url = "/reg/event/view/%s"%(new_event.key)
+                return redirect(event_url)
+            else:
+                return render_to_response('event_create.html', {'form':form})
+        else:
+            form = reg_forms.EventCreateForm()
+            return render_to_response('event_create.html', {'form':form})
+    else:
+        return redirect('/reg')
+
+def view_event(request, key):
+    """ get the event by its key and display it.
+    """
+
+    user = request.user
+    is_guest = False if user.is_authenticated() else True
+
+    try:
+        event = Event.objects.get(key__iexact=key)
+    except Event.DoesNotExist:
+        return HttpResponse("F off")
+
+    return HttpResponse(str(event))
+