First time here? Check out the FAQ!
1

How can develop some API?

In my forum,I want to get question based on keyword from API.For example,

[my site]/api/getquestions/?query=$keyword

it can return all the question I want.In the other side, I want to get a user's inboxs.

[my site]/api/getInbox/userid

@Evgeny

Andy's avatar
120
Andy
asked 2016-02-25 20:28:17 -0500, updated 2016-03-06 19:50:35 -0500
edit flag offensive 0 remove flag close merge delete

Comments

1

In English, please add a space after each comma, like this, and after each period ending a sentence. Like this. (In what you wrote,they are missing.Like this.)

slelievre's avatar slelievre (2016-02-26 10:15:44 -0500) edit
add a comment see more comments

1 Answer

0

I have solved my question myself.

here is the key code below.

def inbox(request, account_id):

'''
Gets a single question
'''
#print repr(account_id)
user = get_object_or_404(User, last_name=account_id)
data = get_user_data(user)
#print repr(data)
activity_types = const.RESPONSE_ACTIVITY_TYPES_FOR_DISPLAY
activity_types += (const.TYPE_ACTIVITY_MENTION,)
memo_set = user.get_notifications(activity_types)

memo_set = memo_set.select_related(
                'activity',
                'activity__content_type',
                'activity__question__thread',
                'activity__user',
                'activity__user__askbot_profile__gravatar',
            ).order_by(
                '-activity__active_at'
            )[:const.USER_VIEW_DATA_SIZE]

response_list = list()
for memo in memo_set:
    obj = memo.activity.content_object
    if obj is None:
        memo.activity.delete()
        continue#a temp plug due to bug in the comment deletion

    act = memo.activity
    act_user = act.user
   act_message = act.get_activity_type_display()
    act_type = 'edit'

    response = {
        'id': memo.id,
        'timestamp': str(act.active_at),
        'user': str(act_user),
        'is_new': memo.is_new(),
        'url': act.get_absolute_url(),
        'snippet': act.get_snippet(),
        'title': act.question.thread.title,
        'message_type': act_message,
        'memo_type': act_type,
        'question_id': act.question.id,
        'followup_messages': list(),
        'content': obj.html or obj.text,
    }

    if memo.is_new():
        response_list.append(response)

json_string = simplejson.dumps(response_list)
return HttpResponse(json_string, content_type='application/json')
Andy's avatar
120
Andy
answered 2016-03-06 19:53:52 -0500, updated 2016-03-06 19:58:18 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments