Skip to content

Commit

Permalink
RW: tweak/expose org needs list, bug fix CRUD, move add-form above
Browse files Browse the repository at this point in the history
list-add row to utilize full width (consistency with summary + usability
with inline subforms)
  • Loading branch information
nursix committed Nov 13, 2015
1 parent a1b14af commit 7d19c51
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 65 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
nursix-1.1.0-devel-3083-gf804ccf (2015-11-13 12:11:39)
nursix-1.1.0-devel-3084-ga1b14af (2015-11-13 17:11:54)
3 changes: 2 additions & 1 deletion modules/s3/s3codecs/svg.py
Expand Up @@ -125,7 +125,8 @@ def encode(self, resource, **attr):
if resource.prefix == "gis" and resource.name == "location":
#list_fields.append("wkt")
list_fields = ["wkt"]
elif "location_id$wkt" not in list_fields:
#elif "location_id$wkt" not in list_fields:
else:
#list_fields.append("location_id$wkt")
list_fields = ["location_id$wkt"]

Expand Down
33 changes: 10 additions & 23 deletions modules/s3/s3crud.py
Expand Up @@ -577,17 +577,6 @@ def read(self, r, **attr):

_config = self._config
editable = _config("editable", True)
list_fields = _config("list_fields")

# List fields
if not list_fields:
fields = resource.readable_fields()
else:
fields = [table[f] for f in list_fields if f in table.fields]
if not fields:
fields = []
if fields[0].name != table.fields[0]:
fields.insert(0, table[table.fields[0]])

# Get the target record ID
record_id = self.record_id
Expand Down Expand Up @@ -745,32 +734,30 @@ def read(self, r, **attr):

elif representation == "csv":
exporter = S3Exporter().csv
return exporter(resource)
output = exporter(resource)

#elif representation == "map":
# exporter = S3Map()
# return exporter(r, **attr)
# output = exporter(r, **attr)

elif representation == "pdf":
exporter = S3Exporter().pdf
return exporter(resource, request=r, **attr)
output = exporter(resource, request=r, **attr)

elif representation == "shp":
list_fields = resource.list_fields()
exporter = S3Exporter().shp
return exporter(resource,
list_fields=list_fields,
**attr)
output = exporter(resource, list_fields=list_fields, **attr)

elif representation == "svg":
list_fields = resource.list_fields()
exporter = S3Exporter().svg
return exporter(resource,
list_fields=list_fields,
**attr)
output = exporter(resource, list_fields=list_fields, **attr)

elif representation == "xls":
list_fields = _config("list_fields")
list_fields = resource.list_fields()
exporter = S3Exporter().xls
return exporter(resource, list_fields=list_fields)
output = exporter(resource, list_fields=list_fields)

elif representation == "json":
exporter = S3Exporter().json
Expand All @@ -782,7 +769,7 @@ def read(self, r, **attr):
else:
tooltip = None

return exporter(resource, tooltip=tooltip)
output = exporter(resource, tooltip=tooltip)

else:
r.error(415, current.ERROR.BAD_FORMAT)
Expand Down
192 changes: 161 additions & 31 deletions modules/templates/RW/config.py
Expand Up @@ -8,7 +8,7 @@
from gluon.contrib.simplejson.ordered_dict import OrderedDict

from gluon import current
from gluon.html import A, URL, TR, TD
from gluon.html import A, DIV, LI, URL, TAG, TD, TR, UL
from gluon.storage import Storage

from s3 import s3_fullname, S3Represent, S3SQLInlineLink, S3SQLSubFormLayout
Expand Down Expand Up @@ -447,37 +447,105 @@ def customise_org_organisation_controller(**attr):
# -------------------------------------------------------------------------
def customise_req_organisation_needs_resource(r, tablename):

if tablename == "req_organisation_needs":
organisation_id = "organisation_id"
s3db = current.s3db

CASH = T("Cash Donations needed")

if r.tablename == "req_organisation_needs":
# Allow only organisations which do not have a needs record
# yet (single component):
table = r.table
field = table.organisation_id
from s3 import IS_ONE_OF
dbset = current.db(table.id == None)
left = table.on(table.organisation_id == current.s3db.org_organisation.id)
field.requires = IS_ONE_OF(dbset, "org_organisation.id",
field.represent,
left = left,
orderby = "org_organisation.name",
sort = True,
)

if r.representation in ("html", "aadata", "iframe"):

# Structured lists for interactive views
from gluon import Field
table = current.s3db.req_organisation_needs
table.needs_skills = Field.Method(lambda row: \
organisation_needs(row, need_type="skills"))
table.needs_items = Field.Method(lambda row: \
organisation_needs(row, need_type="items"))
current.response.s3.stylesheets.append("../themes/RW/needs.css")

needs_skills = (T("Volunteers needed"), "needs_skills")
needs_items = (T("Supplies needed"), "needs_items")

# Filter widgets
from s3 import S3TextFilter, S3OptionsFilter
filter_widgets = [#S3TextFilter(["organisation_id$name",
# ],
# label = T("Search"),
# ),
S3OptionsFilter("organisation_id"),
S3OptionsFilter("organisation_needs_skill.skill_id",
label = T("Skills sought"),
),
S3OptionsFilter("organisation_needs_item.item_id",
label = T("Supplies sought"),
),
]

# CRUD form
from s3 import S3SQLCustomForm, S3SQLInlineComponent
crud_form = S3SQLCustomForm(
"organisation_id",
S3SQLInlineComponent("organisation_needs_skill",
label = T("Volunteers needed"),
fields = ["skill_id",
"demand",
"comments",
],
),
S3SQLInlineComponent("organisation_needs_item",
label = T("Supplies needed"),
fields = ["item_id",
"demand",
"comments",
],
),
(CASH, "money"),
"money_details",
#"vol",
#"vol_details",
)

