<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -38,7 +38,7 @@ class index:
     def GET(self):
         petitions = db.select(['petition', 'signatory'],
                     what='petition.id, petition.title, count(signatory.user_id) as signature_count',
-                    where='petition.id = signatory.petition_id and petition.deleted is null',
+                    where='petition.id = signatory.petition_id and petition.deleted is null and petition.published is not null',
                     group='petition.id, petition.title',
                     order='count(signatory.user_id) desc'
                     )
@@ -51,19 +51,34 @@ def send_to_congress(uid, i, signid):
     env = simplejson.loads(i.get('captcha_env', '{}'))
     i.msg = i.msg + '\n' + i.get('comment', '')
     send_msgs(uid, i, source_id='s%s' % signid, env=env)
-        
+
 def create_petition(i, email):
     tocongress = i.get('tocongress', 'off') == 'on'
     i.pid = i.pid.replace(' ', '-')
     u = helpers.get_user_by_email(email)
+    is_draft = 'save' in i
+    published = None if is_draft else datetime.now()
     try:
-        db.insert('petition', seqname=False, id=i.pid, title=i.ptitle,
+        db.insert('petition', seqname=False, id=i.pid, title=i.ptitle, 
+                    created=datetime.now(), published=published,
                     description=i.msg, owner_id=u.id, to_congress=tocongress)
     except: return
+    
+    if is_draft:
+        msg = &quot;&quot;&quot;Petition saved for publishing later.&quot;&quot;&quot;
+        helpers.set_msg(msg)
+    else:
+        create_signature(i, u.email)
+
+def create_signature(i, email):
+    tocongress = i.get('tocongress', 'off') == 'on'
+    i.pid = i.pid.replace(' ', '-')
+    u = helpers.get_user_by_email(email)    
     signid = save_signature(i, i.pid, u.id)
     if tocongress: send_to_congress(u.id, i, signid)
+    sendmail_to_signatory(u, i.pid)
     msg = &quot;&quot;&quot;Congratulations, you've created your petition.
-             Now sign and share it with all your friends.&quot;&quot;&quot;
+             Now share it with all your friends.&quot;&quot;&quot;
     helpers.set_msg(msg)
     
 class new:
@@ -209,6 +224,9 @@ def get_referrer(pid, uid):
     if referrer != uid:
         return referrer
 
+def is_draft(p):
+    return not bool(p.published)
+
 class petition:
     def GET(self, pid, sf=None, wf=None):
         i = web.input()
@@ -239,9 +257,10 @@ class petition:
         useremail = helpers.get_loggedin_email() or helpers.get_unverified_email()
         isauthor = is_author(useremail, pid)
         issignatory = is_signatory(useremail, pid)
+        isdraft = is_draft(p)
         p.signatory_count = get_num_signs(pid)
         msg, msg_type = helpers.get_delete_msg()
-        return render.petition(p, sf, useremail, isauthor, issignatory, wf, captcha_html, msg)
+        return render.petition(p, sf, useremail, isauthor, issignatory, wf, captcha_html, isdraft, msg)
 
     @auth.require_login
     def GET_edit(self, pid):
@@ -253,8 +272,9 @@ class petition:
             pf.fill(userid=u.id, email=user_email, pid=p.id, ptitle=p.title, msg=p.description, tocongress=p.to_congress)
             wf = forms.wyrform()
             fill_user_details(wf)
+            isdraft = is_draft(p)
             title = &quot;Edit your petition&quot;
-            return render.petitionform(pf, wf, title, target='/c/%s?m=edit' % (pid))
+            return render.petitionform(pf, wf, title, target='/c/%s?m=edit' % (pid), is_draft=isdraft)
         elif user_email:
             msg = &quot;You don't have permissions to edit this petition.&quot;
         else:    
@@ -346,8 +366,13 @@ class petition:
         wyr_valid = (not(tocongress) or wf.validates(i))
         if not pf.validates(i) or not wyr_valid:
             title = &quot;Edit petition&quot;
-            return render.petitionform(pf, wf, title, target='/c/%s?m=edit' % (pid))
-        db.update('petition', where='id=$pid', title=i.ptitle, description=i.msg, to_congress=tocongress, vars=locals())
+            return render.petitionform(pf, wf, title, target='/c/%s?m=edit' % (pid), is_draft=is_draft)
+        
+        p = dict(title=i.ptitle, description=i.msg, to_congress=tocongress)
+        if 'publish' in i: p['published'] = datetime.now()
+        db.update('petition', where='id=$pid', vars=locals(), **p)
+        if 'publish' in i:
+            create_signature(i, i.email)
         update_user_details(i)
         raise web.seeother('/%s' % pid)
 
@@ -406,7 +431,8 @@ class share:
         contacts = get_contacts(user_id)
         sender = helpers.get_user_by_email(helpers.get_loggedin_email() or helpers.get_unverified_email())
 
-        page_or_petition = 'page'    
+        page_or_petition = 'page'
+        isdraft = False
         if not emailform:
             emailform = forms.emailform()
             track_id, description = None, None
@@ -414,12 +440,12 @@ class share:
                 url = url.rstrip('/')
                 pid = web.lstrips(url, '/c/')
                 p = get_petition_by_id(pid)
+                isdraft = is_draft(p)
                 description = p and p.description
-                track_id = helpers.get_trackid(user_id, pid)
+                track_id = helpers.get_trackid(user_id, pid) if not isdraft else None
                 contacts = filter(lambda c: not is_signatory(c.email, pid), contacts)
                 page_or_petition = 'petition'
-
-            msg = render_plain.share_mail(title, url, sender, description, track_id)
+            msg = render_plain.share_mail(title, url, sender, description, isdraft, track_id)
             emailform.fill(subject=title, body=msg)
 
         loadcontactsform = loadcontactsform or forms.loadcontactsform()</diff>
      <filename>petition.py</filename>
    </modified>
    <modified>
      <diff>@@ -6,6 +6,8 @@ $def with (page)
       &lt;title&gt;watchdog.net: $:page.title.strip()&lt;/title&gt;
   $else:
       &lt;title&gt;$:page.title.strip() (watchdog.net)&lt;/title&gt;
+
+$if page.get('noindex'): &lt;meta name=&quot;robots&quot; content=&quot;noindex, nofollow&quot;&gt;
   &lt;link href=&quot;/static/css/style.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot; /&gt;
   &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/jquery.js&quot;&gt;&lt;/script&gt;
   $:page.get('head')</diff>
      <filename>templates/base.html</filename>
    </modified>
    <modified>
      <diff>@@ -1,10 +1,13 @@
-$def with (p, signform, useremail, isauthor, issignatory, wyrform=None, captchas='', msg=None)
+$def with (p, signform, useremail, isauthor, issignatory, wyrform=None, captchas='', is_draft=None, msg=None)
 
 $var width: 90%
 $var color: white
 
 $var title: $p.title
 
+$if is_draft:
+    $var noindex: True
+
 &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/petition.js&quot;&gt;&lt;/script&gt;
 $if wyrform:
     &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/wyr.js&quot;&gt;&lt;/script&gt;
@@ -27,24 +30,26 @@ $if msg:
 
 $:format(p.description)
 
-&lt;!-- &lt;h2&gt;$p.title&lt;/h2&gt; --&gt;
-
 &lt;div id=&quot;upperbar&quot;&gt;
-$if p.signatory_count == 0:
-        &lt;h2&gt;Be the first to sign this petition!&lt;/h2&gt;
-$else:
-	$if p.signatory_count == 1:
-		$ ppl = 'person&lt;/a&gt;&lt;/strong&gt; has'
-	$else:
-		$ ppl = 'people&lt;/a&gt;&lt;/strong&gt; have'
-	$if issignatory:
-	    $ signed = ', including &lt;em&gt;you&lt;/em&gt;'
-	$else:
-	    $ signed = ''
+$if is_draft:
+    &lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
+$else:    
+    $if p.signatory_count == 0:
+            &lt;h2&gt;Be the first to sign this petition!&lt;/h2&gt;
+    $else:
+    	$if p.signatory_count == 1:
+    		$ ppl = 'person&lt;/a&gt;&lt;/strong&gt; has'
+    	$else:
+    		$ ppl = 'people&lt;/a&gt;&lt;/strong&gt; have'
+    	$if issignatory:
+    	    $ signed = ', including &lt;em&gt;you&lt;/em&gt;'
+    	$else:
+    	    $ signed = ''
 	&lt;h2&gt; &lt;strong&gt;&lt;a href='/c/$p.id/signatories'&gt;$p.signatory_count $:ppl signed this petition$:signed.&lt;/h2&gt;
 
 &lt;div id=&quot;uppermenu&quot;&gt;
-  &lt;a href=&quot;/share?url=/c/$p.id/&amp;title=$p.title&quot;&gt;Share&lt;/a&gt;&amp;nbsp;
+$ share = 'Share Draft' if is_draft else 'Share'    
+  &lt;a href=&quot;/share?url=/c/$p.id/&amp;title=$p.title&quot;&gt;$share&lt;/a&gt;&amp;nbsp;
 $if isauthor:
     &lt;a href=&quot;?m=edit&quot;&gt;Edit&lt;/a&gt;
     &lt;a href=&quot;?m=delete&quot;&gt;Delete&lt;/a&gt;</diff>
      <filename>templates/petition.html</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,4 @@
-$def with (pform, cform, title=&quot;Create a new petition&quot;, target=&quot;/c/new&quot;, captchas='', msg=None)
+$def with (pform, cform, title=&quot;Create a new petition&quot;, target=&quot;/c/new&quot;, captchas='', is_draft=None, msg=None)
 
 $var width: 90%
 $var color: white
@@ -41,6 +41,7 @@ $else:
    	  &lt;strong&gt;http://watchdog.net/c/&lt;/strong&gt; $:pform.pid.render() &lt;span id=&quot;note_pid&quot;&gt;&lt;/span&gt;
    	  &lt;span&gt;(Letters, numbers and dashes only.)&lt;/span&gt;
     $else:
+        &lt;input type=&quot;hidden&quot; name='pid' value=&quot;$:pform.pid.value&quot; /&gt;
    	    &lt;p&gt;&lt;strong&gt;http://watchdog.net/c/$:pform.pid.value&lt;/strong&gt;&lt;/p&gt;
     
     &lt;label for=&quot;msg&quot;&gt;Give it a description&lt;/label&gt;
@@ -81,8 +82,13 @@ $else:
     &lt;/div&gt;
 &lt;/fieldset&gt;
 
-$if target == '/c/new':
-    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot;&gt;Create my petition&lt;/button&gt;
+$if not is_draft:
+    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot; name=&quot;publish&quot;&gt;Create my petition&lt;/button&gt;
+$else:
+    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot; name=&quot;publish&quot;&gt;Save and Publish&lt;/button&gt;
+
+$if target == '/c/new' or is_draft:
+    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot; name=&quot;save&quot;&gt;Save Draft&lt;/button&gt;
 $else:
-    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot;&gt;Save my petition&lt;/button&gt;
+    &lt;button type=&quot;submit&quot; style=&quot;font-size: 1.2em&quot; name=&quot;publish&quot;&gt;Save my petition&lt;/button&gt;
 &lt;/form&gt;</diff>
      <filename>templates/petitionform.html</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,4 @@
-$def with (title, url, sender, description=None, track_id=None)
+$def with (title, url, sender, description=None, is_draft=False, track_id=None)
 
 Dear Friend,
 
@@ -7,7 +7,10 @@ $if track_id:
 $else:
         $ full_url = &quot;http://watchdog.net&quot; + url
 
-$if description:
+$if is_draft:
+    Could you please review the draft of the petition &quot;$:title&quot; at $full_url 
+    and let me know your comments on it?
+$elif description:
        I hope you'll join me in signing the petition &quot;$:title&quot;. You can do so right now at:
        
        $full_url</diff>
      <filename>templates/share_mail.html</filename>
    </modified>
    <modified>
      <diff>@@ -30,8 +30,10 @@ CREATE TABLE petition(
     title text,
     description text,
     owner_id int references users,
-    created timestamp default now(),
+    created timestamp,
     deleted timestamp,
+    published timestamp, -- null for drafts; same as created if published
+    last_modified timestamp default now(),
     to_congress boolean
 );
 </diff>
      <filename>userdata.sql</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>a12c8b323f1a6660763bc067ee82ef83d8526d71</id>
    </parent>
  </parents>
  <author>
    <name>Devi</name>
    <email>asldevi@gmail.com</email>
  </author>
  <url>http://github.com/aaronsw/watchdog/commit/e3ccacbe7260e231e7f48d05d463da52e777dd10</url>
  <id>e3ccacbe7260e231e7f48d05d463da52e777dd10</id>
  <committed-date>2009-03-05T03:44:40-08:00</committed-date>
  <authored-date>2009-03-05T03:44:40-08:00</authored-date>
  <message>draft mode for petitions

run the following sql in DB before pulling this code.

BEGIN;
alter table petition add column published timestamp;
alter table petition add column last_modified timestamp default now();
alter table petition alter column created drop default;
update petition set published=created where 1=1;
COMMIT;</message>
  <tree>7761e3b40cfe014c9f57898e406136c45cfbf548</tree>
  <committer>
    <name>Devi</name>
    <email>asldevi@gmail.com</email>
  </committer>
</commit>
