Commits

Anonymous committed 2eda0c5

admin added, group creation successfull

Comments (0)

Files changed (12)

 @author: CCMT
 '''
 from django.forms import ModelForm
-from Group.models import Group
 from django import forms
 
+from Group.models import Group, generate_short_code
+
 
 class GroupForm(ModelForm):
 
         model = Group
         fields = ('name', 'description')
 
-    def save(self, commit=True):
-        pass
+    def save(self, user, commit=True):
+        # get the group model to add short_code field and user field values manually
+        group = super(GroupForm, self).save(commit=False)
+        
+        # create a unique short code for group
+        group.short_code = generate_short_code(user)
+        
+        # assign user foreign key
+        group.user = user
+        
+        # save the form with rest of existing data.
+        if commit:
+            group.save()
+        
+        return group 
 
 @author: CCMT
 '''
+import datetime
+import time
+import hashlib
+
 from django.contrib.auth.models import User
 from django.db import models
 
     dob = models.DateField()
     phone = models.CharField(max_length=20)
     organization = models.CharField(max_length=50)
-    
+
+    def __unicode__(self):
+        return "%s %s" %(self.first_name, self.last_name)    
     
 class Group(models.Model):
     user = models.ForeignKey(User)
     description = models.CharField(max_length=200)
     short_code = models.CharField(max_length=30)
     member = models.ManyToManyField(Member)
-    
+    
+    def __unicode__(self):
+        return self.name
+    
+def generate_short_code(user):
+    """Function to generate short code for group
+    """
+    t = datetime.datetime.now()
+    uts = int(time.mktime(t.timetuple()))
+
+    if user.email not in ['', None]:
+        salt = '%s%s' % (user.email, uts)
+    else:
+        salt = '%s%s' % (user.username, uts)
+
+    return hashlib.sha1(salt).hexdigest()

Group/templates/group/all_groups.html

+{% extends "base.html" %}
+
+{% block content %}
+
+<h3 class="sub-header">My groups</h3>
+
+<ul>
+{% for group in groups %}
+<li class="group-detail">
+    <p><strong>Name:</strong> {{ group.name }}</p>
+    <p><strong>Description:</strong> {{ group.description }}</p>
+    <p><a href="#" class="btn btn-small" >Update Contacts</a></p>
+</li>
+{% endfor %}
+</ul>
+
+{% endblock %}

Group/templates/group/group_form.html

     {% csrf_token %}
     {{ form.as_table }}
     <br/>
-    <input type="submit" class="btn btn-primary" id="add-contact-submit">
+    <input type="submit" class="btn btn-primary" id="create-group-submit">
 </form>

Group/templates/group/groups.html

+<h3 class="sub-header">My groups</h3>
+
+<ul>
+{% for group in groups %}
+<li class="group-detail">
+    <p><strong>Name:</strong> {{ group.name }}</p>
+    <p><strong>Description:</strong> {{ group.description }}</p>
+    <p><a href="#" class="btn btn-small" >Update Contacts</a></p>
+</li>
+{% endfor %}
+</ul>
 from django.shortcuts import render_to_response
 from django.contrib.auth.decorators import login_required
 from django.template import RequestContext
-from django.template.loader import render_to_string
+from django.template.loader import render_to_string #, Context
 from django.http import HttpResponse
 from django.utils import simplejson
+from django.http import Http404
+#from django.views.decorators.csrf import csrf_exempt
 
 from Group.forms import GroupForm
+from Group.models import Group
 
 
 @login_required(login_url='/auth/')
 def group(request):
-    return render_to_response('home.html',context_instance=RequestContext(request))
+    groups = Group.objects.all()
+    return render_to_response('group/all_groups.html',
+                              {'groups':groups},
+                              context_instance=RequestContext(request))
 
 
 @login_required(login_url='/auth/')
 def create_group(request):
+    if not request.is_ajax():
+        raise Http404
+
     if request.method == 'POST':
         form = GroupForm(data=request.POST)
         if form.is_valid():
-            form.save()
-            html = render_to_string('group/groups.html', {'groups':'groups'})
+            form.save(request.user)
+            groups = Group.objects.all()
+            html = render_to_string('group/groups.html', 
+                                    {'groups':groups},
+                                    context_instance=RequestContext(request))
         else:
-            html = render_to_string('group/group_form.html', {'form':form})
+            html = render_to_string('group/group_form.html',
+                                    {'form':form},
+                                    context_instance=RequestContext(request))
 
     else:
         form = GroupForm()
-        html = render_to_string('group/group_form.html', {'form':form})
+        html = render_to_string('group/group_form.html',
+                                {'form':form},
+                                context_instance=RequestContext(request))
 
     data = {'html': html}
     json = simplejson.dumps(data)
-    return HttpResponse(json, mimetype='application/json')
+    return HttpResponse(json,mimetype='application/json')
+    

MassMail/admin.py

+'''
+Created on Apr 10, 2013
+
+@author: CCMT
+'''
+from django.contrib import admin
+
+from MassMail.models import Contract
+from Group.models import Group
+
+admin.site.register(Contract)
+admin.site.register(Group)

MassMail/db.sqlite3

Binary file modified.

MassMail/models.py

     contact_size = models.IntegerField(default=0)
     emails_count = models.IntegerField(default=0)
 
+    def __unicode__(self):
+        return 'Contract for %s ' % (self.user)
     

MassMail/static/css/styles.css

     margin-top:20px;
     padding: 20px;
 }
+
+.group-detail{
+	list-style: none;
+    margin: 20px auto;
+    padding: 10px;
+    background: whiteSmoke;
+    border: 1px solid #CCC;
+    border-radius: 5px;
+    line-height:24px;
+}

MassMail/static/javascript/common.js

     });
 
     $("#create-group").click(function (e){
-    	console.log('create group clicked');
-    	e.preventDefault();
-    	
+
+    	e.preventDefault();    	
         var content = $("#formDiv");
         
         $.ajax({
             url:'/group/create/',
             type: 'GET',
             success: function (resp){
-                console.log('response received...',resp);
                 $(content).html(resp.html);
             }
         });
     });
 
 
-    $("#add-contact-submit").live('click', function (e){
+    $("#create-group-submit").live('click', function (e){
             e.preventDefault();
             var form = $("#create-group-form");
             var content = $("#formDiv");

MassMail/views.py

     return render_to_response('home.html',context_instance=RequestContext(request))
 
 def signup(request):
+    print 'signup  :',request
     # if request method is post validate the data. else just render the signup form.
     if request.method == 'POST':
         form = SignupForm(data=request.POST)