diff --git a/ordr3/events.py b/ordr3/events.py index f405e8d..8c63ca8 100644 --- a/ordr3/events.py +++ b/ordr3/events.py @@ -1,6 +1,6 @@ from collections import namedtuple -from pyramid.events import subscriber +from pyramid.events import BeforeRender, subscriber from pyramid.renderers import render from pyramid_mailer.message import Message @@ -92,6 +92,12 @@ def notify_user(event): event.request.mailer.send(message) +@subscriber(BeforeRender) +def add_global(event): + request = event["request"] + event.rendering_val["new_user_badge"] = request.repo.count_new_users() + + def emit(request, event): event.request = request request.registry.notify(event) diff --git a/ordr3/repo.py b/ordr3/repo.py index 3e94211..9ae00f0 100644 --- a/ordr3/repo.py +++ b/ordr3/repo.py @@ -152,6 +152,14 @@ class SqlAlchemyRepository(AbstractOrderRepository): .all() ) + def count_new_users(self): + """ count the number of new users that need approval """ + return ( + self.session.query(models.User) + .filter(models.User.role == models.UserRole.NEW) + .count() + ) + def search_vendor(self, reference): """ search for a vendor by its canonical name """ vendor = ( diff --git a/ordr3/resources.py b/ordr3/resources.py index 9c6b7ce..c95c8d4 100644 --- a/ordr3/resources.py +++ b/ordr3/resources.py @@ -43,13 +43,19 @@ class BaseResource(abc.ABC): return cls(primary_key, parent, sql_model_instance) +class UserList(BaseResource): + def __acl__(self): + """ access controll list """ + return [(Allow, "role:admin", "view")] + + class Root(BaseResource): """ Root resource """ __name__ = None __parent__ = None - nodes = {} + nodes = {"users": UserList} def __init__(self, request): self.request = request diff --git a/ordr3/static/infinite.js b/ordr3/static/infinite.js new file mode 100644 index 0000000..a85b74b --- /dev/null +++ b/ordr3/static/infinite.js @@ -0,0 +1,85 @@ +/*! +Waypoints Infinite Scroll Shortcut - 4.0.1 +Copyright © 2011-2016 Caleb Troughton +Licensed under the MIT license. +https://github.com/imakewebthings/waypoints/blob/master/licenses.txt +*/ +(function() { + 'use strict' + + var $ = window.jQuery + var Waypoint = window.Waypoint + + /* http://imakewebthings.com/waypoints/shortcuts/infinite-scroll */ + function Infinite(options) { + this.options = $.extend({}, Infinite.defaults, options) + this.container = this.options.element + if (this.options.container !== 'auto') { + this.container = this.options.container + } + this.$container = $(this.container) + this.$more = $(this.options.more) + + if (this.$more.length) { + this.setupHandler() + this.waypoint = new Waypoint(this.options) + } + } + + /* Private */ + Infinite.prototype.setupHandler = function() { + this.options.handler = $.proxy(function() { + this.options.onBeforePageLoad() + this.destroy() + this.$container.addClass(this.options.loadingClass) + + $.get($(this.options.more).attr('href'), $.proxy(function(data) { + var $data = $($.parseHTML(data)) + var $newMore = $data.find(this.options.more) + + var $items = $data.find(this.options.items) + if (!$items.length) { + $items = $data.filter(this.options.items) + } + + this.$container.append($items) + this.$container.removeClass(this.options.loadingClass) + + if (!$newMore.length) { + $newMore = $data.filter(this.options.more) + } + if ($newMore.length) { + this.$more.replaceWith($newMore) + this.$more = $newMore + this.waypoint = new Waypoint(this.options) + this.$container.append(this.$more) + } + else { + this.$more.remove() + } + + this.options.onAfterPageLoad($items) + }, this)) + }, this) + } + + /* Public */ + Infinite.prototype.destroy = function() { + if (this.waypoint) { + this.waypoint.destroy() + } + } + + Infinite.defaults = { + container: 'auto', + items: '.infinite-item', + more: '.infinite-more-link', + offset: 'bottom-in-view', + loadingClass: 'infinite-loading', + onBeforePageLoad: $.noop, + onAfterPageLoad: $.noop + } + + Waypoint.Infinite = Infinite +}()) +; diff --git a/ordr3/static/infinite.min.js b/ordr3/static/infinite.min.js new file mode 100644 index 0000000..d429cfb --- /dev/null +++ b/ordr3/static/infinite.min.js @@ -0,0 +1,7 @@ +/*! +Waypoints Infinite Scroll Shortcut - 4.0.1 +Copyright © 2011-2016 Caleb Troughton +Licensed under the MIT license. +https://github.com/imakewebthings/waypoints/blob/master/licenses.txt +*/ +!function(){"use strict";function t(n){this.options=i.extend({},t.defaults,n),this.container=this.options.element,"auto"!==this.options.container&&(this.container=this.options.container),this.$container=i(this.container),this.$more=i(this.options.more),this.$more.length&&(this.setupHandler(),this.waypoint=new o(this.options))}var i=window.jQuery,o=window.Waypoint;t.prototype.setupHandler=function(){this.options.handler=i.proxy(function(){this.options.onBeforePageLoad(),this.destroy(),this.$container.addClass(this.options.loadingClass),i.get(i(this.options.more).attr("href"),i.proxy(function(t){var n=i(i.parseHTML(t)),e=n.find(this.options.more),s=n.find(this.options.items);s.length||(s=n.filter(this.options.items)),this.$container.append(s),this.$container.removeClass(this.options.loadingClass),e.length||(e=n.filter(this.options.more)),e.length?(this.$more.replaceWith(e),this.$more=e,this.waypoint=new o(this.options)):this.$more.remove(),this.options.onAfterPageLoad(s)},this))},this)},t.prototype.destroy=function(){this.waypoint&&this.waypoint.destroy()},t.defaults={container:"auto",items:".infinite-item",more:".infinite-more-link",offset:"bottom-in-view",loadingClass:"infinite-loading",onBeforePageLoad:i.noop,onAfterPageLoad:i.noop},o.Infinite=t}(); \ No newline at end of file diff --git a/ordr3/static/jquery.waypoints.min.js b/ordr3/static/jquery.waypoints.min.js new file mode 100644 index 0000000..609ece0 --- /dev/null +++ b/ordr3/static/jquery.waypoints.min.js @@ -0,0 +1,7 @@ +/*! +Waypoints - 4.0.1 +Copyright © 2011-2016 Caleb Troughton +Licensed under the MIT license. +https://github.com/imakewebthings/waypoints/blob/master/licenses.txt +*/ +!function(){"use strict";function t(o){if(!o)throw new Error("No options passed to Waypoint constructor");if(!o.element)throw new Error("No element option passed to Waypoint constructor");if(!o.handler)throw new Error("No handler option passed to Waypoint constructor");this.key="waypoint-"+e,this.options=t.Adapter.extend({},t.defaults,o),this.element=this.options.element,this.adapter=new t.Adapter(this.element),this.callback=o.handler,this.axis=this.options.horizontal?"horizontal":"vertical",this.enabled=this.options.enabled,this.triggerPoint=null,this.group=t.Group.findOrCreate({name:this.options.group,axis:this.axis}),this.context=t.Context.findOrCreateByElement(this.options.context),t.offsetAliases[this.options.offset]&&(this.options.offset=t.offsetAliases[this.options.offset]),this.group.add(this),this.context.add(this),i[this.key]=this,e+=1}var e=0,i={};t.prototype.queueTrigger=function(t){this.group.queueTrigger(this,t)},t.prototype.trigger=function(t){this.enabled&&this.callback&&this.callback.apply(this,t)},t.prototype.destroy=function(){this.context.remove(this),this.group.remove(this),delete i[this.key]},t.prototype.disable=function(){return this.enabled=!1,this},t.prototype.enable=function(){return this.context.refresh(),this.enabled=!0,this},t.prototype.next=function(){return this.group.next(this)},t.prototype.previous=function(){return this.group.previous(this)},t.invokeAll=function(t){var e=[];for(var o in i)e.push(i[o]);for(var n=0,r=e.length;r>n;n++)e[n][t]()},t.destroyAll=function(){t.invokeAll("destroy")},t.disableAll=function(){t.invokeAll("disable")},t.enableAll=function(){t.Context.refreshAll();for(var e in i)i[e].enabled=!0;return this},t.refreshAll=function(){t.Context.refreshAll()},t.viewportHeight=function(){return window.innerHeight||document.documentElement.clientHeight},t.viewportWidth=function(){return document.documentElement.clientWidth},t.adapters=[],t.defaults={context:window,continuous:!0,enabled:!0,group:"default",horizontal:!1,offset:0},t.offsetAliases={"bottom-in-view":function(){return this.context.innerHeight()-this.adapter.outerHeight()},"right-in-view":function(){return this.context.innerWidth()-this.adapter.outerWidth()}},window.Waypoint=t}(),function(){"use strict";function t(t){window.setTimeout(t,1e3/60)}function e(t){this.element=t,this.Adapter=n.Adapter,this.adapter=new this.Adapter(t),this.key="waypoint-context-"+i,this.didScroll=!1,this.didResize=!1,this.oldScroll={x:this.adapter.scrollLeft(),y:this.adapter.scrollTop()},this.waypoints={vertical:{},horizontal:{}},t.waypointContextKey=this.key,o[t.waypointContextKey]=this,i+=1,n.windowContext||(n.windowContext=!0,n.windowContext=new e(window)),this.createThrottledScrollHandler(),this.createThrottledResizeHandler()}var i=0,o={},n=window.Waypoint,r=window.onload;e.prototype.add=function(t){var e=t.options.horizontal?"horizontal":"vertical";this.waypoints[e][t.key]=t,this.refresh()},e.prototype.checkEmpty=function(){var t=this.Adapter.isEmptyObject(this.waypoints.horizontal),e=this.Adapter.isEmptyObject(this.waypoints.vertical),i=this.element==this.element.window;t&&e&&!i&&(this.adapter.off(".waypoints"),delete o[this.key])},e.prototype.createThrottledResizeHandler=function(){function t(){e.handleResize(),e.didResize=!1}var e=this;this.adapter.on("resize.waypoints",function(){e.didResize||(e.didResize=!0,n.requestAnimationFrame(t))})},e.prototype.createThrottledScrollHandler=function(){function t(){e.handleScroll(),e.didScroll=!1}var e=this;this.adapter.on("scroll.waypoints",function(){(!e.didScroll||n.isTouch)&&(e.didScroll=!0,n.requestAnimationFrame(t))})},e.prototype.handleResize=function(){n.Context.refreshAll()},e.prototype.handleScroll=function(){var t={},e={horizontal:{newScroll:this.adapter.scrollLeft(),oldScroll:this.oldScroll.x,forward:"right",backward:"left"},vertical:{newScroll:this.adapter.scrollTop(),oldScroll:this.oldScroll.y,forward:"down",backward:"up"}};for(var i in e){var o=e[i],n=o.newScroll>o.oldScroll,r=n?o.forward:o.backward;for(var s in this.waypoints[i]){var a=this.waypoints[i][s];if(null!==a.triggerPoint){var l=o.oldScroll=a.triggerPoint,p=l&&h,u=!l&&!h;(p||u)&&(a.queueTrigger(r),t[a.group.id]=a.group)}}}for(var c in t)t[c].flushTriggers();this.oldScroll={x:e.horizontal.newScroll,y:e.vertical.newScroll}},e.prototype.innerHeight=function(){return this.element==this.element.window?n.viewportHeight():this.adapter.innerHeight()},e.prototype.remove=function(t){delete this.waypoints[t.axis][t.key],this.checkEmpty()},e.prototype.innerWidth=function(){return this.element==this.element.window?n.viewportWidth():this.adapter.innerWidth()},e.prototype.destroy=function(){var t=[];for(var e in this.waypoints)for(var i in this.waypoints[e])t.push(this.waypoints[e][i]);for(var o=0,n=t.length;n>o;o++)t[o].destroy()},e.prototype.refresh=function(){var t,e=this.element==this.element.window,i=e?void 0:this.adapter.offset(),o={};this.handleScroll(),t={horizontal:{contextOffset:e?0:i.left,contextScroll:e?0:this.oldScroll.x,contextDimension:this.innerWidth(),oldScroll:this.oldScroll.x,forward:"right",backward:"left",offsetProp:"left"},vertical:{contextOffset:e?0:i.top,contextScroll:e?0:this.oldScroll.y,contextDimension:this.innerHeight(),oldScroll:this.oldScroll.y,forward:"down",backward:"up",offsetProp:"top"}};for(var r in t){var s=t[r];for(var a in this.waypoints[r]){var l,h,p,u,c,d=this.waypoints[r][a],f=d.options.offset,w=d.triggerPoint,y=0,g=null==w;d.element!==d.element.window&&(y=d.adapter.offset()[s.offsetProp]),"function"==typeof f?f=f.apply(d):"string"==typeof f&&(f=parseFloat(f),d.options.offset.indexOf("%")>-1&&(f=Math.ceil(s.contextDimension*f/100))),l=s.contextScroll-s.contextOffset,d.triggerPoint=Math.floor(y+l-f),h=w=s.oldScroll,u=h&&p,c=!h&&!p,!g&&u?(d.queueTrigger(s.backward),o[d.group.id]=d.group):!g&&c?(d.queueTrigger(s.forward),o[d.group.id]=d.group):g&&s.oldScroll>=d.triggerPoint&&(d.queueTrigger(s.forward),o[d.group.id]=d.group)}}return n.requestAnimationFrame(function(){for(var t in o)o[t].flushTriggers()}),this},e.findOrCreateByElement=function(t){return e.findByElement(t)||new e(t)},e.refreshAll=function(){for(var t in o)o[t].refresh()},e.findByElement=function(t){return o[t.waypointContextKey]},window.onload=function(){r&&r(),e.refreshAll()},n.requestAnimationFrame=function(e){var i=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||t;i.call(window,e)},n.Context=e}(),function(){"use strict";function t(t,e){return t.triggerPoint-e.triggerPoint}function e(t,e){return e.triggerPoint-t.triggerPoint}function i(t){this.name=t.name,this.axis=t.axis,this.id=this.name+"-"+this.axis,this.waypoints=[],this.clearTriggerQueues(),o[this.axis][this.name]=this}var o={vertical:{},horizontal:{}},n=window.Waypoint;i.prototype.add=function(t){this.waypoints.push(t)},i.prototype.clearTriggerQueues=function(){this.triggerQueues={up:[],down:[],left:[],right:[]}},i.prototype.flushTriggers=function(){for(var i in this.triggerQueues){var o=this.triggerQueues[i],n="up"===i||"left"===i;o.sort(n?e:t);for(var r=0,s=o.length;s>r;r+=1){var a=o[r];(a.options.continuous||r===o.length-1)&&a.trigger([i])}}this.clearTriggerQueues()},i.prototype.next=function(e){this.waypoints.sort(t);var i=n.Adapter.inArray(e,this.waypoints),o=i===this.waypoints.length-1;return o?null:this.waypoints[i+1]},i.prototype.previous=function(e){this.waypoints.sort(t);var i=n.Adapter.inArray(e,this.waypoints);return i?this.waypoints[i-1]:null},i.prototype.queueTrigger=function(t,e){this.triggerQueues[e].push(t)},i.prototype.remove=function(t){var e=n.Adapter.inArray(t,this.waypoints);e>-1&&this.waypoints.splice(e,1)},i.prototype.first=function(){return this.waypoints[0]},i.prototype.last=function(){return this.waypoints[this.waypoints.length-1]},i.findOrCreate=function(t){return o[t.axis][t.name]||new i(t)},n.Group=i}(),function(){"use strict";function t(t){this.$element=e(t)}var e=window.jQuery,i=window.Waypoint;e.each(["innerHeight","innerWidth","off","offset","on","outerHeight","outerWidth","scrollLeft","scrollTop"],function(e,i){t.prototype[i]=function(){var t=Array.prototype.slice.call(arguments);return this.$element[i].apply(this.$element,t)}}),e.each(["extend","inArray","isEmptyObject"],function(i,o){t[o]=e[o]}),i.adapters.push({name:"jquery",Adapter:t}),i.Adapter=t}(),function(){"use strict";function t(t){return function(){var i=[],o=arguments[0];return t.isFunction(arguments[0])&&(o=t.extend({},arguments[1]),o.handler=arguments[0]),this.each(function(){var n=t.extend({},o,{element:this});"string"==typeof n.context&&(n.context=t(this).closest(n.context)[0]),i.push(new e(n))}),i}}var e=window.Waypoint;window.jQuery&&(window.jQuery.fn.waypoint=t(window.jQuery)),window.Zepto&&(window.Zepto.fn.waypoint=t(window.Zepto))}(); \ No newline at end of file diff --git a/ordr3/static/script.js b/ordr3/static/script.js index 7fa48c6..3f4d08c 100644 --- a/ordr3/static/script.js +++ b/ordr3/static/script.js @@ -4,6 +4,22 @@ var capitalize = function(some_string) { } else { return(""); } +}; + +var load_more = function(target) { + var next = target.attr("data-next") + var href = new URL(window.location); + href.searchParams.set('o', next); + + $.get( href, function( response, tmp, request ) { + next_offset = request.getResponseHeader('O3-Next-Offset'); + if (next_offset == "None") { + target.hide() + } else { + target.attr("data-next", next_offset) + } + $(".o3-data-table tbody").append(response) + }); } $(function() { @@ -47,4 +63,7 @@ $(function() { $(target).fadeOut( 100 ).delay( 100 ).fadeIn( 100 ); }); + var infinite = new Waypoint.Infinite({ + element: $('.infinite-container')[0] + }); }); diff --git a/ordr3/static/style.css b/ordr3/static/style.css index 4a4de1a..783b8ff 100644 --- a/ordr3/static/style.css +++ b/ordr3/static/style.css @@ -64,6 +64,7 @@ .o3-head-space .col-2, .o3-head-space .col-5, .o3-head-space .col-10 { padding-top:1rem; + padding-bottom:2rem; } .o3-sidebar { @@ -112,16 +113,26 @@ padding:.5rem; } -td.actions a { +td.o3-actions a { color: #6c757d!important; margin-right:0.5rem; font-size:1rem; } -td.actions a:hover { +td.o3-actions a:hover { color: #343a40!important; } .o3-copy { cursor:pointer; } + + +.o3-data-table th { + border-top:none; + } + + +.infinite-more-link td { + text-align:center; + } diff --git a/ordr3/templates/account/myaccount.jinja2 b/ordr3/templates/account/myaccount.jinja2 index 94bd92f..0ee4b14 100644 --- a/ordr3/templates/account/myaccount.jinja2 +++ b/ordr3/templates/account/myaccount.jinja2 @@ -1,29 +1,18 @@ -{% extends "ordr3:templates/layout_small.jinja2" %} +{% extends "ordr3:templates/layout_full.jinja2" %} {% block subtitle %} My Account {% endblock subtitle %} {% block content %} -
-
-
-
-
-
-

Ordr

-
-
-
Edit your account
- {{form.render()|safe}} -
-

- Change Password -

-
-
-
-
-
+
+
+

Edit your account

+ {{form.render()|safe}} +
+

+ Change Password +

+
{% endblock content %} diff --git a/ordr3/templates/layout_full.jinja2 b/ordr3/templates/layout_full.jinja2 index 0a54d84..24975d5 100644 --- a/ordr3/templates/layout_full.jinja2 +++ b/ordr3/templates/layout_full.jinja2 @@ -14,6 +14,8 @@ + + diff --git a/ordr3/templates/layout_small.jinja2 b/ordr3/templates/layout_small.jinja2 index c4884d7..85ef1c4 100644 --- a/ordr3/templates/layout_small.jinja2 +++ b/ordr3/templates/layout_small.jinja2 @@ -10,7 +10,7 @@ - + diff --git a/ordr3/templates/users/list.jinja2 b/ordr3/templates/users/list.jinja2 new file mode 100644 index 0000000..dd373dd --- /dev/null +++ b/ordr3/templates/users/list.jinja2 @@ -0,0 +1,40 @@ +{% extends "ordr3:templates/layout_full.jinja2" %} + +{% block subtitle %} Manage Users {% endblock subtitle %} + +{% block content %} + +
+ + + +
+
+ + + + + + + + + + + + + {% include 'ordr3:templates/users/list_content.jinja2' %} + +
UsernameFirst NameLast NameEmailRoleActions
+ {% if not users %} +

No data available

+ {% endif %} +
+ + +{% endblock content %} diff --git a/ordr3/templates/users/list_content.jinja2 b/ordr3/templates/users/list_content.jinja2 new file mode 100644 index 0000000..41aa927 --- /dev/null +++ b/ordr3/templates/users/list_content.jinja2 @@ -0,0 +1,24 @@ +{% import 'ordr3:templates/macros.jinja2' as macros with context %} +{% for user in users %} + + {{ user.username }} + {{ user.first_name }} + {{ user.last_name }} + {{ user.email }} + {{ user.role.name.capitalize() }} + + {{ macros.icon("pencil")}} + {{ macros.icon("trash")}} + + +{% endfor %} +{% if next_offset %} + + + + + +{% endif %} diff --git a/ordr3/views/users.py b/ordr3/views/users.py new file mode 100644 index 0000000..12f1027 --- /dev/null +++ b/ordr3/views/users.py @@ -0,0 +1,55 @@ +from sqlalchemy import func +from pyramid.view import view_config + +from .. import models + + +def _get_role(request): + role_param = request.GET.get("role", "") + try: + return models.UserRole[role_param.upper()] + except KeyError: + return None + + +def _get_offset(request): + offset_param = request.GET.get("o", 0) + try: + return int(offset_param) + except ValueError: + return 0 + + +@view_config( + context="ordr3:resources.UserList", + permission="view", + request_method="GET", + renderer="ordr3:templates/users/list.jinja2", +) +@view_config( + context="ordr3:resources.UserList", + permission="view", + request_method="GET", + xhr=True, + renderer="ordr3:templates/users/list_content.jinja2", +) +def list(context, request): + role = _get_role(request) + offset = _get_offset(request) + limit = 12 + + query = request.repo.session.query(models.User) + if role: + query = query.filter(models.User.role == role) + query = query.order_by(func.lower(models.User.username)) + users = query[offset : offset + limit] # noqa: E203 + + next_offset = None if limit != len(users) else (offset + limit) + filter_role = "all" if role is None else role.name.lower() + + return { + "filter_role": filter_role, + "roles": models.UserRole, + "users": users, + "next_offset": next_offset, + }