How to replicate/copy complex pages

84 views
Skip to first unread message

Brygg Ullmer

unread,
Jan 25, 2015, 1:00:48 AM1/25/15
to mezzani...@googlegroups.com
Greetings!

We've been greatly enjoying getting going with Mezzanine toward creating several new web spaces.  One challenge we've run into is how to replicate/duplicate/copy a complex page, for subsequent modification.  E.g., the following page:

https://cc.cct.lsu.edu/groups/tangviz/papers/

... uses the "codex" MezzaTheme portfolio page type.  


We'd like to make a series of copies that preserve subsets of the contents, but haven't found a mechanism for this.  Can anyone advice on the cleanest way (ideally through the admin web UI) to realize this?

Appreciated!

Brygg Ullmer
Louisiana State University

Josh Cartmell

unread,
Jan 26, 2015, 11:14:12 AM1/26/15
to mezzani...@googlegroups.com
Hi Brygg, unfortunately I don't think there is a way to do what you described right now.  To be clear, you would want to do something like:

Page 1 | Item 1, Item 2, Item 3, Item 4

and then create a copy like:

Page 1 Copy | Item 1, Item 3, Item 4

Is that correct?

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brygg Ullmer

unread,
Jan 27, 2015, 1:07:08 AM1/27/15
to mezzani...@googlegroups.com
Josh,

Many thanks for your message -- warmly appreciated!

Yes, your description is accurate.  After posting my message, I realized aspects may be specific to the Codex MezzaTheme.  In a dump of the MySQL dbase, I see:

REPLACE INTO `codex_portfolioitem` (`id`, `_order`, `portfolio_id`, `content`, `title`, `href`, `featured_image`) VALUES

... followed by the content of the associations.  While I suspect there are both Python and MySQL (PostgreSQL, etc.) ways of doing this, am I correct in gathering a SQL query of the existing page items, followed by a SQL insertion of said items in association with the new page "copy," may be the current "state of the art?"

Warmly appreciated!

Brygg

Josh Cartmell

unread,
Jan 27, 2015, 11:09:30 AM1/27/15
to mezzani...@googlegroups.com
I'm sure that you could do it directly with SQL but you also could write a script that could do it with Django's orm, something like (I haven't tested this so it may require some tweaking):

from copy import deepcopy

def copy_page(portfolio):

    portfolio_copy = deepcopy(portfolio)
    portfolio_copy.id = None
    portfolio_copy.save()

    for item in portfolio.items.all():
        # replace the next line with actual logic that determines if the item shouldn't be copied
        if (do_not_copy):
            continue
        item_copy = deepcopy(item)
        item_copy.portfolio = portfolio_copy
        item_copy.save()

If you wanted to get really fancy you could even hook this up to the admin somehow, rather than running it at the command line

Brygg Ullmer

unread,
Jan 28, 2015, 1:22:30 AM1/28/15
to mezzani...@googlegroups.com
Josh,

Many thanks for the very helpful code example!  Using such a variation (as opposed to direct SQL) does appear cleaner and more in keeping with Django.

My apologies for the newcomer question, but -- per these links:


... am I correct in understanding that Django orm scripts of the style you illustrate/describe may typically be accessed with a call like:

python /usr/lib/python2.7/site-packages/django/bin/django-admin.py shell --plain --settings=./mysite.settings

followed by code that begins with:

from django.test import Client

Warmly appreciated!

Brygg 

Ken Bolton

unread,
Jan 28, 2015, 9:18:28 AM1/28/15
to mezzanine-users
Hi Brygg,

Your understanding is correct, though in my experience more often formulated so:

$ python manage.py shell

Let me take a moment to plug the excellent django-extensions module. In your python environment, `pip install django-extensions` will download and add the module to your python path. Mezzanine (and Cartridge) will take advantage of this module if installed.

$ python manage.py shell_plus

The above command will get your python shell up and load your models for you. It can be a huge time-saver, particularly if you are making changes and trying to test them in the shell.

hth!

- ken

--

Brygg Ullmer

unread,
Jan 30, 2015, 1:00:24 AM1/30/15
to mezzani...@googlegroups.com
Kenneth,

Many thanks for this!  I have installed django-extensions; it looks very promising.  Based on your and Josh's advice, I'm now tending to dump the codex_portfolioitem table to JSON or YAML, then use a variation of your two django-native suggestions to selectively import items back to the target pages.  I'll update (probably in a week or so) as this rolls out.

with appreciation,

Brygg
Reply all
Reply to author
Forward
0 new messages