Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.utils.translation import gettext_lazy as _ 

2from django.core.paginator import Paginator, InvalidPage 

3from django.http import Http404 

4 

5from cms.plugin_base import CMSPluginBase 

6from cms.plugin_pool import plugin_pool 

7from djangocms_link.cms_plugins import LinkPlugin 

8from djangocms_picture.cms_plugins import PicturePlugin 

9from djangocms_picture.models import Picture 

10 

11from watson import search as watson 

12 

13from . import models 

14 

15module_name = 'Unimelb' 

16 

17@plugin_pool.register_plugin 

18class UnimelbMessagesPlugin(CMSPluginBase): 

19 """ 

20 Displays Django system messages using the Unimelb notices system. 

21 

22 https://docs.djangoproject.com/en/3.2/ref/contrib/messages/ 

23 https://web.unimelb.edu.au/components/notices 

24 """ 

25 model = models.UnimelbMessages 

26 name = _('Messages') 

27 module = module_name 

28 render_template = "djangocms_unimelb/messages.html" 

29 

30 

31@plugin_pool.register_plugin 

32class UnimelbLeadPlugin(CMSPluginBase): 

33 """ 

34 Displays text in a lead section. 

35 

36 https://web.unimelb.edu.au/components/text 

37 """ 

38 model = models.UnimelbLead 

39 name = _('Lead') 

40 module = module_name 

41 render_template = "djangocms_unimelb/lead.html" 

42 allow_children = True 

43 

44 

45@plugin_pool.register_plugin 

46class UnimelbNoticesPlugin(CMSPluginBase): 

47 """ 

48 https://web.unimelb.edu.au/components/notices 

49 """ 

50 model = models.UnimelbNotice 

51 name = _('Notice') 

52 module = module_name 

53 render_template = "djangocms_unimelb/notice.html" 

54 allow_children = True 

55 

56 

57@plugin_pool.register_plugin 

58class UnimelbTabPlugin(CMSPluginBase): 

59 """ 

60 Creates a tab using the Unimelb theme. 

61 

62 See https://web.unimelb.edu.au/components/tabs 

63 """ 

64 model = models.UnimelbTab 

65 name = _('Tabs') 

66 module = _('Unimelb') 

67 # change_form_template = 'djangocms_unimelb/admin/tabs.html' 

68 allow_children = True 

69 child_classes = ['UnimelbTabItemPlugin'] 

70 render_template = "djangocms_unimelb/tab.html" 

71 

72 fieldsets = [ 

73 (None, { 

74 'fields': ( 

75 ('tab_type','tab_index'), 

76 ) 

77 }), 

78 ] 

79 

80 def render(self, context, instance, placeholder): 

81 context.update({'instance': instance}) 

82 return context 

83 

84 

85@plugin_pool.register_plugin 

86class UnimelbTabItemPlugin(CMSPluginBase): 

87 """ 

88 Creates an item in a tab using the Unimelb theme. 

89 

90 See https://web.unimelb.edu.au/components/tabs 

91 """ 

92 model = models.UnimelbTabItem 

93 name = _('Tab item') 

94 module = _('Unimelb') 

95 # change_form_template = 'djangocms_unimelb/admin/tabs.html' 

96 allow_children = True 

97 parent_classes = ['UnimelbTabPlugin'] 

98 render_template = "djangocms_unimelb/render_children.html" 

99 

100 fieldsets = [ 

101 (None, { 

102 'fields': ( 

103 'tab_title', 

104 ) 

105 }), 

106 ] 

107 

108 def render(self, context, instance, placeholder): 

109 context.update({'instance': instance}) 

110 return context 

111 

112 

113@plugin_pool.register_plugin 

114class UnimelbStaffListingPlugin(CMSPluginBase): 

115 """ 

116 Creates a staff listing using the Unimelb theme. 

117 

118 See https://web.unimelb.edu.au/components/tabs 

119 """ 

120 model = models.UnimelbStaffListing 

121 name = _('Staff Listing') 

122 module = _('Unimelb') 

123 allow_children = True 

