Skip to content

Commit

Permalink
cms: cms_Calendar working
Browse files Browse the repository at this point in the history
  • Loading branch information
flavour committed Jan 12, 2017
1 parent 0d494ce commit e0d2a8b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 25 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
nursix-dev-738-g6630449 (2017-01-12 09:50:54)
0d494ce (2017-01-12 16:12:42)
96 changes: 79 additions & 17 deletions modules/s3db/cms.py
Expand Up @@ -38,6 +38,7 @@
"cms_customise_post_fields",
"cms_post_list_layout",
"S3CMS",
#"cms_Calendar",
)

import datetime
Expand Down Expand Up @@ -1861,21 +1862,55 @@ def _extract(self, days, r, **attr):
resource.add_filter((FS("date") > days[0].replace(hour = 0, minute=0, second=0, microsecond=0)) & \
(FS("date") < days[-1].replace(hour = 23, minute=59, second=59)))

# @ToDo: Configurable fields (location_id not always relevant, but e.g. Author maybe)
fields = ["name",
# @ToDo: Configurable fields (location_id not always relevant, but e.g. Author may be)
fields = ["body",
"date",
"location_id",
"post_module.module",
"post_module.resource",
"post_module.record",
]

data = resource.select(fields)
data = resource.select(fields, represent=True, raw_data=True)

# @ToDo: Reformat posts into Array by day & return the maximum number of Posts in a day
posts = []
pappend = posts.append
# Reformat posts into Array of rows and columns (days)
# Need to start with the columns as we don't yet know how many rows we need
cols = []
cappend = cols.append
max_rows = []
mappend = max_rows.append
# Initialise arrays
for day in days:
pappend()
cappend([])
mappend(0)

# Place each record into the correct day's column
len_days = len(days)
for record in data.rows:
date = record._row["cms_post.date"]
for i in range(len_days):
if date < days[i + 1]:
cols[i].append(record)
max_rows[i] += 1
break

# Now convert to rows
rows = []
rappend = rows.append
len_rows = max(max_rows)
# Initialise array
for i in range(len_rows):
rappend([])

for row in rows:
rappend = row.append
for col in cols:
if len(col):
rappend(col.pop())
else:
rappend(Storage())

return posts
return rows

# -------------------------------------------------------------------------
def html(self, r, **attr):
Expand Down Expand Up @@ -1907,15 +1942,17 @@ def html(self, r, **attr):
rappend(TD(day.strftime("%A")))
item.append(title_row)

# @ToDo: Represent Posts as Cards
# - e.g. using cms_post_list_layout?
data_row = TR()
rappend = data_row.append
i = 0
for day in days:
rappend(TD(posts[i].body))
i += 1
item.append(data_row)
for row in posts:
data_row = TR()
rappend = data_row.append
row.reverse()
for day in days:
post = row.pop()
if post:
rappend(TD(self.post_layout(post)))
else:
rappend(TD())
item.append(data_row)

output = dict(item=item)
output["title"] = T("Weekly Schedule")
Expand All @@ -1929,4 +1966,29 @@ def html(self, r, **attr):
current.response.view = "simple.html"
return output

# -------------------------------------------------------------------------
@staticmethod
def post_layout(post):
"""
Format a calendar entry
"""

title = post["cms_post.body"]
record_id = post["cms_post_module.record"]
if record_id:
title = A(title,
_href = URL(c = str(post["cms_post_module.module"]),
f = str(post["cms_post_module.resource"]),
args = record_id,
),
_target = "_blank",
)

location = post["cms_post.location_id"]

return DIV(title,
BR(),
location,
)

# END =========================================================================
38 changes: 31 additions & 7 deletions modules/templates/SCPHIMS/config.py
Expand Up @@ -124,12 +124,6 @@ def config(settings):
#settings.mail.tls = True
#settings.mail.login = "username:password"

# =========================================================================
# CMS
#

settings.cms.richtext = True

# =========================================================================
# Mobile
#
Expand All @@ -138,6 +132,35 @@ def config(settings):
}),
]

# =========================================================================
# CMS
#

settings.cms.richtext = True

# -------------------------------------------------------------------------
def customise_cms_post_controller(**attr):

s3 = current.response.s3
standard_prep = s3.prep
def custom_prep(r):
# Call standard prep
if callable(standard_prep):
if not standard_prep(r):
return False

if r.method == "calendar":
# Only show entries related to Activities & Assessments
from s3 import FS
r.resource.add_filter(FS("post_module.module").belongs(("project", "dc")))

return True
s3.prep = custom_prep

return attr

settings.customise_cms_post_controller = customise_cms_post_controller

# =========================================================================
# Data Collection
#
Expand Down Expand Up @@ -204,7 +227,8 @@ def customise_dc_target_resource(r, tablename):

# Always at L3
from s3 import S3LocationSelector
table.location_id.widget = S3LocationSelector(levels=("L1", "L2", "L3"))
table.location_id.widget = S3LocationSelector(levels=("L1", "L2", "L3"),
show_map=False)

has_role = current.auth.s3_has_role
if has_role("ERT_LEADER") or has_role("HUM_MANAGER"):
Expand Down

0 comments on commit e0d2a8b

Please sign in to comment.