next_page = r.url(method="") \
if r.tablename == "req_organisation_needs" else None

s3db.configure("req_organisation_needs",
crud_form = crud_form,
filter_widgets = filter_widgets,
create_next = next_page,
update_next = next_page,
)
else:
organisation_id = None

from s3 import S3SQLCustomForm, S3SQLInlineComponent
crud_form = S3SQLCustomForm(
organisation_id,
S3SQLInlineComponent("organisation_needs_skill",
label = T("Volunteers needed"),
fields = ["skill_id",
"demand",
"comments",
],
),
S3SQLInlineComponent("organisation_needs_item",
label = T("Supplies needed"),
fields = ["item_id",
"demand",
"comments",
],
),
(T("Cash Donations needed"), "money"),
"money_details",
"vol",
"vol_details",
)

current.s3db.configure("req_organisation_needs",
crud_form = crud_form,
)
# Simple fields for exports
needs_skills = (T("Volunteers needed"),
"organisation_needs_skill.skill_id")
needs_items = (T("Supplies needed"),
"organisation_needs_item.item_id")

# List fields (all formats)
list_fields = ["organisation_id",
needs_skills,
needs_items,
(CASH, "money"),
(T("Cash Donation Details"), "money_details"),
]

s3db.configure("req_organisation_needs",
list_fields = list_fields,
)

settings.customise_req_organisation_needs_resource = customise_req_organisation_needs_resource

Expand Down Expand Up @@ -782,4 +850,66 @@ def rowstyle(self, form, fields, *args, **kwargs):
parent.append(formstyle(col_id, label, widget, comment))
return TR(parent)

# =============================================================================
demand_options = {1: "Low Demand",
2: "Moderate Demand",
3: "High Demand",
4: "Urgently needed",
}

# =============================================================================
def organisation_needs(row, need_type=None):
"""
Field.Method to render structured organisation needs (list views)
@param row: the row (passed from Field.Method)
@param need_type: the need type (skills|items)
"""

NONE = current.messages["NONE"]

try:
needs = getattr(row, "req_organisation_needs")
except AttributeError:
return NONE
needs_id = needs.id

s3db = current.s3db
if need_type == "skills":
ltable = s3db.req_organisation_needs_skill
stable = s3db.hrm_skill
left = stable.on(stable.id == ltable.skill_id)
elif need_type == "items":
ltable = s3db.req_organisation_needs_item
stable = s3db.supply_item
left = stable.on(stable.id == ltable.item_id)

query = (ltable.organisation_needs_id == needs_id)
rows = current.db(query).select(ltable.demand,
stable.name,
left = left,
)
if not rows:
return NONE

needs = {}
dfield = str(ltable.demand)
nfield = str(stable.name)
for row in rows:
demand = row[dfield]
if demand not in needs:
needs[demand] = [row[nfield]]
else:
needs[demand].append(row[nfield])

T = current.T
output = DIV(_class="org-needs")
for demand in (4, 3, 2, 1):
if demand not in needs:
continue
title = "%s:" % T(demand_options[demand])
items = UL([LI(T(skill)) for skill in needs[demand]])
output.append(TAG[""](title, items))
return output

# END =========================================================================
5 changes: 4 additions & 1 deletion modules/templates/RW/menus.py
Expand Up @@ -31,7 +31,10 @@ def menu_modules(cls):
MM("News", c="cms", f="newsfeed", args="datalist",
icon="icon-news",
),
MM("Current Needs", c="req", f="site_needs", m="summary"),
MM("Current Needs", link=False)(
MM("Facility Needs", c="req", f="site_needs", m="summary"),
MM("Organization Needs", c="req", f="organisation_needs"),
),
MM("Facilities", c="org", f="facility", m="summary"),
MM("Organizations", c="org", f="organisation"),
homepage("gis"),
Expand Down
10 changes: 10 additions & 0 deletions static/themes/RW/needs.css
@@ -0,0 +1,10 @@
.org-needs,
.org-needs ul li {
font-size: 0.8rem;
}
.org-needs {
font-weight: bold;
}
.org-needs ul li {
font-weight: normal;
}
18 changes: 10 additions & 8 deletions views/_list_filter.html
Expand Up @@ -19,6 +19,16 @@
<div class='row'>
<div id='map' class='small-12 columns'></div>
</div>
<div id='list-add' class='row'>
<div class="small-12 columns">
{{try:}}{{=H3(addtitle)}}{{except:}}{{pass}}
{{try:}}{{=addheader}}{{except:}}{{pass}}
{{include "key.html"}}
<div class='form-container'>
{{try:}}{{=form}}{{except:}}{{pass}}
</div>
</div>
</div>
<div class='row'>
<div class='medium-3 large-4 medium-push-9 large-push-8 columns'>
<div id='list-btn-add'>
Expand All @@ -28,14 +38,6 @@
</div>
</div>
<div class='medium-9 large-8 medium-pull-3 large-pull-4 columns'>
<div id='list-add'>
{{try:}}{{=H3(addtitle)}}{{except:}}{{pass}}
{{try:}}{{=addheader}}{{except:}}{{pass}}
{{include "key.html"}}
<div class='form-container'>
{{try:}}{{=form}}{{except:}}{{pass}}
</div>
</div>
{{try:}}{{if list_filter_form:}}
<div id='list-filter' class='form-container'>
{{=list_filter_form}}
Expand Down

0 comments on commit 7d19c51

Please sign in to comment.