124 child_classes = ['UnimelbStaffMemberPlugin'] 

125 render_template = "djangocms_unimelb/staff_listing.html" 

126 

127 def get_render_template(self, context, instance, placeholder): 

128 if instance.listing_type == 'detailed': 

129 return 'djangocms_unimelb/staff_listing_detailed.html' 

130 elif instance.listing_type == 'min': 

131 return 'djangocms_unimelb/staff_listing_min.html' 

132 

133 return 'djangocms_unimelb/staff_listing_default.html' 

134 

135 def render(self, context, instance, placeholder): 

136 context.update({'instance': instance}) 

137 return context 

138 

139 

140@plugin_pool.register_plugin 

141class UnimelbStaffMemberPlugin(CMSPluginBase): 

142 """ 

143 Adds a staff member to a staff listing using the Unimelb theme. 

144 

145 See https://web.unimelb.edu.au/components/tabs 

146 """ 

147 model = models.UnimelbStaffMember 

148 name = _('Staff Member') 

149 module = _('Unimelb') 

150 allow_children = False 

151 parent_classes = ['UnimelbStaffListingPlugin'] 

152 

153 def get_render_template(self, context, instance, placeholder): 

154 parent_instance, plugin_class = instance.parent.get_plugin_instance() 

155 

156 if parent_instance.listing_type == 'detailed': 

157 return 'djangocms_unimelb/staff_member_detailed.html' 

158 elif parent_instance.listing_type == 'min': 

159 return 'djangocms_unimelb/staff_member_min.html' 

160 

161 return 'djangocms_unimelb/staff_member_default.html' 

162 

163 

164@plugin_pool.register_plugin 

165class UnimelbHeadingPlugin(CMSPluginBase): 

166 """ 

167 See https://web.unimelb.edu.au/components/headings 

168 """ 

169 model = models.UnimelbHeading 

170 name = _('Heading') 

171 module = _('Unimelb') 

172 allow_children = False 

173 render_template = "djangocms_unimelb/heading.html" 

174 

175 def render(self, context, instance, placeholder): 

176 # If there are no heading then get it from the current page or current tab 

177 if not instance.text: 

178 # Check for tab item 

179 tab_item_plugin = instance.get_ancestors().filter(plugin_type='UnimelbTabItemPlugin').first() 

180 if tab_item_plugin: 

181 tab_item = tab_item_plugin.get_plugin_instance()[0] 

182 instance.text = str(tab_item) 

183 else: 

184 # If not tab item then user page title 

185 request = context['request'] 

186 current_page = request.current_page 

187 instance.text = current_page.get_page_title() 

188 

189 return super().render(context, instance, placeholder) 

190 

191 

192 

193@plugin_pool.register_plugin 

194class UnimelbHeaderPlugin(CMSPluginBase): 

195 """ 

196 https://web.unimelb.edu.au/components/headers 

197 """ 

198 model = models.UnimelbHeader 

199 name = _('Header') 

200 module = _('Unimelb') 

201 allow_children = True 

202 render_template = "djangocms_unimelb/header.html" 

203 

204 

205@plugin_pool.register_plugin 

206class UnimelbBlockListingPlugin(CMSPluginBase): 

207 """ 

208 Creates a block listing using the Unimelb theme. 

209 

210 See https://web.unimelb.edu.au/components/block-listing 

211 """ 

212 model = models.UnimelbBlockListing 

213 name = _('Block Listing') 

214 module = _('Unimelb') 

215 allow_children = True 

216 child_classes = ['UnimelbBlockListingItemNewsPlugin'] 

217 render_template = "djangocms_unimelb/blocklisting.html" 

218 

219 

220@plugin_pool.register_plugin 

221class UnimelbBlockListingItemNewsPlugin(CMSPluginBase): 

222 """ 

223 Creates a block listing using the Unimelb theme. 

224 

225 See https://web.unimelb.edu.au/components/block-listing 

226 """ 

227 model = models.UnimelbBlockListingItem 

228 name = _('Block Listing Item') 

