Пример #1
0
def create(petition: PetitionIn, expand: bool = False, token: str = Security(security)):
    if not allowed_to_control_petition(token):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Not authorized to control this petition",
        )
    try:
        petition_object, ci_uid = _generate_petition_object(petition)
    except FileNotFoundError:
        raise HTTPException(
            status_code=HTTP_404_NOT_FOUND,
            detail="Credential issuer is not validated, missing info/keys/credentials",
        )
    except ConnectionError:
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY,
            detail="Credential issuer server is not available",
        )

    p = Petition(
        petition=petition_object,
        petition_id=petition.petition_id,
        credential_issuer_uid=ci_uid,
        credential_issuer_url=petition.credential_issuer_url,
    )

    try:
        DBSession.add(p)
        DBSession.commit()
    except IntegrityError:
        DBSession.rollback()
        raise HTTPException(
            status_code=HTTP_409_CONFLICT, detail="Duplicate Petition Id"
        )
    return p.publish(expand)
Пример #2
0
def create_user(data):
    time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    db_session = DBSession()
    print(data)
    user = db_session.query(User).filter(
        User.email == data['email']).first()
    if user:
        return {'status': 'error',
                'message': 'message'}
    password = sha512(data['password'].encode('utf-8')).hexdigest()
    new_user = User(
        username=data['username'], password=password,
        email=data['email'], confirmed_at=time, is_active=True,
        is_admin=data['is_admin'])

    try:
        db_session.add(new_user)
        db_session.commit()

        result = {'status': 'OK',
                  'user': data['username']}
    except:
        db_session.rollback()
        result = {'status': 'error'}

    db_session.close()
    return result
Пример #3
0
def update_user(user_id, data):
    data = {k: data[k] for k in data if k in ['username', 'password',
                                              'new_password', 'email']}
    db_session = DBSession()

    user = db_session.query(User).get(user_id)
    password = sha512(data['password'].encode('utf-8')).hexdigest()
    if (not user or user.password != password):
        return {'status': 'error'}
    if (user.password == password and data['new_password'] != ''):
        new_password = sha512(
            data['new_password'].encode('utf-8')).hexdigest()
        user.username = data['username']
        user.email = data['email']
        user.password = new_password
    elif (user.password == data['password'] and data['new_password'] == ''):
        user.username = data['username']
        user.email = data['email']
        user.password = data['password']
    else:
        return {'status': 'error'}
    db_session.commit()
    db_session.close()

    return {'status': 'OK',
            'user': user_id}
Пример #4
0
def delete_type(type_id):
    if not (type_id):
        return {'status': 'error'}

    db_session = DBSession()
    db_session.query(Types).filter(Types.id == type_id).delete()
    db_session.commit()
    db_session.close()

    return {'status': 'OK'}
Пример #5
0
def delete_outhor(author_id):
    if not (author_id):
        return {'status': 'error'}
    print(type(author_id))
    db_session = DBSession()
    outhor = db_session.query(Authors).get(author_id)
    db_session.delete(outhor)
    db_session.commit()
    db_session.close()

    return {'status': 'OK'}
Пример #6
0
def delete_book(book_id):
    if not (book_id):
        return {'status': 'error'}

    db_session = DBSession()
    book = db_session.query(Books).get(book_id)
    db_session.delete(book)
    db_session.commit()
    db_session.close()

    return {'status': 'OK'}
Пример #7
0
async def count(petition_id: str):
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")

    if p.status != STATUS.CLOSED:
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY, detail="Petition still open"
        )

    p.count = zencode(CONTRACTS.COUNT_PETITION, keys=p.tally, data=p.petition)
    DBSession.commit()
    return json.loads(p.count)
Пример #8
0
def delete_user(user_id, data):
    if not user_id:
        return{'status': 'error'}

    db_session = DBSession()
    user = db_session.query(User).get(user_id)
    password = sha512(data['password'].encode('utf-8')).hexdigest()
    if (not user or data['username'] != user.username or
            password != user.password):
        return{'status': 'error'}
    db_session.query(User).filter(User.id == user_id).delete()
    db_session.commit()
    db_session.close()

    return {'status': 'OK'}
Пример #9
0
def create_author(data):
    db_session = DBSession()
    new_author = Authors(**data)

    try:
        db_session.add(new_author)
        db_session.commit()

        result = {'status': 'OK',
                  'author': data['fullname']}
    except:
        db_session.rollback()
        result = {'status': 'error'}

    db_session.close()
    return result
Пример #10
0
async def sign(petition_id: str, signature: PetitionSignature, expand: bool = False):
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")

    try:
        petition = zencode(
            CONTRACTS.ADD_SIGNATURE, keys=p.petition, data=signature.json()
        )
    except Error as e:
        debug(e)
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY,
            detail="Petition signature is duplicate or not valid",
        )
    p.petition = petition

    DBSession.commit()
    return p.publish(expand)
Пример #11
0
def create_book(data):
    db_session = DBSession()

    new_book = Books(name=data['name'],
                     type_id=data['type_id'],
                     author_id=data['author_id'],
                     book_translator=data['book_translator'])

    try:
        db_session.add(new_book)
        db_session.commit()

        result = {'status': 'OK', 'book': data['name']}
    except:
        db_session.rollback()
        result = {'status': 'error'}

    db_session.close()
    return result
Пример #12
0
def create_type(data):
    db_session = DBSession()

    book_type = db_session.query(Types).filter(
        Types.name == data['name']).first()
    if book_type:
        return {'status': 'error', 'message': 'message'}

    new_type = Types(name=data['name'])

    try:
        db_session.add(new_type)
        db_session.commit()

        result = {'status': 'OK', 'type': data['name']}
    except:
        db_session.rollback()
        result = {'status': 'error'}

    db_session.close()
    return result
Пример #13
0
async def tally(
    petition_id: str,
    authorizable_attribute: TallyBody = Body(...),
    expand: bool = False,
    token: str = Security(security),
):
    if not allowed_to_control_petition(token):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Not authorized to control this petition",
        )
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")
    petition = Bunch(
        petition_id=petition_id,
        credential_issuer_url=p.credential_issuer_url,
        authorizable_attribute_id=authorizable_attribute.authorizable_attribute_id,
    )
    credential = _retrieve_credential(petition)
    p.tally = zencode(CONTRACTS.TALLY_PETITION, keys=credential, data=p.petition)
    DBSession.commit()
    return p.publish(expand)