4 # must fix this rcon issue somehow.
5 # this is stupid but will work for now
10 def hasChannel(channelName
):
13 return rcon
.sismember("sputnik:channels", channelName
)
15 def createChannel(channelName
):
18 if not hasChannel(channelName
):
19 rcon
.sadd("sputnik:channels", channelName
)
23 def removeChannel(channelName
):
26 return rcon
.srem("sputnik:channels", channelName
)
28 def addClientToChannel(channelName
, client
):
31 rcon
.sadd("ses:%s:channels" % client
, channelName
)
33 rcon
.sadd("sputnik:channel:%s" % channelName
, client
)
35 def removeClientFromChannel(channelName
, client
):
38 return rcon
.srem("sputnik:channel:%s" % channelName
, client
)
40 def addMessageToChannel(request
, channelName
, message
, myself
= False ):
43 clnts
= rcon
.smembers("sputnik:channel:%s" % channelName
)
45 message
["channel"] = channelName
46 message
["clientID"] = request
.clientID
49 if not myself
and c
== request
.sputnikID
:
52 rcon
.push( "ses:%s:messages" % c
, simplejson
.dumps(message
), tail
= True)
54 def removeClient(clientName
):
57 for chnl
in rcon
.smembers("ses:%s:channels" % clientName
):
58 removeClientFromChannel(chnl
, clientName
)
59 rcon
.srem("ses:%s:channels" % clientName
, chnl
)
61 rcon
.delete("ses:%s:last_access" % clientName
)
64 # also, i should delete all messages
67 ## treba viditi shto opcenito sa onim
69 def booki_main(request
, message
):
73 if message
["command"] == "ping":
74 addMessageToChannel(request
, "/booki/", {})
76 if message
["command"] == "disconnect":
79 if message
["command"] == "connect":
81 if not rcon
.exists("sputnik:client_id"):
82 rcon
.set("sputnik:client_id", 0)
84 clientID
= rcon
.incr("sputnik:client_id")
85 ret
["clientID"] = clientID
86 request
.sputnikID
= "%s:%s" % (request
.session
.session_key
, clientID
)
88 # subscribe to this channels
89 for chnl
in message
["channels"]:
90 if not hasChannel(chnl
):
93 addClientToChannel(chnl
, request
.sputnikID
)
96 rcon
.set("ses:%s:last_access" % request
.sputnikID
, time
.time())
102 def booki_chat(request
, message
, projectid
, bookid
):
103 if message
["command"] == "message_send":
104 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_received", "from": request
.user
.username
, "message": message
["message"]})
109 # remove all the copy+paste code
114 def getTOCForBook(book
):
115 from booki
.editor
import models
119 for chap
in list(models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")):
120 # is it a section or chapter
122 results
.append((chap
.chapter
.id, chap
.chapter
.title
, chap
.chapter
.url_title
, chap
.typeof
, chap
.chapter
.status
.id))
124 results
.append(('s%s' % chap
.id, chap
.name
, chap
.name
, chap
.typeof
))
132 def getHoldChapters(book_id
):
133 from django
.db
import connection
, transaction
134 cursor
= connection
.cursor()
135 # wgere chapter_id is NULL that is the hold Chapter
136 cursor
.execute("select editor_chapter.id, editor_chapter.title, editor_chapter.url_title, editor_booktoc.chapter_id, editor_chapter.status_id from editor_chapter left outer join editor_booktoc on (editor_chapter.id=editor_booktoc.chapter_id) where editor_chapter.book_id=%s;", (book_id
, ))
139 for row
in cursor
.fetchall():
141 chapters
.append((row
[0], row
[1], row
[2], 1, row
[4]))
146 def booki_book(request
, message
, projectid
, bookid
):
147 from booki
.editor
import models
150 if message
["command"] == "init_editor":
152 project
= models
.Project
.objects
.get(id=projectid
)
153 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
157 chapters
= getTOCForBook(book
)
158 holdChapters
= getHoldChapters(bookid
)
163 if a
== request
.sputnikID
:
164 return "<b>%s</b>" % a
167 users
= [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
169 ## get workflow statuses
171 statuses
= [(status
.id, status
.name
) for status
in models
.ProjectStatus
.objects
.filter(project
=project
).order_by("-weight")]
177 def _getDimension(att
):
178 if att
.attachment
.name
.endswith(".jpg"):
180 im
= Image
.open(att
.attachment
.name
)
187 attachments
= [{"id": att
.id, "dimension": _getDimension(att
), "status": att
.status
.id, "name": os
.path
.split(att
.attachment
.name
)[1], "size": att
.attachment
.size
} for att
in models
.Attachment
.objects
.filter(book
=book
)]
191 metadata
= [{'name': v
.name
, 'value': v
.getValue()} for v
in models
.Info
.objects
.filter(book
=book
)]
194 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "user_joined", "user_joined": request
.user
.username
}, myself
= False)
196 return {"chapters": chapters
, "metadata": metadata
, "hold": holdChapters
, "users": users
, "statuses": statuses
, "attachments": attachments
}
199 if message
["command"] == "chapter_status":
200 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": message
["status"], "username": request
.user
.username
})
204 if message
["command"] == "chapter_save":
205 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
206 chapter
.content
= message
["content"];
209 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": 'User %s has saved chapter "%s".' % (request
.user
.username
, chapter
.title
)}, myself
=True)
211 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
216 if message
["command"] == "chapter_rename":
217 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
218 oldTitle
= chapter
.title
219 chapter
.title
= message
["chapter"];
222 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": 'User %s has renamed chapter "%s" to "%s".' % (request
.user
.username
, oldTitle
, message
["chapter"])}, myself
=True)
224 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
226 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_rename", "chapterID": message
["chapterID"], "chapter": message
["chapter"]})
231 if message
["command"] == "chapters_changed":
232 lst
= [chap
[5:] for chap
in message
["chapters"]]
233 lstHold
= [chap
[5:] for chap
in message
["hold"]]
235 project
= models
.Project
.objects
.get(id=projectid
)
236 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
242 m
= models
.BookToc
.objects
.get(id__exact
=int(chap
[1:]))
247 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(chap
))
251 chptr
= models
.Chapter
.objects
.get(id__exact
=int(chap
))
252 m
= models
.BookToc(book
= book
,
261 if message
["kind"] == "remove":
262 if type(message
["chapter_id"]) == type(u
' ') and message
["chapter_id"][0] == 's':
263 m
= models
.BookToc
.objects
.get(id__exact
=message
["chapter_id"][1:])
266 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(message
["chapter_id"]))
269 # addMessageToChannel(request, "/chat/%s/%s/" % (projectid, bookid), {"command": "message_info", "from": request.user.username, "message": 'User %s has rearranged chapters.' % request.user.username})
271 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapters_changed", "ids": lst
, "hold_ids": lstHold
, "kind": message
["kind"], "chapter_id": message
["chapter_id"]})
275 if message
["command"] == "get_users":
278 if a
== request
.sputnikID
:
282 res
["users"] = [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
286 if message
["command"] == "get_chapter":
289 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
290 res
["title"] = chapter
.title
291 res
["content"] = chapter
.content
293 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "edit", "username": request
.user
.username
})
298 if message
["command"] == "chapter_split":
299 project
= models
.Project
.objects
.get(id=projectid
)
300 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
305 mainChapter
= models
.BookToc
.objects
.get(book
=book
, chapter__id__exact
=message
["chapterID"])
310 from django
.template
.defaultfilters
import slugify
313 allChapters
= [chap
for chap
in models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")]
314 initialPosition
= len(allChapters
)-mainChapter
.weight
315 #allChapters.remove(mainChapter)
319 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
322 for chap
in message
["chapters"]:
323 chapter
= models
.Chapter(book
= book
,
324 url_title
= slugify(chap
[0]),
328 created
= datetime
.datetime
.now(),
329 modified
= datetime
.datetime
.now())
333 m
= models
.BookToc(book
= book
,
339 allChapters
.insert(initialPosition
+n
, m
)
343 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": 'User %s has split chapter "%s".' % (request
.user
.username
, mainChapter
.chapter
.title
)}, myself
=True)
346 mainChapter
.chapter
.delete()
349 for chap
in allChapters
:
359 chapters
= getTOCForBook(book
)
360 holdChapters
= getHoldChapters(bookid
)
365 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_split", "chapterID": message
["chapterID"], "chapters": chapters
, "hold": holdChapters
, "username": request
.user
.username
}, myself
= True)
371 if message
["command"] == "create_chapter":
372 from booki
.editor
import models
375 project
= models
.Project
.objects
.get(id=projectid
)
376 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
378 from django
.template
.defaultfilters
import slugify
380 url_title
= slugify(message
["chapter"])
382 # here i should probably set it to default project status
383 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
385 chapter
= models
.Chapter(book
= book
,
386 url_title
= url_title
,
387 title
= message
["chapter"],
389 content
= '<h1>%s</h1>' % message
["chapter"],
390 created
= datetime
.datetime
.now(),
391 modified
= datetime
.datetime
.now())
394 # c = models.BookToc(book = book,
395 # name = message["chapter"],
401 result
= (chapter
.id, chapter
.title
, chapter
.url_title
, 1, s
.id)
403 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": 'User %s has created new chapter "%s".' % (request
.user
.username
, message
["chapter"])}, myself
=True)
406 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
}, myself
= True)
411 if message
["command"] == "publish_book":
412 project
= models
.Project
.objects
.get(id=projectid
)
413 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
415 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is being published.' % (book
.title
, )}, myself
=True)
418 f
= urllib2
.urlopen("http://objavi.flossmanuals.net/objavi.cgi?book=%s&project=%s&mode=epub&server=booki.flossmanuals.net&destination=archive.org" % (book
.url_title
, project
.url_name
))
424 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is published.' % (book
.title
, )}, myself
=True)
426 return {"dtaall": ta
, "dta": dta
, "dtas3": dtas3
}
429 if message
["command"] == "create_section":
430 from booki
.editor
import models
433 project
= models
.Project
.objects
.get(id=projectid
)
434 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
436 c
= models
.BookToc(book
= book
,
437 name
= message
["chapter"],
443 result
= ("s%s" % c
.id, c
.name
, None, c
.typeof
)
446 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": 'User %s has created new section "%s".' % (request
.user
.username
, message
["chapter"])}, myself
=True)
448 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
, "typeof": c
.typeof
}, myself
= True)