229 module = _('Unimelb') 

230 allow_children = True 

231 parent_classes = ['UnimelbBlockListingPlugin'] 

232 child_classes = ['UnimelbBlockListingImagePlugin'] 

233 render_template = "djangocms_unimelb/blocklistingitemnews.html" 

234 fields = ( 

235 'category', 

236 ('heading','minor_heading'), 

237 'text', 

238 'call_to_action', 

239 ('double', 'image_location'), 

240 ('meta_left', 'meta_right'), 

241 'picture', 

242 'external_picture', 

243 ('link_url', 'link_page'), 

244 ('file'), 

245 'link_target', 

246 'link_attributes', 

247 ) 

248 # fieldsets = PicturePlugin.fieldsets.copy() 

249 fieldsets = [( 

250 None, { 

251 'fields': fields 

252 } 

253 )] 

254 

255 

256@plugin_pool.register_plugin 

257class UnimelbBlockListingImagePlugin(CMSPluginBase): 

258 """ 

259 Creates a block listing using the Unimelb theme. 

260 """ 

261 model = Picture 

262 name = _('Block Listing Image') 

263 module = _('Unimelb') 

264 render_template = "djangocms_unimelb/blocklistingimage.html" 

265 parent_classes = ['UnimelbBlockListingItemNews'] 

266 allow_children = False 

267 

268 

269 fieldsets = [ 

270 (None, { 

271 'fields': ( 

272 'picture', 

273 'external_picture', 

274 ) 

275 }), 

276 ] 

277 

278 

279@plugin_pool.register_plugin 

280class UnimelbFooterPlugin(CMSPluginBase): 

281 """ 

282 Creates a footer with a link to social media sites. 

283 """ 

284 model = models.UnimelbFooter 

285 name = _('Footer') 

286 module = _('Unimelb') 

287 allow_children = True 

288 child_classes = ['UnimelbFooterLinkPlugin'] 

289 render_template = "djangocms_unimelb/footer.html" 

290 

291 

292@plugin_pool.register_plugin 

293class UnimelbFooterLinkPlugin(CMSPluginBase): 

294 """ 

295 Creates a footer with a link to social media sites. 

296 """ 

297 model = models.UnimelbFooterLink 

298 name = _('Footer Link') 

299 module = _('Unimelb') 

300 parent_classes = ['UnimelbFooterPlugin'] 

301 render_template = "djangocms_unimelb/footer_link.html" 

302 

303 fields = ( 

304 ('text', 'icon'), 

305 ('external_link', 'internal_link'), 

306 ) 

307 fieldsets = LinkPlugin.fieldsets.copy() 

308 fieldsets[0] = ( 

309 None, { 

310 'fields': fields 

311 } 

312 ) 

313 

314@plugin_pool.register_plugin 

315class UnimelbSearchFormPlugin(LinkPlugin): 

316 """ 

317 Creates an input form. 

318 

319 See https://web.unimelb.edu.au/components/search 

320 """ 

321 model = models.UnimelbSearchForm 

322 name = _('Search Form') 

323 module = _('Unimelb') 

324 allow_children = False 

325 

326 fields = ( 

327 ('external_link', 'internal_link'), 

328 ) 

329 fieldsets = LinkPlugin.fieldsets.copy() 

330 fieldsets[0] = ( 

331 None, { 

332 'fields': fields 

333 } 

334 ) 

335 

336 # Need to override this function 

337 def get_render_template(self, context, instance, placeholder): 

338 return "djangocms_unimelb/search_form.html" 

339 

340 

341@plugin_pool.register_plugin 

342class UnimelbSearchResultsPlugin(CMSPluginBase): 

343 """ 

344 Creates a search results box using the Unimelb theme. 

345 """ 

346 model = models.UnimelbSearchResults 

347 name = _('Search Results') 

348 module = _('Unimelb') 

349 render_template = "djangocms_unimelb/search_results.html" 

350 

351 def render(self, context, instance, placeholder): 

352 request = context['request'] 

353 query_param = "q" 

354 

