def authenticate(self, oauth_profile, username, request): source = oauth_profile['source'] source_uid = oauth_profile['source_uid'] device_uid = oauth_profile['device_uid'] try: user = self.user_document.objects.get(username=username) if source not in user.accounts: user.accounts[source] = oauth_profile if device_uid not in user.device_uids: user.device_uids.append(device_uid) user.record_login_history() except self.user_document.DoesNotExist: user_document = self.user_document user = user_document(username=username, source=source, source_uid=source_uid) import datetime from people.models import User user.create_time = datetime.datetime.utcnow() user.accounts[source] = oauth_profile user.authenticate_by_oauth() device_users = User.objects(id=device_uid) device_user = None if device_users: device_user = device_users[0] user.device_uids = [device_uid] user.feature = device_user.feature user.record_login_history() if device_user: user.combine_user_log(device_user) else: user.init_warm_data() return user
def main(): usable_ids = warm_conn.keys('b:s:a*') usable_ids = map(lambda _id: int(_id.split(':')[-1]), usable_ids) articles = Article.objects(seq_id__in=usable_ids).only('seq_id', 'id', 'category', 'hot', 'quality', 'published_at', 'feature') update_time = datetime.datetime.utcnow() set_global_cal_time(update_time) seq_id_article_mapping = dict(((a.seq_id, (a, calculate_scores(0, 0, a.published_at, a.quality, a.hot, update_time, update_time))) for a in articles)) User.recompute_common_candidateset(usable_ids, seq_id_article_mapping) users = User.objects.only('id', 'seq_id', 'feature', 'recent_feature', 'feature_updated_time', 'candidate_updated_time').all() usable_ids_set = set(usable_ids) for user in users: ids, _ = user.get_seen_article_ids_and_last_datetime() _usable_ids = usable_ids_set - set(ids) print user, user.seq_id, len(_usable_ids) short_feature, long_feature, _ = user.update_warm_feature() User.recompute_candidateset(user, short_feature, long_feature, seq_id_article_mapping, subset=_usable_ids)
def post(self, request): username = request.POST['username'] password = request.POST['password'] email = request.POST['email'] try: user = User.create_user(username=username,password=password,email=email) except Exception, e: raise("Cannot create_user")
def handle(self, *args, **options): i = 100 while i > 0: i -= 1 out_green('=' * 80) first_name = create_random_string(10) last_name = create_random_string(10) user = User() user.email = create_random_email() user.username = first_name.lower() user.first_name = first_name user.last_name = last_name user.company_id = random.randint(1, 2) user.save()
def initial(self, request, user_id, *args, **kwargs): if user_id == 'mine': self.user = request.user self.user_id = str(request.user.id) self.mine = True else: self.user_id = user_id self.user = User.get(user_id) self.mine = False self.set_owner(str(self.user.id)) super(UserBaseView, self).initial(request, *args, **kwargs)
def post(self, request, *args, **kwargs): password = request.DATA.get(KEY_PASSWORD) phone = request.DATA.get(KEY_USERNAME) username = User.get_username_by_phone(phone) serializer = UsernameLoginTokenSerializer(data=dict(username=username, password=password)) if not serializer.is_valid(): raise UserNameOrPasswordIncorrect user = serializer.object['user'] login_info = UserLoginSerializer(user).data token = serializer.object['token'] login_info[KEY_TOKEN] = token return Response(login_info)
def generate_verification_code(cls, receiver, type=TYPE_PHONE): user = User.objects(phone=receiver) if user: raise IdentityHasBinded() now = datetime.utcnow() verified_receivers = cls.objects(receiver=receiver, created_at__gt=now - timedelta(minutes=1)) if verified_receivers: raise VerifyOverFrequency() now = datetime.utcnow() expires_in = timedelta(hours=3) expires_at = now + expires_in code = random.randint(100000, 999999) vcode = cls.objects.create(receiver=receiver, code=code, created_at=now, expires_at=expires_at) return vcode.code
def insert(request): if request.method == "POST": user = User() username = request.POST.get("username", None) password = request.POST.get("password", None) user.username = username user.password = password user.save() return render(request, 'insert.html')
def new_comment(request, key): target = db.get(key) try: isAjax = request.REQUEST['isAjax'] returnurl = None except: try: returnurl = request.REQUEST['returnurl'] except: returnurl = None return create_object(request, form_class=CommentForm, extra_fields = {'author': User.get_current_user(), 'target': target}, extra_context = locals(), post_save_redirect = returnurl )
def user_login_or_register(request, userinfo): source = userinfo.get(KEY_SOURCE) if not source in SOURCES: raise UserIdentityInvalid source_uid = userinfo.get(KEY_SOURCE_UID) username = User.generate_username(source_uid, source) if source in SOURCES_DEVICES: _data = dict(device_info=userinfo, username=username, request=request) else: _data = dict(oauth_profile=userinfo, username=username, request=request) # pylint: disable=E1123, E1120 serializer = OAuthProfileLoginTokenSerializer(data=_data) # pylint: enable=E1123, E1120 if not serializer.is_valid(): raise UserIdentityInvalid user = serializer.object['user'] return user, serializer.object['token']
def post(self, request, *args, **kwargs): self.verify(request) password = request.DATA.get(KEY_PASSWORD) phone = request.DATA.get(KEY_USERNAME) receiver = request.DATA.get(KEY_VERIFY_RECEIVER) if not phone == receiver: raise UserNameOrPasswordIncorrect username = User.generate_phone_username(phone) u = User.objects.create(username=username, phone=phone, source='phone', source_uid=phone, gender=MALE) u.set_password(password) serializer = UsernameLoginTokenSerializer(data=dict(username=username, password=password)) if not serializer.is_valid(): raise UserNameOrPasswordIncorrect user = serializer.object['user'] login_info = UserLoginSerializer(user).data token = serializer.object['token'] login_info[KEY_TOKEN] = token return Response(login_info)
import sys, os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from people import db from people.models import User, Notification from werkzeug.security import generate_password_hash import time db.drop_all() db.create_all() ts = time.time() user = User("00admin", "Admin", "Account", generate_password_hash("plaintextpassword"), ts) user.active = True user.confirmed_at = ts notification = Notification("Info", ts, "Hello People", "Test notification", None, user) notification2 = Notification("Warning", ts, "Important message!!!", "This is a very important message.", user, user) db.session.add(user) db.session.add(notification) db.session.add(notification2) db.session.commit()
def get_author(self): from people.models import User return User.get(self.author_id)
def new_article(request): return create_object(request, form_class=ArticleForm, extra_fields = {'author': User.get_current_user()})