def register(request): email = request.POST.get(EMAIL, '') password = request.POST.get(PASSWORD, '') if not email or not password: return json_http(None) first_name = request.POST.get(FIRST_NAME, '') last_name = request.POST.get(LAST_NAME, '') phone = request.POST.get(PHONE, '') photo = request.FILES.get(PHOTO, '') device_token = request.POST.get(DEVICE_TOKEN, '') response_data = {ERROR_CODE:NO_ERROR} try: users = User.objects.filter(email=email) for user in users: if user.social_id: continue response_data[ERROR_CODE] = EMAIL_EXISTED response_data[ERROR_MESSAGE] = EMAIL_EXISTED_MESSAGE return json_http(response_data) user = User.objects.create(email=email) user.set_password(password) user.first_name = first_name user.last_name = last_name user.phone = phone photo_url = save_file(file=photo, upload_to='user') user.photo = photo_url or IMAGE_DEFAULT user.device_token = device_token user_token = default_token_generator.make_token(user) user.user_token = user_token user.save() user_login = gauth.authenticate(email=email, password=password) gauth.login(request, user_login) response_data[USER_TOKEN] = user_login.user_token response_data[USER_ID] = user_login.id response_data[FIRST_NAME] = user_login.first_name response_data[LAST_NAME] = user_login.last_name photo_link = "" if unicode(user_login.photo): photo_link = settings.MEDIA_URL + unicode(user_login.photo) response_data[PHOTO] = photo_link response_data[NUM_FOLLOWER] = user_login.num_follower response_data[NUM_LIKE] = user_login.num_like response_data[NUM_PHOTO] = user_login.num_photo response_data[SOCIAL_ID] = user_login.social_id response_data[DEVICE_TOKEN] = user_login.device_token except: response_data[ERROR_CODE] = UNKNOWN_ERROR response_data[ERROR_MESSAGE] = UNKNOWN_ERROR_MESSAGE return json_http(response_data) response_data[USER_TOKEN] = user.user_token return json_http(response_data)
def get(self, path): try: SID, LSID, AUTH = gauth.authenticate('nlightreademo', 'thecakeisapie') rq = urllib2.Request('http://google.com/reader' + self.request.path_qs) #SID Cookies are deprecated as of 24.06.2009 #rq.add_header("Cookie", SID) rq.add_header("Authorization", "GoogleLogin auth=" + AUTH.replace("Auth=", "")) r = urllib2.urlopen(rq) self.response.out.write("".join(r.readlines())) except urllib2.HTTPError, e: self.error(e.code) self.response.out.write("".join(e.readlines()))
def login_via_facebook(request): social_id = request.POST.get(SOCIAL_ID, '') if not social_id: return email = request.POST.get(EMAIL, '') photo = request.POST.get(PHOTO, '') first_name = request.POST.get(FIRST_NAME, '') last_name = request.POST.get(LAST_NAME, '') phone = request.POST.get(PHONE, '') device_token = request.POST.get(DEVICE_TOKEN, '') response_data = {ERROR_CODE:NO_ERROR} if not device_token: response_data[ERROR_CODE] = DEVICE_TOKEN_NOT_SET response_data[ERROR_MESSAGE] = DEVICE_TOKEN_NOT_SET_MESSAGE return json_http(response_data) try: user, created = User.objects.get_or_create(social_id=social_id) user_token = User.objects.make_random_password() user.user_token = user_token user.email = email user.device_token = device_token user.photo = photo user.first_name = first_name user.last_name = last_name user.phone = phone user.save() user_login = gauth.authenticate(user_token=user_token) gauth.login(request, user_login) response_data[USER_TOKEN] = user_login.user_token response_data[USER_ID] = user_login.id response_data[FIRST_NAME] = user_login.first_name response_data[LAST_NAME] = user_login.last_name response_data[PHOTO] = photo response_data[NUM_FOLLOWER] = user_login.num_follower response_data[NUM_LIKE] = user_login.num_like response_data[NUM_PHOTO] = user_login.num_photo response_data[SOCIAL_ID] = user_login.social_id response_data[DEVICE_TOKEN] = user_login.device_token except: response_data[ERROR_CODE] = UNKNOWN_ERROR response_data[ERROR_MESSAGE] = UNKNOWN_ERROR_MESSAGE return json_http(response_data) return json_http(response_data)
def login(request): email = request.POST.get(EMAIL, '') password = request.POST.get(PASSWORD, '') if not email or not password: return json_http(None) response_data = {ERROR_CODE:NO_ERROR} device_token = request.POST.get(DEVICE_TOKEN, '') try: user_login = gauth.authenticate(email=email, password=password) if user_login is None: response_data[ERROR_CODE] = WRONG_EMAIL_OR_PASSWORD response_data[ERROR_MESSAGE] = WRONG_EMAIL_OR_PASSWORD_MESSAGE return json_http(response_data) user_token = default_token_generator.make_token(user_login) user_login.user_token = user_token user_login.save() gauth.login(request, user_login) user_login.device_token = device_token user_login.save() response_data[USER_TOKEN] = user_login.user_token response_data[USER_ID] = user_login.id response_data[FIRST_NAME] = user_login.first_name response_data[LAST_NAME] = user_login.last_name photo_link = unicode(user_login.photo) if '://' in unicode(user_login.photo) else settings.MEDIA_URL + unicode(user_login.photo) response_data[PHOTO] = photo_link response_data[NUM_FOLLOWER] = user_login.num_follower response_data[NUM_LIKE] = user_login.num_like response_data[NUM_PHOTO] = user_login.num_photo response_data[SOCIAL_ID] = user_login.social_id response_data[DEVICE_TOKEN] = user_login.device_token except User.DoesNotExist: response_data[ERROR_CODE] = WRONG_EMAIL return json_http(response_data)
def get(self): SID, LSID, AUTH = gauth.authenticate('nlightreademo', 'thecakeisapie') self.response.headers['Content-Type'] = 'text/plain; charset=utf8' self.response.out.write(gauth.get_token(AUTH))