355 query = request.GET.get(query_param, "").strip() 

356 qs = watson.search( query ) 

357 paginator = Paginator( qs, instance.pagination ) 

358 page_kwarg = 'page' 

359 page = request.GET.get(page_kwarg) or 1 

360 try: 

361 page_number = int(page) 

362 except ValueError: 

363 if page == 'last': 

364 page_number = paginator.num_pages 

365 else: 

366 raise Http404(_('Page is not “last”, nor can it be converted to an int.')) 

367 

368 try: 

369 page_obj = paginator.page(page_number) 

370 except InvalidPage as e: 

371 raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { 

372 'page_number': page_number, 

373 'message': str(e) 

374 }) 

375 

376 context.update(dict( 

377 query=query, 

378 search_results=page_obj.object_list, 

379 paginator=paginator, 

380 page_obj=page_obj, 

381 )) 

382 

383 return super().render(context, instance, placeholder) 

384 

385 

386@plugin_pool.register_plugin 

387class UnimelbAccordionPlugin(CMSPluginBase): 

388 """ 

389 Creates an accordion component using the Unimelb theme. 

390 

391 https://web.unimelb.edu.au/components/accordion 

392 https://resources.web.unimelb.edu.au/web-content/creating-content/how-to-use-accordions-well 

393 """ 

394 model = models.UnimelbAccordion 

395 name = _('Accordion') 

396 module = _('Unimelb') 

397 allow_children = True 

398 child_classes = ['UnimelbAccordionItemPlugin'] 

399 render_template = "djangocms_unimelb/accordion.html" 

400 

401 

402@plugin_pool.register_plugin 

403class UnimelbAccordionItemPlugin(CMSPluginBase): 

404 """ 

405 Creates an accordion component using the Unimelb theme. 

406 

407 https://web.unimelb.edu.au/components/accordion 

408 https://resources.web.unimelb.edu.au/web-content/creating-content/how-to-use-accordions-well 

409 """ 

410 model = models.UnimelbAccordionItem 

411 name = _('Accordion Item') 

412 module = _('Unimelb') 

413 allow_children = True 

414 parent_classes = ['UnimelbAccordionPlugin'] 

415 render_template = "djangocms_unimelb/accordion_item.html" 

416 

417 

418@plugin_pool.register_plugin 

419class UnimelbTimelinePlugin(CMSPluginBase): 

420 """ 

421 Creates an timeline component using the Unimelb theme. 

422 

423 https://web.unimelb.edu.au/components/timeline 

424 https://www.w3schools.com/tags/tag_dl.asp 

425 """ 

426 model = models.UnimelbTimeline 

427 name = _('Timeline') 

428 module = _('Unimelb') 

429 allow_children = True 

430 child_classes = ['UnimelbTimelineItemPlugin'] 

431 render_template = "djangocms_unimelb/timeline.html" 

432 

433 

434@plugin_pool.register_plugin 

435class UnimelbTimelineItemPlugin(CMSPluginBase): 

436 """ 

437 Creates an timeline component using the Unimelb theme. 

438 

439 https://web.unimelb.edu.au/components/timeline 

440 https://www.w3schools.com/tags/tag_dl.asp 

441 """ 

442 model = models.UnimelbTimelineItem 

443 name = _('Timeline Item') 

444 module = _('Unimelb') 

445 allow_children = True 

446 parent_classes = ['UnimelbTimelinePlugin'] 

447 render_template = "djangocms_unimelb/timeline_item.html" 

448 

449 

450@plugin_pool.register_plugin 

451class UnimelbBlockquotePlugin(CMSPluginBase): 

452 """ 

453 https://web.unimelb.edu.au/components/text 

454 """ 

455 model = models.UnimelbBlockquote 

456 name = _('Blockquote') 

457 module = _('Unimelb') 

458 render_template = "djangocms_unimelb/blockquote.html" 

459 

460 

461@plugin_pool.register_plugin 

462class UnimelbFigureDivPlugin(CMSPluginBase): 

463 """ 

464 https://web.unimelb.edu.au/components/figure 

465 """ 

