Changeset 023aab1063008d5dbb513ec396d5249513f565c9


Ignore:
Timestamp:
31.07.2013 13:47:56 (8 years ago)
Author:
Stanislaw Klekot <dozzie@…>
Branches:
b66903eafbcb1d49112014abc82c8bf683413db0
Children:
b844b09e1f6574cc82d51136eb74fc2fcde6ee11
Parents:
0bc598ad2cf7c9092fcf6bf51da79d3d5a6f0606
git-author:
Stanislaw Klekot <dozzie@…> (31.07.2013 13:47:56)
git-committer:
Stanislaw Klekot <dozzie@…> (31.07.2013 13:47:56)
Message:

Got rid of fugly save_wiki() view.

All that's necessary is done in edit_wiki(). Better yet, thanks to Django
messaging system, redirect-after-save (similar to one in Trac) was possible.
Now refreshing a page after saving doesn't display completely unnecessary
message to user.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • dashwiki/wiki/views.py

    r0bc598 r023aab  
    144144# wiki pages {{{ 
    145145#----------------------------------------------------------------------------- 
    146 # save_wiki() {{{ 
     146# edit_wiki() {{{ 
    147147 
    148148@login_required 
    149 def save_wiki(request, page_name): 
    150   template = loader.get_template('display_wiki.html') 
     149def edit_wiki(request, page_name): 
     150  template = loader.get_template('edit_wiki.html') 
    151151 
    152152  from forms import WikiPageEdit 
    153153 
    154154  page = load_wiki_or_null(page_name) 
    155   has_changed = False 
    156   if page: 
    157     page_form = WikiPageEdit(request.POST, instance = page) 
    158     if page.content != page_form.data['content']: 
    159       has_changed = True 
     155  save_no_change = False 
     156  if page != None: 
     157    page_instance = { 'instance': page } 
    160158  else: 
    161     page_form = WikiPageEdit(request.POST, initial = { 'name': page_name }) 
    162     page = page_form.instance 
    163     has_changed = True 
    164  
    165   context = RequestContext(request, { 
    166     'name': page_name, 
    167     'real_name': page_name, 
    168     'form': page_form, 
    169     'body_raw': page.content, 
    170     'body_tree': None, 
    171     'editable': request.user.is_authenticated(), # always True in this case 
    172   }) 
    173  
    174   if not has_changed: 
    175     # XXX: is this actually a good idea? maybe some placeholder instead of 
    176     # actual macro value? 
    177     context['body_tree'] = call_all_macros(page.get_ast(), request, page_name) 
     159    page_instance = { 'initial': { 'name': page_name } } 
     160 
     161  if 'save' in request.POST: 
     162    page_form = WikiPageEdit(request.POST, **page_instance) 
     163  else: 
     164    page_form = WikiPageEdit(**page_instance) 
     165 
     166  # page unchanged 
     167  if page != None and page.content == page_form.data.get('content'): 
    178168    messages.info(request, 'Wiki page unchanged') 
    179   else: 
     169    response = HttpResponse(status = 302) 
     170    response['location'] = request.path 
     171    return response 
     172 
     173  if page_form.is_valid(): # try saving it {{{ 
    180174    from dashwiki.markup import ParseError 
    181175    try: 
     
    183177      new_rev = page.make_revision(request.user) 
    184178      new_rev.save() 
    185       logger.info(log('wiki page saved', page = page_form.instance.name, 
    186                       revision = new_rev.revision, 
    187                       author = request.user.username)) 
    188       # XXX: is this actually a good idea? maybe some placeholder instead of 
    189       # actual macro value? 
    190       context['body_tree'] = call_all_macros(page.get_ast(), request, page_name) 
     179      logger.info(log( 
     180        'wiki page saved', 
     181        page = page_form.instance.name, 
     182        revision = new_rev.revision, 
     183        author = request.user.username 
     184      )) 
    191185      messages.success(request, 'Wiki page saved') 
     186 
     187      response = HttpResponse(status = 302) 
     188      response['location'] = request.path 
     189      return response 
    192190    except ParseError, e: 
    193       def _parse_error_format(err): 
    194         return ("Syntax error at '%s' (%s), line %d, char %d.\n" + \ 
    195                 "Expected one of these tokens: %s") % \ 
    196                (err['text'], err['token'], err['line'], err['char'], 
    197                 ", ".join([str(e) for e in err['expected']])) 
    198  
    199       # go back to editing 
    200       template = loader.get_template('edit_wiki.html') 
    201191      for err in e.errors: 
    202         messages.error(request, _parse_error_format(err)) 
    203  
    204   return HttpResponse(template.render(context)) 
    205  
    206 # }}} 
    207 #----------------------------------------------------------------------------- 
    208 # edit_wiki() {{{ 
    209  
    210 @on_button_call(button = 'save', target = save_wiki) # FIXME: temporary handler 
    211 @login_required 
    212 def edit_wiki(request, page_name): 
    213   template = loader.get_template('edit_wiki.html') 
    214  
    215   from forms import WikiPageEdit 
    216  
    217   page = load_wiki_or_null(page_name) 
    218   if page: 
    219     page_form = WikiPageEdit(instance = page) 
    220   else: 
    221     page_form = WikiPageEdit(initial = { 'name': page_name }) 
     192        messages.error(request, 
     193          "Syntax error at '%s' (%s), line %d, char %d.\n" 
     194          "Expected one of these tokens: %s" % ( 
     195            err['text'], err['token'], err['line'], err['char'], 
     196            ", ".join([str(e) for e in err['expected']]) 
     197          ) 
     198        ) 
     199  # }}}} 
    222200 
    223201  context = RequestContext(request, { 
Note: See TracChangeset for help on using the changeset viewer.