Commits

Marcin Kuzminski committed b0fef8a

Added simple UI for admin to manage emails map

  • Participants
  • Parent commits 20e307d
  • Branches codereview

Comments (0)

Files changed (5)

rhodecode/config/routing.py

         #EXTRAS USER ROUTES
         m.connect("user_perm", "/users_perm/{id}",
                   action="update_perm", conditions=dict(method=["PUT"]))
+        m.connect("user_emails", "/users_emails/{id}",
+                  action="add_email", conditions=dict(method=["PUT"]))
+        m.connect("user_emails_delete", "/users_emails/{id}",
+                  action="delete_email", conditions=dict(method=["DELETE"]))
 
     #ADMIN USERS REST ROUTES
     with rmap.submapper(path_prefix=ADMIN_PREFIX,

rhodecode/controllers/admin/users.py

 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator
 from rhodecode.lib.base import BaseController, render
 
-from rhodecode.model.db import User, Permission
+from rhodecode.model.db import User, Permission, UserEmailMap
 from rhodecode.model.forms import UserForm
 from rhodecode.model.user import UserModel
 from rhodecode.model.meta import Session
         c.user.permissions = {}
         c.granted_permissions = UserModel().fill_perms(c.user)\
             .permissions['global']
-
+        c.user_email_map = UserEmailMap.query()\
+                        .filter(UserEmailMap.user == c.user).all()
         defaults = c.user.get_dict()
         perm = Permission.get_by_key('hg.create.repository')
         defaults.update({'create_repo_perm': UserModel().has_perm(id, perm)})
                     category='success')
             Session.commit()
         return redirect(url('edit_user', id=id))
+
+    def add_email(self, id):
+        """PUT /user_emails/id: Update an existing item"""
+        # url('user_emails', id=ID, method='put')
+
+        #TODO: validation and form !!!
+        email = request.POST.get('new_email')
+        user_model = UserModel()
+
+        try:
+            user_model.add_extra_email(id, email)
+            Session.commit()
+            h.flash(_("Added email %s to user" % email), category='success')
+        except Exception:
+            log.error(traceback.format_exc())
+            h.flash(_('An error occurred during email saving'),
+                    category='error')
+        return redirect(url('edit_user', id=id))
+
+    def delete_email(self, id):
+        """DELETE /user_emails_delete/id: Delete an existing item"""
+        # url('user_emails_delete', id=ID, method='delete')
+        user_model = UserModel()
+        user_model.delete_extra_email(id, request.POST.get('del_email'))
+        Session.commit()
+        h.flash(_("Removed email from user"), category='success')
+        return redirect(url('edit_user', id=id))

rhodecode/model/user.py

 from rhodecode.model.db import User, UserRepoToPerm, Repository, Permission, \
     UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember, \
     Notification, RepoGroup, UserRepoGroupToPerm, UsersGroup,\
-    UsersGroupRepoGroupToPerm
+    UsersGroupRepoGroupToPerm, UserEmailMap
 from rhodecode.lib.exceptions import DefaultUserException, \
     UserOwnsReposException
 
                 .scalar()
         if obj:
             self.sa.delete(obj)
+
+    def add_extra_email(self, user, email):
+        """
+        Adds email address to UserEmailMap
+
+        :param user:
+        :param email:
+        """
+        user = self.__get_user(user)
+        obj = UserEmailMap()
+        obj.user = user
+        obj.email = email
+        self.sa.add(obj)
+        return obj
+
+    def delete_extra_email(self, user, email_id):
+        """
+        Removes email address from UserEmailMap
+
+        :param user:
+        :param email_id:
+        """
+        user = self.__get_user(user)
+        obj = UserEmailMap.query().get(email_id)
+        if obj:
+            self.sa.delete(obj)

rhodecode/public/css/style.css

 	padding:0px 0px 0px 10px;
 }
 
+.emails_wrap{
+	padding: 0px 20px;
+}
+
+.emails_wrap .email_entry{
+    height: 30px;
+    padding:0px 0px 0px 10px;
+}
+.emails_wrap .email_entry .email{
+	float: left
+}
+.emails_wrap .email_entry .email_action{
+	float: left
+}
+
 /*README STYLE*/
 
 div.readme {

rhodecode/templates/admin/users/user_edit.html

     </div>
     ${h.end_form()}
 </div>
+<div class="box box-right">
+    <!-- box / title -->
+    <div class="title">
+        <h5>${_('Email addresses')}</h5>
+    </div>
+    
+    <div class="emails_wrap">
+      <table class="noborder">
+      %for em in c.user_email_map:
+        <tr>
+            <td><div class="gravatar"><img alt="gravatar" src="${h.gravatar_url(em.user.email,16)}"/> </div></td>
+            <td><div class="email">${em.email}</div></td>
+            <td>
+              ${h.form(url('user_emails_delete', id=c.user.user_id),method='delete')}
+                  ${h.hidden('del_email',em.email_id)}
+                  ${h.submit('remove_',_('delete'),id="remove_email_%s" % em.email_id,
+                  class_="delete_icon action_button", onclick="return  confirm('"+_('Confirm to delete this email: %s') % em.email+"');")}
+              ${h.end_form()}            
+            </td>
+        </tr>
+      %endfor
+      </table>
+    </div>
+    
+    ${h.form(url('user_emails', id=c.user.user_id),method='put')}
+    <div class="form">
+        <!-- fields -->
+        <div class="fields">
+             <div class="field">
+                <div class="label">
+                    <label for="email">${_('New email address')}:</label>
+                </div>
+                <div class="input">
+                    ${h.text('new_email', class_='medium')}
+                </div>
+             </div>        
+            <div class="buttons">
+              ${h.submit('save',_('Add'),class_="ui-button")}
+              ${h.reset('reset',_('Reset'),class_="ui-button")}
+            </div>
+        </div>
+    </div>
+    ${h.end_form()}
+</div>
 </%def>