4 # must fix this rcon issue somehow.
5 # this is stupid but will work for now
9 def hasChannel(channelName
):
12 return rcon
.sismember("sputnik:channels", channelName
)
14 def createChannel(channelName
):
17 if not hasChannel(channelName
):
18 rcon
.sadd("sputnik:channels", channelName
)
22 def removeChannel(channelName
):
25 return rcon
.srem("sputnik:channels", channelName
)
27 def addClientToChannel(channelName
, client
):
30 rcon
.sadd("ses:%s:channels" % client
, channelName
)
32 rcon
.sadd("sputnik:channel:%s" % channelName
, client
)
34 def removeClientFromChannel(channelName
, client
):
37 return rcon
.srem("sputnik:channel:%s" % channelName
, client
)
39 def addMessageToChannel(request
, channelName
, message
, myself
= False ):
42 clnts
= rcon
.smembers("sputnik:channel:%s" % channelName
)
44 message
["channel"] = channelName
45 message
["clientID"] = request
.clientID
48 if not myself
and c
== request
.sputnikID
:
51 rcon
.push( "ses:%s:messages" % c
, simplejson
.dumps(message
), tail
= True)
53 def removeClient(clientName
):
56 for chnl
in rcon
.smembers("ses:%s:channels" % clientName
):
57 removeClientFromChannel(chnl
, clientName
)
58 rcon
.srem("ses:%s:channels" % clientName
, chnl
)
60 rcon
.delete("ses:%s:last_access" % clientName
)
63 # also, i should delete all messages
66 ## treba viditi shto opcenito sa onim
68 def booki_main(request
, message
):
72 if message
["command"] == "ping":
73 addMessageToChannel(request
, "/booki/", {})
75 if message
["command"] == "disconnect":
78 if message
["command"] == "connect":
80 if not rcon
.exists("sputnik:client_id"):
81 rcon
.set("sputnik:client_id", 0)
83 clientID
= rcon
.incr("sputnik:client_id")
84 ret
["clientID"] = clientID
85 request
.sputnikID
= "%s:%s" % (request
.session
.session_key
, clientID
)
87 # subscribe to this channels
88 for chnl
in message
["channels"]:
89 if not hasChannel(chnl
):
92 addClientToChannel(chnl
, request
.sputnikID
)
95 rcon
.set("ses:%s:last_access" % request
.sputnikID
, time
.time())
101 def booki_chat(request
, message
, projectid
, bookid
):
102 if message
["command"] == "message_send":
103 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_received", "from": request
.user
.username
, "message": message
["message"]})
108 # remove all the copy+paste code
113 def getTOCForBook(book
):
114 from booki
.editor
import models
118 for chap
in list(models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")):
119 # is it a section or chapter
121 results
.append((chap
.chapter
.id, chap
.chapter
.title
, chap
.chapter
.url_title
, chap
.typeof
, chap
.chapter
.status
.id))
123 results
.append(('s%s' % chap
.id, chap
.name
, chap
.name
, chap
.typeof
))
131 def getHoldChapters(book_id
):
132 from django
.db
import connection
, transaction
133 cursor
= connection
.cursor()
134 # wgere chapter_id is NULL that is the hold Chapter
135 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
, ))
138 for row
in cursor
.fetchall():
140 chapters
.append((row
[0], row
[1], row
[2], 1, row
[4]))
145 def booki_book(request
, message
, projectid
, bookid
):
146 from booki
.editor
import models
149 if message
["command"] == "init_editor":
151 project
= models
.Project
.objects
.get(id=projectid
)
152 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
156 chapters
= getTOCForBook(book
)
157 holdChapters
= getHoldChapters(bookid
)
162 if a
== request
.sputnikID
:
163 return "<b>%s</b>" % a
166 users
= [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
168 ## get workflow statuses
170 statuses
= [(status
.id, status
.name
) for status
in models
.ProjectStatus
.objects
.filter(project
=project
).order_by("-weight")]
176 def _getDimension(att
):
177 if att
.attachment
.name
.endswith(".jpg"):
179 im
= Image
.open(att
.attachment
.name
)
186 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
)]
190 metadata
= [{'name': v
.name
, 'value': v
.getValue()} for v
in models
.Info
.objects
.filter(book
=book
)]
193 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "user_joined", "user_joined": request
.user
.username
}, myself
= False)
195 return {"chapters": chapters
, "metadata": metadata
, "hold": holdChapters
, "users": users
, "statuses": statuses
, "attachments": attachments
}
198 if message
["command"] == "chapter_status":
199 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": message
["status"], "username": request
.user
.username
})
203 if message
["command"] == "chapter_save":
204 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
205 chapter
.content
= message
["content"];
208 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)
210 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
215 if message
["command"] == "chapter_rename":
216 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
217 oldTitle
= chapter
.title
218 chapter
.title
= message
["chapter"];
221 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)
223 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
225 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_rename", "chapterID": message
["chapterID"], "chapter": message
["chapter"]})
230 if message
["command"] == "chapters_changed":
231 lst
= [chap
[5:] for chap
in message
["chapters"]]
232 lstHold
= [chap
[5:] for chap
in message
["hold"]]
234 project
= models
.Project
.objects
.get(id=projectid
)
235 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
241 m
= models
.BookToc
.objects
.get(id__exact
=int(chap
[1:]))
246 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(chap
))
250 chptr
= models
.Chapter
.objects
.get(id__exact
=int(chap
))
251 m
= models
.BookToc(book
= book
,
260 if message
["kind"] == "remove":
261 if type(message
["chapter_id"]) == type(u
' ') and message
["chapter_id"][0] == 's':
262 m
= models
.BookToc
.objects
.get(id__exact
=message
["chapter_id"][1:])
265 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(message
["chapter_id"]))
268 # addMessageToChannel(request, "/chat/%s/%s/" % (projectid, bookid), {"command": "message_info", "from": request.user.username, "message": 'User %s has rearranged chapters.' % request.user.username})
270 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapters_changed", "ids": lst
, "hold_ids": lstHold
, "kind": message
["kind"], "chapter_id": message
["chapter_id"]})
274 if message
["command"] == "get_users":
277 if a
== request
.sputnikID
:
281 res
["users"] = [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
285 if message
["command"] == "get_chapter":
288 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
289 res
["title"] = chapter
.title
290 res
["content"] = chapter
.content
292 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "edit", "username": request
.user
.username
})
297 if message
["command"] == "chapter_split":
298 project
= models
.Project
.objects
.get(id=projectid
)
299 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
304 mainChapter
= models
.BookToc
.objects
.get(book
=book
, chapter__id__exact
=message
["chapterID"])
309 from django
.template
.defaultfilters
import slugify
312 allChapters
= [chap
for chap
in models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")]
313 initialPosition
= len(allChapters
)-mainChapter
.weight
314 #allChapters.remove(mainChapter)
318 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
321 for chap
in message
["chapters"]:
322 chapter
= models
.Chapter(book
= book
,
323 url_title
= slugify(chap
[0]),
327 created
= datetime
.datetime
.now(),
328 modified
= datetime
.datetime
.now())
332 m
= models
.BookToc(book
= book
,
338 allChapters
.insert(initialPosition
+n
, m
)
342 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)
345 mainChapter
.chapter
.delete()
348 for chap
in allChapters
:
358 chapters
= getTOCForBook(book
)
359 holdChapters
= getHoldChapters(bookid
)
364 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_split", "chapterID": message
["chapterID"], "chapters": chapters
, "hold": holdChapters
, "username": request
.user
.username
}, myself
= True)
370 if message
["command"] == "create_chapter":
371 from booki
.editor
import models
374 project
= models
.Project
.objects
.get(id=projectid
)
375 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
377 from django
.template
.defaultfilters
import slugify
379 url_title
= slugify(message
["chapter"])
381 # here i should probably set it to default project status
382 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
384 chapter
= models
.Chapter(book
= book
,
385 url_title
= url_title
,
386 title
= message
["chapter"],
388 created
= datetime
.datetime
.now(),
389 modified
= datetime
.datetime
.now())
392 c
= models
.BookToc(book
= book
,
393 name
= message
["chapter"],
399 result
= (chapter
.id, chapter
.title
, chapter
.url_title
, 1, s
.id)
401 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)
404 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
}, myself
= True)
409 if message
["command"] == "publish_book":
410 project
= models
.Project
.objects
.get(id=projectid
)
411 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
413 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is being published.' % (book
.title
, )}, myself
=True)
416 #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))
419 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is published.' % (book
.title
, )}, myself
=True)
424 if message
["command"] == "create_section":
425 from booki
.editor
import models
428 project
= models
.Project
.objects
.get(id=projectid
)
429 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
431 c
= models
.BookToc(book
= book
,
432 name
= message
["chapter"],
438 result
= ("s%s" % c
.id, c
.name
, None, c
.typeof
)
441 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)
443 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
, "typeof": c
.typeof
}, myself
= True)