Attachment 'state_action.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - State action
4
5 Change an given state/icon on the current page
6 Need State.py
7
8 @copyright: 2007 Flieger Richard (info@rf-mmx.de)
9 @license: GNU GPL, see COPYING for details.
10 """
11
12 from MoinMoin.Page import Page, config
13 from MoinMoin import PageEditor, formatter
14 import os
15 import re
16
17 # the statelist has to be the same as the statelist of the macro
18 statelist = {"todo" : "/!\\",
19 "done" : "(./)",
20 "aborted" : "{X}",
21 "inprogress" : "(!)",
22 "fun" : ":)" }
23
24 Defaultstate = "TODO"
25
26 def keyToValue( dict1 ):
27 dict2 = {}
28 for i in statelist:
29 dict2[dict1[i]] = i
30 return dict2
31
32 smileylist = keyToValue( statelist )
33
34 class StateAction:
35
36 def __init__( self, request, referrer ):
37 self.request = request
38 self.referrer = referrer
39 self.fmt = formatter.text_html.Formatter( self.request )
40 self.pagename = self.referrer
41 self.new = self.request.form.get( 'new', [None] )[0]
42 self.MacroID = self.request.form.get( 'id', [None] )[0]
43 self.old = self.request.form.get( 'old', [None] )[0]
44 self.rev = int( self.request.form.get( 'rev', "" )[0])
45 self.error = []
46
47 def render( self ):
48 _ = self.request.getText
49 comment = _('''State was changed.''')
50 statewaschanged = _('''State was changed''')
51 statewasnotchanged = _('''State was not changed''')
52 pagewaschanged = _('''Someone else changed this page while you were editing!''')
53 page = Page( self.request, self.pagename )
54 currentrev = page.get_real_rev()
55 writable = page.isWritable()
56 permission = self.request.user.may.write( self.pagename )
57 if self.rev == currentrev and writable and permission:
58 if not self.old:
59 self.old = "todo"
60 if self.new:
61 try:
62 oldtext = PageEditor.PageEditor( self.request, self.pagename, do_editor_backup=0 )
63 oldtext = oldtext.get_raw_body()
64 newtext = oldtext
65 newtext = replaceByID( newtext, self.new, int(self.MacroID))
66 page = PageEditor.PageEditor( self.request, self.pagename, do_editor_backup=0 )
67 self.request.reset()
68 if not newtext == oldtext:
69 page.saveText( newtext, currentrev, comment=comment, trivial=1 )
70 query = { 'action': 'show' }
71 url = Page( self.request, self.pagename ).url( self.request, query, 0 )
72 self.request.http_redirect( url )
73 msg = statewaschanged
74 else:
75 msg = statewasnotchanged
76 except self.error:
77 page = Page( self.request, self.pagename )
78 msg = statewasnotchanged
79 else:
80 msg = self.generateQueryMessage( )
81 else:
82 page = Page( self.request, self.pagename )
83 msg = pagewaschanged
84
85 page = Page( self.request, self.pagename )
86 page.send_page( self.request, msg=msg )
87
88 return ''
89
90 def get_reversion(self):
91 p = Page( self.request, self.pagename )
92 rev = p.get_real_rev()
93 return str(rev)
94
95 def findallResults( self, text, pattern ):
96 resultList = re.findall(pattern, text)
97 return resultList
98
99 def generateQueryMessage( self ):
100 html = u""
101
102 smileys = config.smileys.items()
103 smileys.sort()
104 sm =[]
105
106 for off in range( len( smileys ) ):
107 markup, smiley = smileys[off]
108 smileyname = parsText( "(.*?)\....", smiley[3] )
109 if smileyname not in sm:
110 if markup in keyToValue( statelist ):
111 sm.append( smileyname )
112 iconimg = self.fmt.smiley( markup )
113 html += getHTML(self.old, markup, iconimg, self.MacroID, self.rev)
114 return html
115
116 def getID(self):
117 try:
118 id = self.request.stateID
119 except self.error:
120 id = 0
121 return id
122
123 def getHTML( old, new, iconimg, id, rev ):
124 html = u"""<a href="?action=state&old=%(old)s&new=%(new)s&id=%(id)s&rev=%(rev)s">%(iconimg)s</a>""" % { "new" : smileylist.get(new, Defaultstate).upper() , "old" : old, "id" : id, "rev" : rev, "iconimg" : iconimg }
125 return html
126
127 def replaceByID( text, new, id ):
128 newtext = text
129 new = "[[State(%s)]]" % new
130 pattern = re.compile( "\[\[State\(?\s*.*?\s*\)?\]\]" )
131 id -= 1
132 resultlist = pattern.findall( text )
133 if resultlist:
134 index = 0
135 newtext = text
136 pos1 = 0
137 pos2 = 0
138 pos1 = newtext[pos1:].find(resultlist[0])
139 for result in resultlist:
140 pos1 = pos2 + newtext[pos2:].find(result)
141 pos2 = pos1 + len(result)
142 if index == id:
143 newtext = newtext[:pos1] + new + newtext[pos2:]
144 break
145 index += 1
146 return newtext
147
148 def getResults(self, text ):
149 pattern = re.compile( self.findregex)
150 resultList = pattern.findall( text)
151 pos = 0
152 newresultList = []
153 for result in resultList:
154 pos = text.find(result,pos)
155 if pos > self.contextsize:
156 pos1 = pos - self.contextsize
157 else:
158 pos1 = 0
159 pos2 = pos + len(result) + self.contextsize
160 newresultList.append( (text[pos1:pos], result,text[pos+len(result):pos2]) )
161 pos += len(result) +1
162 return newresultList
163
164 def parsText( regex, source ):
165 source = source.encode( "iso-8859-1" )
166 import re
167 variable = re.search( regex, source )
168 if variable != None:
169 variable = variable.group( 1 )
170 value = variable.decode( "iso-8859-1" )
171 else:
172 return 0
173 return value
174
175 def execute( pagename, request ):
176 return StateAction( request, pagename ).render()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.