466 model = models.UnimelbFigureDiv 

467 name = _('Figure Div') 

468 module = _('Unimelb') 

469 render_template = "djangocms_unimelb/figure_div.html" 

470 allow_children = True 

471 

472 

473@plugin_pool.register_plugin 

474class UnimelbFigurePlugin(CMSPluginBase): 

475 """ 

476 https://web.unimelb.edu.au/components/figure 

477 """ 

478 model = models.UnimelbFigure 

479 name = _('Figure') 

480 module = _('Unimelb') 

481 render_template = "djangocms_unimelb/figure.html" 

482 text_enabled = True 

483 

484 fieldsets = [ 

485 (None, { 

486 'fields': ( 

487 'picture', 

488 'external_picture', 

489 ('inset', 'confined','size'), 

490 'caption', 

491 ) 

492 }), 

493 (_('Advanced settings'), { 

494 'classes': ('collapse',), 

495 'fields': ( 

496 'template', 

497 'use_responsive_image', 

498 ('width', 'height'), 

499 'alignment', 

500 'attributes', 

501 ) 

502 }), 

503 (_('Link settings'), { 

504 'classes': ('collapse',), 

505 'fields': ( 

506 ('link_url', 'link_page'), 

507 'link_target', 

508 'link_attributes', 

509 ) 

510 }), 

511 (_('Cropping settings'), { 

512 'classes': ('collapse',), 

513 'fields': ( 

514 ('use_automatic_scaling', 'use_no_cropping'), 

515 ('use_crop', 'use_upscale'), 

516 'thumbnail_options', 

517 ) 

518 }) 

519 ] 

520 

521 

522@plugin_pool.register_plugin 

523class UnimelbPathfinderPlugin(CMSPluginBase): 

524 """ 

525 Creates a pathfinder using the Unimelb theme. 

526 

527 See https://web.unimelb.edu.au/components/pathfinder 

528 """ 

529 model = models.UnimelbPathfinder 

530 name = _('Pathfinder') 

531 module = _('Unimelb') 

532 allow_children = True 

533 child_classes = ['UnimelbPathfinderBoxPlugin'] 

534 render_template = "djangocms_unimelb/pathfinder.html" 

535 

536 def render(self, context, instance, placeholder): 

537 children_count = len(instance.child_plugin_instances) 

538 

539 # If there are no children pathfinder boxes then make some pseudo ones from the page hierarchy 

540 if children_count == 0: 

541 request = context['request'] 

542 current_page = request.current_page 

543 instance.boxes = [] 

544 for child in current_page.get_child_pages(): 

545 instance.boxes.append(dict( 

546 heading=child.get_menu_title(), 

547 text=child.get_meta_description(), 

548 get_link=f"/{child.get_path()}", 

549 button="View", 

550 )) 

551 children_count = len(instance.boxes) 

552 

553 number = children_count 

554 number = max(number, 2) 

555 number = min(number, 4) 

556 context.update({'number': number}) 

557 return super().render(context, instance, placeholder) 

558 

559 

560@plugin_pool.register_plugin 

561class UnimelbPathfinderBoxPlugin(LinkPlugin): 

562 """ 

563 Creates a pathfinder box using the Unimelb theme. 

564 

565 See https://web.unimelb.edu.au/components/pathfinder 

566 """ 

567 model = models.UnimelbPathfinderBox 

568 name = _('Pathfinder Box') 

569 module = _('Unimelb') 

570 parent_classes = ['UnimelbPathfinderPlugin'] 

571 allow_children = False 

572 

573 fields = ( 

574 'heading', 

575 'text', 

576 'button', 

577 ('external_link', 'internal_link'), 

578 ) 

579 fieldsets = LinkPlugin.fieldsets.copy() 

580 fieldsets[0] = ( 

581 None, { 

582 'fields': fields 

583 } 

584 ) 

585 

586 # Need to override this function 

587 def get_render_template(self, context, instance, placeholder): 

588 return "djangocms_unimelb/pathfinder_box.html" 

589 

590