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
):
75 if message
["command"] == "ping":
76 addMessageToChannel(request
, "/booki/", {})
78 if message
["command"] == "disconnect":
81 if message
["command"] == "connect":
82 # this is where we have problems
83 if not rcon
.exists("sputnik:client_id"):
84 rcon
.set("sputnik:client_id", 0)
86 clientID
= rcon
.incr("sputnik:client_id")
87 ret
["clientID"] = clientID
88 request
.sputnikID
= "%s:%s" % (request
.session
.session_key
, clientID
)
90 # subscribe to this channels
91 for chnl
in message
["channels"]:
92 if not hasChannel(chnl
):
95 addClientToChannel(chnl
, request
.sputnikID
)
98 rcon
.set("ses:%s:last_access" % request
.sputnikID
, time
.time())
104 def booki_chat(request
, message
, projectid
, bookid
):
105 if message
["command"] == "message_send":
106 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_received", "from": request
.user
.username
, "message": message
["message"]})
111 # remove all the copy+paste code
116 def getTOCForBook(book
):
117 from booki
.editor
import models
121 for chap
in list(models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")):
122 # is it a section or chapter
124 results
.append((chap
.chapter
.id, chap
.chapter
.title
, chap
.chapter
.url_title
, chap
.typeof
, chap
.chapter
.status
.id))
126 results
.append(('s%s' % chap
.id, chap
.name
, chap
.name
, chap
.typeof
))
134 def getHoldChapters(book_id
):
135 from django
.db
import connection
, transaction
136 cursor
= connection
.cursor()
137 # wgere chapter_id is NULL that is the hold Chapter
138 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
, ))
141 for row
in cursor
.fetchall():
143 chapters
.append((row
[0], row
[1], row
[2], 1, row
[4]))
148 def getAttachments(book
):
149 from booki
.editor
import models
153 def _getDimension(att
):
154 if att
.attachment
.name
.endswith(".jpg"):
156 im
= Image
.open(att
.attachment
.name
)
163 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
)]
168 def booki_book(request
, message
, projectid
, bookid
):
169 from booki
.editor
import models
172 if message
["command"] == "init_editor":
174 project
= models
.Project
.objects
.get(id=projectid
)
175 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
179 chapters
= getTOCForBook(book
)
180 holdChapters
= getHoldChapters(bookid
)
185 if a
== request
.sputnikID
:
186 return "<b>%s</b>" % a
189 users
= [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
191 ## get workflow statuses
193 statuses
= [(status
.id, status
.name
) for status
in models
.ProjectStatus
.objects
.filter(project
=project
).order_by("-weight")]
196 attachments
= getAttachments(book
)
200 metadata
= [{'name': v
.name
, 'value': v
.getValue()} for v
in models
.Info
.objects
.filter(book
=book
)]
203 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "user_joined", "user_joined": request
.user
.username
}, myself
= False)
207 licenses
= [(elem
.abbrevation
, elem
.name
) for elem
in models
.License
.objects
.all().order_by("name")]
209 return {"licenses": licenses
, "chapters": chapters
, "metadata": metadata
, "hold": holdChapters
, "users": users
, "statuses": statuses
, "attachments": attachments
}
212 if message
["command"] == "attachments_list":
213 project
= models
.Project
.objects
.get(id=projectid
)
214 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
216 attachments
= getAttachments(book
)
218 return {"attachments": attachments
}
221 if message
["command"] == "chapter_status":
222 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": message
["status"], "username": request
.user
.username
})
226 if message
["command"] == "chapter_save":
227 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
228 chapter
.content
= message
["content"];
231 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)
233 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
238 if message
["command"] == "chapter_rename":
239 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
240 oldTitle
= chapter
.title
241 chapter
.title
= message
["chapter"];
244 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)
246 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "normal", "username": request
.user
.username
})
248 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_rename", "chapterID": message
["chapterID"], "chapter": message
["chapter"]})
253 if message
["command"] == "chapters_changed":
254 lst
= [chap
[5:] for chap
in message
["chapters"]]
255 lstHold
= [chap
[5:] for chap
in message
["hold"]]
257 project
= models
.Project
.objects
.get(id=projectid
)
258 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
264 m
= models
.BookToc
.objects
.get(id__exact
=int(chap
[1:]))
269 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(chap
))
273 chptr
= models
.Chapter
.objects
.get(id__exact
=int(chap
))
274 m
= models
.BookToc(book
= book
,
283 if message
["kind"] == "remove":
284 if type(message
["chapter_id"]) == type(u
' ') and message
["chapter_id"][0] == 's':
285 m
= models
.BookToc
.objects
.get(id__exact
=message
["chapter_id"][1:])
288 m
= models
.BookToc
.objects
.get(chapter__id__exact
=int(message
["chapter_id"]))
291 # addMessageToChannel(request, "/chat/%s/%s/" % (projectid, bookid), {"command": "message_info", "from": request.user.username, "message": 'User %s has rearranged chapters.' % request.user.username})
293 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapters_changed", "ids": lst
, "hold_ids": lstHold
, "kind": message
["kind"], "chapter_id": message
["chapter_id"]})
297 if message
["command"] == "get_users":
300 if a
== request
.sputnikID
:
304 res
["users"] = [vidi(m
) for m
in list(rcon
.smembers("sputnik:channel:%s" % message
["channel"]))]
308 if message
["command"] == "get_chapter":
311 chapter
= models
.Chapter
.objects
.get(id=int(message
["chapterID"]))
312 res
["title"] = chapter
.title
313 res
["content"] = chapter
.content
315 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_status", "chapterID": message
["chapterID"], "status": "edit", "username": request
.user
.username
})
320 if message
["command"] == "chapter_split":
321 project
= models
.Project
.objects
.get(id=projectid
)
322 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
327 mainChapter
= models
.BookToc
.objects
.get(book
=book
, chapter__id__exact
=message
["chapterID"])
332 from django
.template
.defaultfilters
import slugify
335 allChapters
= [chap
for chap
in models
.BookToc
.objects
.filter(book
=book
).order_by("-weight")]
336 initialPosition
= len(allChapters
)-mainChapter
.weight
337 #allChapters.remove(mainChapter)
341 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
344 for chap
in message
["chapters"]:
345 chapter
= models
.Chapter(book
= book
,
346 url_title
= slugify(chap
[0]),
350 created
= datetime
.datetime
.now(),
351 modified
= datetime
.datetime
.now())
355 m
= models
.BookToc(book
= book
,
361 allChapters
.insert(initialPosition
+n
, m
)
365 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)
368 mainChapter
.chapter
.delete()
371 for chap
in allChapters
:
381 chapters
= getTOCForBook(book
)
382 holdChapters
= getHoldChapters(bookid
)
387 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_split", "chapterID": message
["chapterID"], "chapters": chapters
, "hold": holdChapters
, "username": request
.user
.username
}, myself
= True)
393 if message
["command"] == "create_chapter":
394 from booki
.editor
import models
397 project
= models
.Project
.objects
.get(id=projectid
)
398 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
400 from django
.template
.defaultfilters
import slugify
402 url_title
= slugify(message
["chapter"])
404 # here i should probably set it to default project status
405 s
= models
.ProjectStatus
.objects
.filter(project
=project
).order_by("weight")[0]
407 chapter
= models
.Chapter(book
= book
,
408 url_title
= url_title
,
409 title
= message
["chapter"],
411 content
= '<h1>%s</h1>' % message
["chapter"],
412 created
= datetime
.datetime
.now(),
413 modified
= datetime
.datetime
.now())
416 # c = models.BookToc(book = book,
417 # name = message["chapter"],
423 result
= (chapter
.id, chapter
.title
, chapter
.url_title
, 1, s
.id)
425 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)
428 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
}, myself
= True)
433 if message
["command"] == "publish_book":
434 project
= models
.Project
.objects
.get(id=projectid
)
435 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
437 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is being published.' % (book
.title
, )}, myself
=True)
440 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
))
446 addMessageToChannel(request
, "/chat/%s/%s/" % (projectid
, bookid
), {"command": "message_info", "from": request
.user
.username
, "message": '"%s" is published.' % (book
.title
, )}, myself
=True)
448 return {"dtaall": ta
, "dta": dta
, "dtas3": dtas3
}
451 if message
["command"] == "create_section":
452 from booki
.editor
import models
455 project
= models
.Project
.objects
.get(id=projectid
)
456 book
= models
.Book
.objects
.get(project
=project
, id=bookid
)
458 c
= models
.BookToc(book
= book
,
459 name
= message
["chapter"],
465 result
= ("s%s" % c
.id, c
.name
, None, c
.typeof
)
468 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)
470 addMessageToChannel(request
, "/booki/book/%s/%s/" % (projectid
, bookid
), {"command": "chapter_create", "chapter": result
, "typeof": c
.typeof
}, myself
= True)