예제 #1
0
def createChapter(request):
    """a method for creating a chapter"""

    #a user cannot create a chapter before he login
    if ('mid' not in request.session):
        result = __resultToJson('3', '', {})
        return HttpResponse(result, content_type = 'application/json')

    coauthor = User.objects.get(uid = request.session['mid'])

    #get the new chapter's brothers
    brotherChapters = Chapter.objects.filter(parentId = int(request.POST['parentId']))
    brotherIds = []
    for chapter in brotherChapters:
        brotherIds.append(str(chapter.cpid))

    #create a new chapter
    newChapter = Chapter()
    newChapter.desc = request.POST['desc']
    newChapter.parentId = int(request.POST['parentId'])
    newChapter.coauthor = coauthor
    newChapter.modeMask = int(request.POST['modeMask'])
    newChapter.createTime = int(time.time())
    newChapter.storyId = int(request.POST['storyId'])
    newChapter.save()
    
    #change the belonging story's timestamp
    if Story.objects.filter(stid = newChapter.storyId).exists():
        story = Story.objects.get(stid = newChapter.storyId)
    else:
        result = __resultToJson('4', '', {})
        return HttpResponse(result, content_type = 'application/json')
    story.timeStamp = int(time.time())
    story.save()
        
    #return result to client
    detail = {'newcpid': str(newChapter.cpid), 'brothers': (',').join(brotherIds)}
    result = __resultToJson('0', '', detail)
    return HttpResponse(result, content_type = 'application/json')
예제 #2
0
def createStory(request):
    """a method for an existing user to create a new story"""
    
    #a user cannot create a story before he login
    if ("mid" not in request.session):
        result = __resultToJson('3', '', {})
        return HttpResponse(result, content_type = 'application/json')

    authorId = request.session['mid']
    author = User.objects.get(uid = authorId)

    #create start chapter
    startChap = Chapter()
    startChap.desc = request.POST['startChapDesc']
    startChap.coauthor = author
    startChap.modeMask = request.POST['startChapModeMask']
    startChap.createTime = int(time.time())
    startChap.save()
    
    #create new story
    newStory = Story()
    newStory.title = request.POST['title']
    newStory.keysMask = int(request.POST['keysMask'])
    newStory.summary = request.POST['summary']
    newStory.author = author
    newStory.modeMask = int(request.POST['modeMask'])
    newStory.startChap = startChap
    newStory.createTime = int(time.time())
    newStory.timeStamp = int(time.time())
    newStory.save()
    
    #save the storyid for the startChapter
    startChap.storyId = newStory.stid
    startChap.save()

    result = __resultToJson('0', '', {'stid': str(newStory.stid)})
    return HttpResponse(result, content_type = 'application/json')