26 |
26 |
27 from django import http |
27 from django import http |
28 from django import newforms as forms |
28 from django import newforms as forms |
29 from django.utils.translation import ugettext_lazy |
29 from django.utils.translation import ugettext_lazy |
30 |
30 |
31 from soc.logic import document |
31 import soc.logic |
32 from soc.logic import out_of_band |
32 from soc.logic import out_of_band |
33 from soc.logic import path_link_name |
33 from soc.logic import path_link_name |
34 from soc.logic.helper import access |
34 from soc.logic.helper import access |
35 from soc.logic.site import id_user |
35 from soc.logic.site import id_user |
36 from soc.views import helper |
36 from soc.views import helper |
74 |
74 |
75 SUBMIT_MESSAGES = ( |
75 SUBMIT_MESSAGES = ( |
76 ugettext_lazy('Document saved.'), |
76 ugettext_lazy('Document saved.'), |
77 ) |
77 ) |
78 |
78 |
79 def edit(request, partial_path=None, linkname=None, |
79 def getDocForForm(form): |
|
80 """Extracts doc fields from a form and creates a new doc from it |
|
81 """ |
|
82 |
|
83 user = users.get_current_user() |
|
84 if user: |
|
85 email = user.email() |
|
86 else: |
|
87 email = None |
|
88 |
|
89 partial_path = form.cleaned_data.get('partial_path') |
|
90 link_name = form.cleaned_data.get('link_name') |
|
91 |
|
92 properties = {} |
|
93 properties['partial_path'] = partial_path |
|
94 properties['link_name'] = link_name |
|
95 properties['title'] = form.cleaned_data.get('title') |
|
96 properties['short_name'] = form.cleaned_data.get('short_name') |
|
97 properties['abstract'] = form.cleaned_data.get('abstract') |
|
98 properties['content'] = form.cleaned_data.get('content') |
|
99 properties['user'] = soc.logic.user_logic.getFromFields(email=email) |
|
100 |
|
101 doc = soc.logic.document_logic.updateOrCreateFromFields(properties, |
|
102 partial_path=partial_path, link_name=link_name) |
|
103 |
|
104 return doc |
|
105 |
|
106 def edit(request, partial_path=None, link_name=None, |
80 template=DEF_SITE_DOCS_EDIT_TMPL): |
107 template=DEF_SITE_DOCS_EDIT_TMPL): |
81 """View for a Developer to modify the properties of a Document Model entity. |
108 """View for a Developer to modify the properties of a Document Model entity. |
82 |
109 |
83 Args: |
110 Args: |
84 request: the standard django request object |
111 request: the standard django request object |
107 |
134 |
108 path = path_link_name.combinePath([partial_path, link_name]) |
135 path = path_link_name.combinePath([partial_path, link_name]) |
109 |
136 |
110 # try to fetch Document entity corresponding to path if one exists |
137 # try to fetch Document entity corresponding to path if one exists |
111 try: |
138 try: |
112 doc = document.getDocumentIfPath(path) |
139 if path: |
|
140 doc = soc.logic.document_logic.getFromFields(partial_path=partial_path, |
|
141 link_name=link_name) |
113 except out_of_band.ErrorResponse, error: |
142 except out_of_band.ErrorResponse, error: |
114 # show custom 404 page when path doesn't exist in Datastore |
143 # show custom 404 page when path doesn't exist in Datastore |
115 error.message = error.message + DEF_CREATE_NEW_DOC_MSG |
144 error.message = error.message + DEF_CREATE_NEW_DOC_MSG |
116 return simple.errorResponse(request, error, template, context) |
145 return simple.errorResponse(request, error, template, context) |
117 |
146 |
118 if request.method == 'POST': |
147 if request.method == 'POST': |
119 form = EditForm(request.POST) |
148 form = EditForm(request.POST) |
120 |
149 |
121 if form.is_valid(): |
150 if form.is_valid(): |
122 new_partial_path = form.cleaned_data.get('partial_path') |
151 doc = getDocForForm(form) |
123 new_linkname = form.cleaned_data.get('link_name') |
|
124 title = form.cleaned_data.get('title') |
|
125 short_name = form.cleaned_data.get('short_name') |
|
126 abstract = form.cleaned_data.get('abstract') |
|
127 content = form.cleaned_data.get('content') |
|
128 |
|
129 doc = soc.logic.document.updateOrCreateDocument( |
|
130 partial_path=new_partial_path, link_name=new_linkname, |
|
131 title=title, short_name=short_name, abstract=abstract, |
|
132 content=content, user=id_user.getUserFromId(logged_in_id)) |
|
133 |
152 |
134 if not doc: |
153 if not doc: |
135 return http.HttpResponseRedirect('/') |
154 return http.HttpResponseRedirect('/') |
136 |
155 |
137 new_path = path_link_name.combinePath([new_partial_path, new_link_name]) |
156 new_path = path_link_name.combinePath([doc.partial_path, doc.link_name]) |
138 |
157 |
139 # redirect to new /site/docs/edit/new_path?s=0 |
158 # redirect to new /site/docs/edit/new_path?s=0 |
140 # (causes 'Profile saved' message to be displayed) |
159 # (causes 'Profile saved' message to be displayed) |
141 return helper.responses.redirectToChangedSuffix( |
160 return helper.responses.redirectToChangedSuffix( |
142 request, path, new_path, |
161 request, path, new_path, |
235 |
254 |
236 if request.method == 'POST': |
255 if request.method == 'POST': |
237 form = CreateForm(request.POST) |
256 form = CreateForm(request.POST) |
238 |
257 |
239 if form.is_valid(): |
258 if form.is_valid(): |
240 new_partial_path = form.cleaned_data.get('partial_path') |
259 doc = getDocForForm(form) |
241 new_linkname = form.cleaned_data.get('link_name') |
|
242 title = form.cleaned_data.get('title') |
|
243 short_name = form.cleaned_data.get('short_name') |
|
244 abstract = form.cleaned_data.get('abstract') |
|
245 content = form.cleaned_data.get('content') |
|
246 |
|
247 doc = soc.logic.document.updateOrCreateDocument( |
|
248 partial_path=new_partial_path, link_name=new_linkname, |
|
249 title=title, short_name=short_name, abstract=abstract, |
|
250 content=content, user=id_user.getUserFromId(logged_in_id)) |
|
251 |
260 |
252 if not doc: |
261 if not doc: |
253 return http.HttpResponseRedirect('/') |
262 return http.HttpResponseRedirect('/') |
254 |
263 |
255 new_path = path_link_name.combinePath([doc.partial_path, doc.link_name]) |
264 new_path = path_link_name.combinePath([doc.partial_path, doc.link_name]) |