def api_user_login(request): api_result = {"api": "login"} username = request.POST["username"] password = request.POST["password"] print username, password user = authenticate(username=username, password=password) print user.pk if user is not None: if user.is_active: login(request, user) api_result["status"] = "success" else: # Return a 'disabled account' error message api_result["status"] = "failure" else: # Return an 'invalid login' error message. api_result["status"] = "failure" print request.user.is_authenticated() # if no polls in feed, initiate new user protocol polls = Feed.user_feed(user, 1, 10) if len(polls) == 0: create_feed_new_user(user) return JsonResponse(api_result)
def feed_ajax(request): try: page = int(request.GET["page"]) if page <= 0: page = 1 except: page = 1 new_questions = Feed.user_feed(request.user, page) context = {"nav": "home", "questions": new_questions} if new_questions: return render(request, "polls/v1_feed_ajax.html", context) else: return HttpResponse("no more polls")
def api_feed(request): api_result = {"api": "feed", "status": "success"} try: token = request.GET["token"] user = get_user_from_token(token) group_id = 0 try: group_id = request.GET["group_id"] except: pass page = 1 try: page = int(request.GET["page"]) except: pass page_size = 10 try: page_size = int(request.GET["page_size"]) except: pass if not user: api_result["status"] = "failure" api_result["error"] = "user not found" else: if group_id == 0: polls = Feed.user_feed(user, page, page_size) else: polls = Feed.group_feed(user, group_id, page, page_size) if page == 1 and not polls: # create_feed_new_user(user) # polls = Feed.user_feed(user, page, page_size) pass if polls: api_result["polls"] = json_polls(polls) else: api_result["polls"] = "None" except Exception as e: api_result["status"] = "failure" api_result["error"] = e.message pass return JsonResponse(api_result)
def home(request): if request.user.is_authenticated(): """ answered = [vote.question.id for vote in Vote.objects.filter(user=request.user)] ignored = [ig.question.id for ig in Ignored.objects.filter(user=request.user)] unanswered = Question.objects.exclude(id__in=answered).exclude(id__in=ignored) new_questions = random.sample(unanswered,min(len(unanswered),10)) """ new_questions = Feed.user_feed(request.user) for q in new_questions: try: suser = UserSocialAuth.objects.get(user_id=q.user.id) image_url = "http://graph.facebook.com/{0}/picture".format(suser.uid) except: image_url = "/static/polls/images/user.png" q.user.image_url = image_url context = {"nav": "home", "questions": new_questions} return render(request, "polls/v1_feed.html", context) else: if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data["username"] password = form.cleaned_data["password"] print username, password user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) status = "success" else: # Return a 'disabled account' error message status = "failure" else: # Return an 'invalid login' error message. status = "failure" print request.user.is_authenticated() """ url = HttpRequest.build_absolute_uri(request, '/polls/api/login/') print(url) values = { 'username': username, 'password': password } data = urllib.urlencode(values) req = urllib2.Request(url, data) response = urllib2.urlopen(req) res = response.read() res_json = json.loads(res) """ if status == "success": return HttpResponseRedirect("/") else: form._errors["username"] = ErrorList(["invalid username/password"]) else: form = LoginForm() # recently finished polls questions = Question.objects.all().order_by("-id")[:10] for q in questions: choices = [] for choice in q.choice_set.all(): choices.append({"cid": choice.id, "ctext": choice.choice_text, "cvotes": choice.votes}) choices = sorted(choices, key=lambda k: k["cvotes"], reverse=True) q.choices = json.dumps(choices) context = {"form": form, "nav": "home", "questions": questions} return render(request, "polls/v1_login.html", context)