Commits

Anonymous committed 88d3307

It is possible to change GPage type. Related fields data
is saved.

  • Participants
  • Parent commits 37d3763

Comments (0)

Files changed (7)

             exclude = GPage.properties().keys()
     return ContentForm
 
+class GTypeForm(forms.Form):
+    def __init__(self, *args, **kwargs):
+        super(GTypeForm, self).__init__(*args, **kwargs)
+
+        self.fields['type'] = djangoforms.forms.ChoiceField(
+                choices=get_namepairs())
+
 class GPageForm(djangoforms.ModelForm):
     class Meta:
         model = GPage

gadmin/models/gpage.py

     for p in get_all():
         yield (str(p.key()), str(p))
 
-@_draft_filter
 def get_all():
     root = get_root()
     return [root,] + get_descendants(root)
 
-@_draft_filter
 def get_branch(page):
     return get_descendants(page)
 
     return get_root()
 
 
-@_draft_filter
 def get_descendants(page, depth=-1):
     if depth == 0:
         return []
         pages += [c] + get_descendants(c, depth)
     return pages
 
-@_draft_filter
 def get_breadcrumb(page):
     tops = []
     top = page
         p.put()
     page.delete()
 
+#def change_type(page, new_type):
+    #if page.class_name == new_type:
+        #return
+
 def reorder(neworder):
     """
     neworder must be [(key, level), (key, level), ..]
 def to_type(page, newtype):
     fields = page.fields()
     args = ((k, getattr(page, k)) for k, v in fields.iteritems())
-    newpage = get_types()[newtype].modelcl(**dict(args))
-    return newpage
+    return get_types()[newtype].modelcl(**dict(args))
+
+def reparent(old_parent, new_parent):
+    for c in old_parent.children_set:
+        c.top = new_parent
+        c.put()
 
 class GPage(polymodel.PolyModel):
     top = db.SelfReferenceProperty(collection_name='children_set')
         url(r'^delete/(?P<page_key>\S+)$', 'delete', name='delete'),
         url(r'^edit/(?P<page_key>\S+)$', 'edit', name='edit'),
         url(r'^reorder$', 'reorder', name='reorder'),
+        url(r'^type/(?P<page_key>\S+)$', 'type', name='type'),
         url(r'^picasa$', 'picasa', name='picasa-all'),
         url(r'^picasa/(?P<album>\S+)$', 'picasa', name='picasa-one'),
 )
 from django.shortcuts import render_to_response, redirect
 from django.core.urlresolvers import reverse
 from gadmin.models import gpage
-from gadmin.forms import GPageForm, form_formclass
+from gadmin.forms import GPageForm, form_formclass, GTypeForm
 from gadmin.account import admin_required
 from google.appengine.ext.db import djangoforms
 from google.appengine.api import urlfetch, users
-
+from django.conf import settings
 
 @admin_required
 def index(request, form=None):
 @admin_required
 def delete(request, page_key):
     page = gpage.GPage.get(page_key)
-    if not page:
-        raise Http404()
+    if not page: raise Http404()
+
     gpage.delete_with_branch(page)
     return redirect('gadmin:index')
 
 @admin_required
 def edit(request, page_key):
     page = gpage.GPage.get(page_key)
-    if not page:
-        raise Http404()
+    if not page: raise Http404()
 
     form = GPageForm((request.POST or None), instance=page)
     additional = types.get_types()[page.__class__.__name__].formcl
 
 @admin_required
 def picasa(request, album=None):
-    guser = users.get_current_user().nickname()
+    nick = users.get_current_user().nickname()
+
+    # Fall back to current user's nickname
+    guser = getattr(settings, 'EAGCMS_PICASA_USER', nick)
     url = 'http://picasaweb.google.com/data/feed/api/user/%s' % guser
     if album:
         url += '/albumid/%s' % (str(album))
         return response
     return HttpResponse("{}")
 
+@admin_required
+def type(request, page_key):
+    page = gpage.GPage.get(page_key)
+    if not page: raise Http404()
+
+    if request.POST:
+        form = GTypeForm(request.POST)
+        if form.is_valid():
+            new_type = form.cleaned_data['type']
+            newpage = gpage.to_type(page, new_type)
+            newpage.put()
+            gpage.reparent(page, newpage)
+            page.delete()
+            return redirect('gadmin:index')
+    else:
+        form = GTypeForm(initial={'type': page.class_name})
+
+    data = {'page': page, 'form': form}
+    return render_to_response('gadmin/type.html', data)
+
 def site(request, slug=""):
     page = gpage.get_by_slug(slug)
-    if not page:
-        raise Http404()
+    if not page: raise Http404()
+
     return page.get_response()
 
 )
 
 # EAGCMS Special options
+# Modules where page types are defined
 EAGCMS_TYPES = (
     'gtypes',
 )
 
+# Optional. picasaweb username
+# that are used in wysiwyg and
+# with PicasaWidget
+EAGCMS_PICASA_USER = 'petraszd'
+

templates/gadmin/index.html

         <a href="{% url gadmin:delete p.key %}">delete</a>
       </div>
       {{ p.title }} ({{ p.level }} - {{ p.order }}), {{ p.class_name }}
+      (<a href="{% url gadmin:type p.key %}">Change type</a>)
     </li>
   {% endfor %}
   </ul>

templates/gadmin/type.html

+{% extends "gadmin/base.html" %}
+{% block gd_title %} TODO {% endblock %}
+{% block gd_content %}
+  <h3>Change Type Of » {{ page.title }}</h1>
+  TODO: Warning
+  <form action="{% url gadmin:type page.key %}" method="POST">
+    {{ form.as_p }}
+    <p>
+      <input type="submit" value="Change" />
+    </p>
+  </form>
+{% endblock %}