def create_friendship(user_id, target_id, state='pending'): if User.get(id=user_id, ignore=404) is None or User.get(id=target_id, ignore=404) is None: raise FaceduckError("001") if exists_friendship(user_id, target_id): raise FaceduckError("001") friendship = Friendship(meta={'id': uuid.uuid4()}, user_id=user_id, target_id=target_id, state=state) friendship.save() return friendship
def create_user(username, email, password, name, surname, birthday, gender): if username_already_exists(username): raise FaceduckError("002") if email_already_exists(email): raise FaceduckError("003") id = uuid.uuid4() password = generate_password_hash(password) user = User(meta={'id': id}, username=username, email=email, password=password, name=name, surname=surname, birthday=birthday, gender=gender) user.save() return user
def get_user(user_id): try: user = User.get(id=user_id) except NotFoundError: raise FaceduckError("001") return user
def create_post(text, author_id, image_url, visibility): id = uuid.uuid4() created_at = str(datetime.now().time()) split_post = text.split(' ') tags = [] for word in split_post: word = remove_punct(word) if len(word) > 0 and word[0] == '#': tags.append(word) if User.get(id=author_id, ignore=404) is None: raise FaceduckError("001") if image_url is None or len(image_url) == 0: image_url = social_card_for_post(text) post = Post(meta={'id': id}, text=text, created_at=created_at, author=author_id, image_url=image_url, tags=tags, visibility=visibility) post.save(refresh=True) return post
def update_friendship(user_id, target_id, state): states = ['friends'] if state not in states: raise FaceduckError("001") if User.get(id=user_id, ignore=404) is None or User.get(id=target_id, ignore=404) is None: raise FaceduckError("001") friendship = exists_friendship(target_id, user_id) if friendship is False: raise FaceduckError("001") friendship.state = state friendship.save() return friendship
def username_already_exists(username): username_response = User.search().from_dict({ "query": { "match_phrase": { "username": username } } }).execute() return len(username_response.hits) > 0
def email_already_exists(email): email_response = User.search().from_dict({ "query": { "match_phrase": { "email": email } } }).execute() return len(email_response.hits) > 0
def get_user_by_email(email): email_response = User.search().from_dict({ "query": { "match_phrase": { "email": email } } }).doc_type(User).execute() if len(email_response.hits) == 0: return None return email_response.hits[0]
def identity(uid): return User.get(id=uid)
def get_all_users(): return User.search().query("match_all").scan()