def api_note_add(request): """ summary: Api interface post message params: GET['uname'] CiCaDa user's username GET['pwd'] user's password not encoding GET['msg'] want to post message GET['from'] your web site name author: kimly x """ # Get querystring params _username = request.GET["uname"] _password = functions.md5_encode(request.GET["pwd"]) _message = request.GET["msg"] _from = request.GET["from"] # Get user info and check user try: _user = User.objects.get(username=_username, password=_password) except: return HttpResponse("-2") # Get category info ,If it not exist create new (_cate, _is_added_cate) = Category.objects.get_or_create(name=_from) try: _note = Note(message=_message, user=_user, category=_cate) _note.save() return HttpResponse("1") except: return HttpResponse("-1")
def index_user_page(request, _username, _page_index): # Get user login state _islogin = __is_login(request) _page_title = u"首页" try: # Get post params _message = request.POST["message"] _is_post = True except KeyError: _is_post = False # save message if _is_post: # check login if not _islogin: return HttpResponseRedirect("/signin/") # save message _category = Category.objects.get_or_create(name=u"网页") try: _user = User.objects.get(id=__user_id(request)) except: return HttpResponseRedirect("/signin/") _note = Note(message=_message, category=_category, user=_user) _note.save(self, *args, **kwargs) return HttpResponseRedirect("/user/" + _user.username) _userid = -1 # Get message list _offset_index = (int(_page_index) - 1) * PAGE_SIZE _last_item_index = PAGE_SIZE * int(_page_index) _login_user_friend_list = None if _islogin: # Get friend message if user is logined _login_user = User.objects.get(username=__user_name(request)) _login_user_friend_list = _login_user.friend.all() else: _login_user = None _friends = None _self_home = False if _username != "": # there is get user's message _user = get_object_or_404(User, username=_username) _userid = _user.id _notes = Note.objects.filter(user=_user).order_by("-addtime") _page_title = u"%s" % _user.realname # Get friend lists _friends = _user.friend.get_query_set().order_by("id")[0:FRIEND_LIST_MAX] if _userid == __user_id(request): _self_home = True else: # Get all message _user = None if _islogin: _query_users = [_login_user] _query_users.extend(_login_user.friend.all()) _notes = Note.objects.filter(user__in=_query_users).order_by("-addtime") else: # Get all user message _notes = Note.objects.order_by("-addtime") # Page bar _page_bar = formatter.pagebar(_notes, _page_index, _username) # Get current page _notes = _notes[_offset_index:_last_item_index] # Body content _template = loader.get_template("index.html") _context = Context( { "page_title": _page_title, "notes": _notes, "islogin": _islogin, "userid": __user_id(request), "self_home": _self_home, "user": _user, "page_bar": _page_bar, "friends": _friends, "login_user_friend_list": _login_user_friend_list, } ) _output = _template.render(_context) return HttpResponse(_output)