コード例 #1
0
def get(request):
    context = checkSession(request)
    user = getCurrentUser(context)
    if user is None:
        return HttpResponse()
    #first, collectRows
    user = user.findByLogin()
    rows = []
    print user.followedQuestions
    for questionId in user.followedQuestions:
        print questionId
        t = TimeLineEvent(question=questionId)
        view = t.findByQuestion()
        for row in view.rows:
            rows.append(row)
    #then, sort it
    notSorted = True
    max = len(rows)
    while notSorted:
        notSorted = False
        i = 0
        while i < (max - 1):
            if isSup(rows[i], rows[i + 1]):
                temp = rows[i]
                rows[i] = rows[i + 1]
                rows[i + 1] = temp
                notSorted = True
            i += 1
    print "sorted"
    values = []
    for row in rows:
        values.append(row.value)

    return HttpResponse(json.dumps(values))
コード例 #2
0
def get(request):
  context = checkSession(request)
  user = getCurrentUser(context)
  if user is None:
    return HttpResponse()
  #first, collectRows
  user=user.findByLogin()
  rows=[]
  print user.followedQuestions
  for questionId in user.followedQuestions:
    print questionId
    t=TimeLineEvent(question=questionId)
    view=t.findByQuestion()
    for row in view.rows :
      rows.append(row)
  #then, sort it
  notSorted=True
  max=len(rows)
  while notSorted:
    notSorted=False
    i=0
    while i < (max-1) :
      if isSup(rows[i],rows[i+1]):
        temp=rows[i]
        rows[i]=rows[i+1]
        rows[i+1]=temp
        notSorted=True
      i+=1
  print "sorted"
  values=[]
  for row in rows :
    values.append(row.value)
    

  return HttpResponse(json.dumps(values))
コード例 #3
0
def postAnswer(request):
    #obtain question by ID
    questionId = request.POST["questionId"]
    q = Question(id=questionId)
    q = q.findById()

    #create answer ID by hashing (userId, questionId)
    context = checkSession(request)
    user = getCurrentUser(context)
    if user is None:
        return HttpResponse(
            json.dumps({
                'error':
                1,
                'errorMessage':
                'You need to be logged in to post an answer'
            }))
    answerId = sha1(user.id + questionId).hexdigest()

    #check if answer ID already exists
    #this means that this user already posted an answer for this question -> abort post
    #the following is deactivated for development purposes
    '''
  for answer in q.answers:
    if answer.id == answerId:
      return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'}))
  '''
    content = request.POST["answer"]
    newAnswer = {'content': content, 'id': answerId, 'poster': user.login}
    q.answers.append(newAnswer)
    q.update()
    #time line event creation
    t = TimeLineEvent()
    t.user = user.login
    t.action = "POST"
    t.questionTitle = Question(id=questionId).findById().content
    t.answer = answerId
    t.question = questionId
    t.create()

    print t

    print 'answer added to question: ' + str(q)

    #unwrap answer dictionaries so that we can serialize into json
    answerList = []
    for answer in q.answers:
        answerList.append(answer.unwrap())

    return HttpResponse(json.dumps(answerList))
コード例 #4
0
def postAnswer(request):
  context=checkSession(request)
  user = getCurrentUser(context)
  if user is None:
    return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You need to be logged in to post an answer'}))

  #obtain question by ID
  questionId = request.POST["questionId"]
  q = Question(id=questionId)
  q = q.findById()

  #create answer ID by hashing (userId, questionId) 
  answerId = sha1(user.id + questionId).hexdigest()

  #check if answer ID already exists
  #this means that this user already posted an answer for this question -> abort post
  #the following is deactivated for development purposes
  for answer in q.answers:
    if answer.id == answerId:
      return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'}))

  content = smart_unicode(request.POST["answer"], encoding='utf-8', strings_only=False, errors='strict')
  newAnswer = {'content': content, 'id': answerId, 'poster':user.login}
  q.answers.append(newAnswer)
  q.update()
  #time line event creation
  t=TimeLineEvent()
  t.user=user.login
  t.action="POST"
  t.questionTitle=Question(id=questionId).findById().title
  t.answer=answerId
  t.question=questionId
  t.create()

  print t
  
  
  print 'answer added to question: ' + str(q)

  #unwrap answer dictionaries so that we can serialize into json
  answerList = []
  for answer in q.answers:
    answerList.append(answer.unwrap())

  response = HttpResponse(json.dumps(answerList))
  response['lastAddedId'] = newAnswer['id']
  return response
コード例 #5
0
def postAnswer(request):
    # obtain question by ID
    questionId = request.POST["questionId"]
    q = Question(id=questionId)
    q = q.findById()

    # create answer ID by hashing (userId, questionId)
    context = checkSession(request)
    user = getCurrentUser(context)
    if user is None:
        return HttpResponse(json.dumps({"error": 1, "errorMessage": "You need to be logged in to post an answer"}))
    answerId = sha1(user.id + questionId).hexdigest()

    # check if answer ID already exists
    # this means that this user already posted an answer for this question -> abort post
    # the following is deactivated for development purposes
    """
  for answer in q.answers:
    if answer.id == answerId:
      return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'}))
  """
    content = request.POST["answer"]
    newAnswer = {"content": content, "id": answerId, "poster": user.login}
    q.answers.append(newAnswer)
    q.update()
    # time line event creation
    t = TimeLineEvent()
    t.user = user.login
    t.action = "POST"
    t.questionTitle = Question(id=questionId).findById().content
    t.answer = answerId
    t.question = questionId
    t.create()

    print t

    print "answer added to question: " + str(q)

    # unwrap answer dictionaries so that we can serialize into json
    answerList = []
    for answer in q.answers:
        answerList.append(answer.unwrap())

    return HttpResponse(json.dumps(answerList))
