You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/11/08 05:45:49 UTC

[4/6] git commit: [#6694] ticket:461 rewrite html form as widget

[#6694] ticket:461 rewrite html form as widget


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/26d01edf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/26d01edf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/26d01edf

Branch: refs/heads/master
Commit: 26d01edf75c5374c88c5c8ca077edbceb414ee17
Parents: 6ee0882
Author: Yuriy Arhipov <yu...@yandex.ru>
Authored: Fri Nov 1 17:06:40 2013 +0400
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Fri Nov 8 04:30:15 2013 +0000

----------------------------------------------------------------------
 .../user_profile/templates/send_message.html    | 33 ++-----------
 .../templates/send_message_form.html            | 45 ++++++++++++++++++
 .../ext/user_profile/templates/user_index.html  |  8 ++--
 Allura/allura/ext/user_profile/user_main.py     | 23 +++++----
 Allura/allura/lib/widgets/user_profile.py       | 50 ++++++++++++++++++++
 .../tests/functional/test_user_profile.py       | 28 +++++++++++
 6 files changed, 144 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/ext/user_profile/templates/send_message.html
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/user_profile/templates/send_message.html b/Allura/allura/ext/user_profile/templates/send_message.html
index 4dcec2d..705cce2 100644
--- a/Allura/allura/ext/user_profile/templates/send_message.html
+++ b/Allura/allura/ext/user_profile/templates/send_message.html
@@ -32,38 +32,11 @@
 {% endblock %}
 
 {% block content %}
+    <div class="content">
     {%if not expire_time%}
-    <div class="grid-24">
-        <div class="grid-24">
-            <b>To:</b> <a href="{{user.url()}}">{{user.display_name|default(user.username)}}</a>
-        </div>
-        <div class="grid-24">
-            <b>From:</b> {{c.user.email_address_header()}}
-        </div>
-        <div class="clear">&nbsp;</div>
-        <form action="send_user_message" method="post">
-            <div class="grid-24">
-                <label for="subject"><b>Subject:</b></label>
-            </div>
-            <input class="grid-10" type="text" name="subject" id="subject">
-            <div class="grid-24">&nbsp;</div>
-            <div class="grid-24">
-                <label for="message"><b>Message:</b></label>
-            </div>
-            <textarea class="grid-10" name="message" id="message" style="height:100px;"></textarea>
-            <div class="grid-24">
-                <input type="checkbox" name="cc" id="ccsender">
-                <label for="ccsender">CC Sender</label>
-            </div>
-            <div class="grid-24">&nbsp;</div>
-            <div class="grid-24">
-                <input type="submit" value="Send Message">
-            </div>
-        </form>
-    </div>
+            {{c.form.display(method = 'POST', action='send_user_message', user=user)}}
     {% else %}
-    <div class="grid-24">
         <h2>Sorry, you can send an email after {{expire_time}}</h2>
-    </div>
     {% endif %}
+    </div>
 {% endblock %}

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/ext/user_profile/templates/send_message_form.html
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/user_profile/templates/send_message_form.html b/Allura/allura/ext/user_profile/templates/send_message_form.html
new file mode 100644
index 0000000..9a2b0df
--- /dev/null
+++ b/Allura/allura/ext/user_profile/templates/send_message_form.html
@@ -0,0 +1,45 @@
+{#-
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+-#}
+<div class="editbox">
+    <div class="grid-19">
+        <b>To:</b> <a href="{{user.url()}}">{{user.display_name|default(user.username)}}</a>
+        </div>
+    <div class="grid-19">
+        <b>From:</b> {{c.user.email_address_header()}}
+    </div>
+    <div class="grid-19">&nbsp;</div>
+    <form method="{{method}}" action="{{action}}">
+        <div class="grid-19">
+            <label class="cr">Subject:</label>
+            {{widget.display_field(widget.fields.subject)}}
+        </div>
+        <div class="grid-19">
+            <label class="cr">Message:</label>
+            {{widget.display_field(widget.fields.message)}}
+        </div>
+        <div class="grid-19">
+            {{widget.display_field(widget.fields.cc)}}
+        </div>
+        <div class="grid-19">&nbsp;</div>
+        <div class="grid-19">
+            {{buttons[0].display()}}
+            <a href="{{c.app.url}}" class="btn link cancel_form">Cancel</a>
+        </div>
+    </form>
+</div>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/ext/user_profile/templates/user_index.html
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/user_profile/templates/user_index.html b/Allura/allura/ext/user_profile/templates/user_index.html
index f328085..88c54c7 100644
--- a/Allura/allura/ext/user_profile/templates/user_index.html
+++ b/Allura/allura/ext/user_profile/templates/user_index.html
@@ -228,9 +228,11 @@
         </div>
       </div>
     {% endif %}
- <div class="grid-24">
-    <b><a href="send_message">Send me a message</a></b>
- </div>
+ {% if has_email %}
+    <div class="grid-24">
+        <b><a href="send_message">Send me a message</a></b>
+    </div>
+  {% endif %}
   </div><!-- end of Personal data section -->
   <div class="grid-24">
     <b>Current {{user.get_pref('display_name')}}'s skills list</b>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/ext/user_profile/user_main.py
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/user_profile/user_main.py b/Allura/allura/ext/user_profile/user_main.py
index b4b76ab..3832a7f 100644
--- a/Allura/allura/ext/user_profile/user_main.py
+++ b/Allura/allura/ext/user_profile/user_main.py
@@ -36,10 +36,15 @@ from allura.model import User, Feed, ACE, ProjectRole
 from allura.controllers import BaseController
 from allura.controllers.feed import FeedArgs, FeedController
 from allura.lib.decorators import require_post
+from allura.lib.widgets.user_profile import SendMessageForm
 
 log = logging.getLogger(__name__)
 
 
+class F(object):
+    send_message = SendMessageForm()
+
+
 class UserProfileApp(Application):
     __version__ = version.__version__
     installable = False
@@ -93,7 +98,8 @@ class UserProfileController(BaseController, FeedController):
         if not user:
             raise exc.HTTPNotFound()
         provider = AuthenticationProvider.get(request)
-        return dict(user=user, reg_date=provider.user_registration_date(user))
+        has_email = c.user.get_pref('email_address') is not None
+        return dict(user=user, reg_date=provider.user_registration_date(user), has_email=has_email)
 
     def get_feed(self, project, app, user):
         """Return a :class:`allura.controllers.feed.FeedArgs` object describing
@@ -109,9 +115,9 @@ class UserProfileController(BaseController, FeedController):
             project.url())
 
     @expose('jinja:allura.ext.user_profile:templates/send_message.html')
-    def send_message(self, subject='', message='', cc=None, errors=None):
+    def send_message(self):
         user = c.project.user_project_of
-        if not user:
+        if not (user and c.user.get_pref('email_address')):
             raise exc.HTTPNotFound()
 
         time_interval = config['user_message.time_interval']
@@ -123,7 +129,7 @@ class UserProfileController(BaseController, FeedController):
             h, remainder = divmod(expire_seconds.total_seconds(), 3600)
             m, s = divmod(remainder, 60)
             expire_time = '%s:%s:%s' % (int(h), int(m), int(s))
-
+        c.form = F.send_message
         return dict(user=user, expire_time=expire_time)
 
     @require_post()
@@ -131,13 +137,10 @@ class UserProfileController(BaseController, FeedController):
     @validate(dict(subject=validators.NotEmpty,
                    message=validators.NotEmpty))
     def send_user_message(self, subject='', message='', cc=None):
+        user = c.project.user_project_of
+        if not (user and c.user.get_pref('email_address')):
+            raise exc.HTTPNotFound()
 
-        if c.form_errors:
-            error_msg = ''
-            for field, error in c.form_errors.iteritems():
-                error_msg += '%s: %s ' % (field, error)
-            flash(error_msg, 'error')
-            redirect(request.referer)
         time_interval = config['user_message.time_interval']
         max_messages = config['user_message.max_messages']
         if cc:

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/lib/widgets/user_profile.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/user_profile.py b/Allura/allura/lib/widgets/user_profile.py
new file mode 100644
index 0000000..eb19e27
--- /dev/null
+++ b/Allura/allura/lib/widgets/user_profile.py
@@ -0,0 +1,50 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+
+import ew as ew_core
+import ew.jinja2_ew as ew
+from formencode import  validators as fev
+from .forms import ForgeForm
+
+
+class SendMessageForm(ForgeForm):
+    template = 'jinja:allura.ext.user_profile:templates/send_message_form.html'
+    submit_text = 'Send Message'
+
+
+    class fields(ew_core.NameList):
+        subject = ew.TextField(
+            validator=fev.UnicodeString(
+                not_empty=True,
+                messages={'empty': "You must provide a Subject"}),
+            attrs=dict(
+                placeholder='Enter your subject here',
+                title='Enter your subject here',
+                style='width: 425px'),
+            label='Subject')
+
+        message = ew.TextArea(
+            validator=fev.UnicodeString(
+                not_empty=True,
+                messages={'empty': "You must provide a Message"}),
+            attrs=dict(
+                placeholder='Enter your message here',
+                title='Enter your message here',
+                style='width: 425px; height:200px'),
+            label='Message')
+
+        cc = ew.Checkbox(label='CC Sender')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/26d01edf/Allura/allura/tests/functional/test_user_profile.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_user_profile.py b/Allura/allura/tests/functional/test_user_profile.py
index 6730d6e..51b6216 100644
--- a/Allura/allura/tests/functional/test_user_profile.py
+++ b/Allura/allura/tests/functional/test_user_profile.py
@@ -101,4 +101,32 @@ class TestUserProfile(TestController):
         response = self.app.get('/u/test-user/profile/send_message', status=200)
         assert 'Sorry, you can send an email after' in response
 
+    @td.with_user_project('test-user')
+    def test_send_message_for_anonymous(self):
+        self.app.get('/u/test-user/profile/send_message',
+                     extra_environ={'username': '*anonymous'},
+                     status=404)
+
+        self.app.post('/u/test-user/profile/send_user_message',
+                      params={'subject': 'test subject',
+                              'message': 'test message',
+                              'cc': 'on'},
+                      extra_environ={'username': '*anonymous'},
+                      status=404)
+
+    @td.with_user_project('test-user')
+    def test_link_to_send_message_form(self):
+        r = self.app.get('/u/test-user/profile',
+                         status=200)
+        assert '<a href="send_message">Send me a message</a>' in r
+
+        r = self.app.get('/u/test-user/profile',
+                     extra_environ={'username': '*anonymous'},
+                     status=200)
+
+        assert '<a href="send_message">Send me a message</a>' not in r
+
+
+
+