| | 438 | #___________________________________________________________________________ |
| | 439 | |
| | 440 | def _save_sequencing(self, page_data): |
| | 441 | """ |
| | 442 | Save the new position weight for every pages, if changed. |
| | 443 | """ |
| | 444 | no_changes = True |
| | 445 | for page in page_data: |
| | 446 | try: |
| | 447 | page_id = str(page.id) |
| | 448 | #page_id = "Not exists test" |
| | 449 | weight = self.request.POST[page_id] |
| | 450 | #weight = "Not a number test" |
| | 451 | weight = int(weight) |
| | 452 | except (KeyError, ValueError), msg: |
| | 453 | self.page_msg.red(_("Error: Wrong POST data!")) |
| | 454 | if settings.DEBUG: |
| | 455 | self.page_msg.red("Debug: %s" % msg) |
| | 456 | # abort! |
| | 457 | return |
| | 458 | |
| | 459 | if page.position == weight: |
| | 460 | # No change needed for this page |
| | 461 | continue |
| | 462 | |
| | 463 | # Set new position weight and save: |
| | 464 | page.position = weight |
| | 465 | page.save() |
| | 466 | |
| | 467 | no_changes = False |
| | 468 | self.page_msg.green( |
| | 469 | _("Set position weight %s for page '%s', ok.") % ( |
| | 470 | weight, page.name |
| | 471 | ) |
| | 472 | ) |
| | 473 | |
| | 474 | if no_changes: |
| | 475 | # Give feedback for the user, if nothing to do... |
| | 476 | self.page_msg.green(_("Nothing to change ;)")) |
| | 477 | |
| | 478 | |
| | 479 | def _get_sequence_data(self, url_args): |
| | 480 | """ |
| | 481 | returned the pages and the "use"-mode. |
| | 482 | Based on the url, the pages are from the "parent", "current", "child" |
| | 483 | level. |
| | 484 | """ |
| | 485 | # Start with "current" level. Only if the url starts with "parent" or |
| | 486 | # "child" change the level. |
| | 487 | use = "current" |
| | 488 | if url_args != None: |
| | 489 | if url_args.startswith("parent"): |
| | 490 | use = "parent" |
| | 491 | parent_page = Page.objects.get( |
| | 492 | id=self.current_page.parent.id |
| | 493 | ) |
| | 494 | parent_filter = parent_page.parent |
| | 495 | elif url_args.startswith("child"): |
| | 496 | use = "child" |
| | 497 | parent_filter = self.current_page.id |
| | 498 | |
| | 499 | if use == "current": |
| | 500 | # "url_args == None" or the url is not well-formed: It doesn't |
| | 501 | # starts with "parent" or "child" |
| | 502 | parent_filter = self.current_page.parent |
| | 503 | |
| | 504 | # Change a "parent__exact=None" query to "parent__isnull=True" |
| | 505 | # see: http://www.djangoproject.com/documentation/db-api/#isnull |
| | 506 | if parent_filter == None: |
| | 507 | filter_kwargs = {"parent__isnull": True} |
| | 508 | else: |
| | 509 | filter_kwargs = {"parent": parent_filter} |
| | 510 | |
| | 511 | page_data = Page.objects.filter(**filter_kwargs) |
| | 512 | |
| | 513 | return use, page_data |
| | 514 | |
| | 515 | |
| | 516 | def sequencing(self, url_args=None): |
| | 517 | """ |
| | 518 | Set the position weight of the cms page to change the correct order. |
| | 519 | TODO: Save the weight range in the preferences |
| | 520 | """ |
| | 521 | use, page_data = self._get_sequence_data(url_args) |
| | 522 | |
| | 523 | if self.request.method == 'POST': |
| | 524 | # Save new position weight |
| | 525 | self._save_sequencing(page_data) |
| | 526 | |
| | 527 | # Order the pages *after* a sended POST data processed. |
| | 528 | page_data = page_data.order_by("position") |
| | 529 | |
| | 530 | childs = Page.objects.filter(parent=self.current_page.id).count() |
| | 531 | has_childs = childs != 0 |
| | 532 | |
| | 533 | parents = Page.objects.filter(parent=self.current_page.parent).count() |
| | 534 | has_parents = parents != 0 |
| | 535 | |
| | 536 | # self.page_msg("url_args:", url_args) |
| | 537 | # self.page_msg("use:", use) |
| | 538 | # self.page_msg("filter_kwargs:", filter_kwargs) |
| | 539 | # self.page_msg("has_childs:", has_childs, " - has_parents:", has_parents) |
| | 540 | |
| | 541 | context = { |
| | 542 | "sequencing_data": page_data, |
| | 543 | "weights": range(-10, 10), |
| | 544 | |
| | 545 | "base_url": self.URLs.methodLink("sequencing"), |
| | 546 | |
| | 547 | "has_childs": has_childs, |
| | 548 | "has_parents": has_parents, |
| | 549 | } |
| | 550 | context["use_%s" % use] = True |
| | 551 | |
| | 552 | self._render_template("sequencing", context)#, debug=True) |
| | 553 | |