コード例 #6
0
def get(request):
  context = checkSession(request)
  user = getCurrentUser(context)
  if user is None:
    return HttpResponse()
  #first, collectRows
  user = user.findByLogin()
  rows = []
  for questionId in user.followedQuestions:
    t = TimeLineEvent(question=questionId)
    view = t.findByQuestion()
    for row in view.rows :
      rows.append(row)
  
  #then, sort it
  notSorted = True
  max = len(rows)
  while notSorted:
    notSorted = False
    i = 0
    while i < (max - 1) :
      if isSup(rows[i], rows[i + 1]):
        temp = rows[i]
        rows[i] = rows[i + 1]
        rows[i + 1] = temp
        notSorted = True
      i += 1
  #timeline concatenation 
  #create a copy of the list
  temp = []
  for row in rows:
    temp.append(row)
  dir(temp)  
  if len(temp)==0:
    return HttpResponse()
  #the bloc class is used as a data wraper for the timeline info
  class Bloc:
    pass  
  b = Bloc()
  b.questionId = temp[0].value['question']
  results = []
  b.count = 0;
  b.questionTitle = temp[0].value['questionTitle']
  b.date = temp[0].value['eventDate']
  b.users = []
  currentBloc=b
  #concatenate it by continuous block, and count the number of occurencies
  for row in temp:
    if row.value['question'] == currentBloc.questionId:
      currentBloc.count += 1
      if currentBloc.users.__contains__(row.value['user']) == False:
        currentBloc.users.append(row.value['user'])
    else :
      results.append(currentBloc)
      currentBloc = Bloc()
      currentBloc.count = 1
      currentBloc.questionTitle = row.value['questionTitle']
      currentBloc.date = row.value['eventDate']
      currentBloc.questionId = row.value['question']
      currentBloc.users=[]
      currentBloc.users.append(row.value['user'])
  results.append(currentBloc)    
  #generating the final timeline

  timeline = []             
  for item in results:
    timeline.append({'questionTitle':item.questionTitle,
                      'date':item.date, 
                      'questionId':item.questionId,
                      'answerCount':item.count,
                      'users':item.users})
    
  print json.dumps(timeline)
  #limit the size of the time line to 20   
  values = []
  i = 0
  for item in timeline:
    values.append(item)
    if i > TIMELINE_SIZE :
      break
    i += 1;
  return HttpResponse(json.dumps(values))
コード例 #7
0
def get(request):
    context = checkSession(request)
    user = getCurrentUser(context)
    if user is None:
        return HttpResponse()
    #first, collectRows
    user = user.findByLogin()
    rows = []
    for questionId in user.followedQuestions:
        t = TimeLineEvent(question=questionId)
        view = t.findByQuestion()
        for row in view.rows:
            rows.append(row)

    #then, sort it
    notSorted = True
    max = len(rows)
    while notSorted:
        notSorted = False
        i = 0
        while i < (max - 1):
            if isSup(rows[i], rows[i + 1]):
                temp = rows[i]
                rows[i] = rows[i + 1]
                rows[i + 1] = temp
                notSorted = True
            i += 1
    #timeline concatenation
    #create a copy of the list
    temp = []
    for row in rows:
        temp.append(row)
    dir(temp)
    if len(temp) == 0:
        return HttpResponse()
    #the bloc class is used as a data wraper for the timeline info
    class Bloc:
        pass

    b = Bloc()
    b.questionId = temp[0].value['question']
    results = []
    b.count = 0
    b.questionTitle = temp[0].value['questionTitle']
    b.date = temp[0].value['eventDate']
    b.users = []
    currentBloc = b
    #concatenate it by continuous block, and count the number of occurencies
    for row in temp:
        if row.value['question'] == currentBloc.questionId:
            currentBloc.count += 1
            if currentBloc.users.__contains__(row.value['user']) == False:
                currentBloc.users.append(row.value['user'])
        else:
            results.append(currentBloc)
            currentBloc = Bloc()
            currentBloc.count = 1
            currentBloc.questionTitle = row.value['questionTitle']
            currentBloc.date = row.value['eventDate']
            currentBloc.questionId = row.value['question']
            currentBloc.users = []
            currentBloc.users.append(row.value['user'])
    results.append(currentBloc)
    #generating the final timeline

    timeline = []
    for item in results:
        timeline.append({
            'questionTitle': item.questionTitle,
            'date': item.date,
            'questionId': item.questionId,
            'answerCount': item.count,
            'users': item.users
        })

    print json.dumps(timeline)
    #limit the size of the time line to 20
    values = []
    i = 0
    for item in timeline:
        values.append(item)
        if i > TIMELINE_SIZE:
            break
        i += 1
    return HttpResponse(json.dumps(values))