Ask Your Question
1

How can develop some API?

asked 2016-02-25 20:28:17 -0600

Andy gravatar image

updated 2016-03-06 19:50:35 -0600

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

edit retag flag offensive 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 gravatar imageslelievre ( 2016-02-26 10:15:44 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-03-06 19:53:52 -0600

Andy gravatar image

updated 2016-03-06 19:58:18 -0600

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')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-02-25 20:28:17 -0600

Seen: 903 times

Last updated: Mar 06 '16