コード例 #1
0
def add_family():
    data = json.loads(request.get_data())
    data = check_data(FamilySchema, data)  #
    currentUserId = get_jwt_identity()
    family = Family(entries=data)
    family.created_by = currentUserId
    family.id = generateID()
    print(family)
    mongo.db.family.insert_one(family.serialize())
    return family.serialize()
コード例 #2
0
def add_person():  #
    currentUserId = get_jwt_identity()
    data = json.loads(request.get_data())
    data = check_data(PersonSchema, data)  #
    person = Person(entries=data)
    person.user_id = currentUserId
    person.id = generateID()
    print(person.serialize())
    mongo.db.person.insert_one(person.serialize())
    return person.serialize()
コード例 #3
0
def add_article():
    data = json.loads(request.get_data())
    data = check_data(ArticleSchema,data)
    user_id = get_jwt_identity()
    data['_id']=generateID()
    data['user_id']=user_id
    data['created_time']=currentTime()
    data['modified_time']=data['created_time']
    data['click_num']=data['like_num']=data['favorite_num']=0
    mongo.db.article.insert_one(data)
    article = mongo.db.article.find_one_or_404({'_id':data['_id']})
    return article
コード例 #4
0
def add_relation():
    is_admin = get_jwt()['is_admin']
    data = json.loads(request.get_data())
    data = check_data(RelationSchema, data)
    current_user_id = get_jwt_identity()
    relation = Relation(entries=data)
    family = mongo.db.family.find_one_or_404({'_id': relation.family_id})
    family = Family(entries=family)

    if check_family_edit_auth(family, current_user_id,
                              is_admin):  # todo:need to add admins
        relation.id = generateID()
        relation.user_id = current_user_id
        mongo.db.relation.insert_one(relation)
        return relation.serialize()
    else:
        raise ApiError(NO_AUTH, 403)
コード例 #5
0
def add_comment(id):
    data = json.loads(request.get_data())
    data = check_data(CommentSchema, data)
    user_id = get_jwt_identity()
    is_admin = get_jwt()['is_admin']
    query = {"_id": id}
    articles = mongo.db.article.find_one_or_404({'_id': id})
    article = Article(entries=articles)
    #TODO: 权限校验
    if check_article_like_auth(article, is_admin, user_id):
        comment = dict()
        comment['_id'] = generateID()
        comment['user_id'] = user_id
        comment['article_id'] = id
        comment['content'] = data['content']
        comment['time'] = currentTime()
        mongo.db.comment.insert_one(comment)
        return comment
    raise ApiError(NO_AUTH, 403)
コード例 #6
0
def add_like(id):
    user_id = get_jwt_identity()
    is_admin = get_jwt()['is_admin']
    like_query = {"$and": [{"user_id": user_id}, {"article_id": id}]}
    like = mongo.db.like.find_one(like_query)
    if like != None:
        raise ApiError(REPETITIVE_OPERATION)
    query = {"_id": id}
    articles = mongo.db.article.find_one_or_404({'_id': id})
    article = Article(entries=articles)
    if check_article_like_auth(article, is_admin, user_id):
        update_data = {"$inc": {"like_num": 1}}
        mongo.db.article.update_one(query, update_data)
        like = dict()
        like['_id'] = generateID()
        like['user_id'] = user_id
        like['article_id'] = article.id
        like['time'] = currentTime()
        mongo.db.like.insert_one(like)
        return like
    raise ApiError(NO_AUTH, 403)
コード例 #7
0
    async def sendEmail(self, ctx, name: str, email: str, manual=True):
        """Send a verification email to the given email"""
        await ctx.message.add_reaction(self.emojis['loading'])

        otp = generateID(seed='01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')

        # Creating the email
        msg = EmailMessage()
        msg['Subject'] = f'Verification of {ctx.author} in {ctx.guild}'
        msg['From'] = config.email
        msg['To'] = email

        if manual:
            command = f'{ctx.clean_prefix}{ctx.command.parent} code {otp}'
        else:
            command = otp
        variables = {
            '{$user}': name,
            '{$otp}': otp,
            '{$guild}': ctx.guild.name,
            '{$channel}': 'https://discord.com/channels/' +
            f'{ctx.guild.id}/{ctx.channel.id}',
            '{$command}': command
        }
        with open('utils/verification.html') as f:
            html = f.read()
        html = re.sub(r'({\$\w+})', lambda x: variables[x.group(0)], html)
        msg.set_content(html, subtype='html')

        # Sending the email
        with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
            smtp.login(config.email, config.password_token)
            smtp.send_message(msg)

        await ctx.message.remove_reaction(self.emojis['loading'],
                                          self.bot.user)

        self.codes[ctx.author.id] = otp