def deletetheentry():
    #data = request.get_json()
    data = request.form['character_delete']
    if not data:
        return "Please add name of the character"
    print(data)
    #Name = data['name']
    session = get_session()
    cnt = session.query(Character).count()
    query = session.query(Character).filter_by(name=data).one()

    try:
        if (cnt > 5):
            print("Inside the delete")
            session.delete(query)
            # session.Flush()
            session.commit()
        else:
            return ("Cannot delete")

    except NoResultFound:
        traceback.print_exc()
        session.rollback()
        return ("failed to delete")
    finally:
        session.close()

    message = "deleted"
    return message
def insert_characters():
    #data = request.get_json()
    Name = request.form['Name']
    Description = request.form['character_info']
    Power = request.form['character_power']

    if not Name:
        return "All fields are required "
    if not Description:
        return "All fields are required "
    if not Power:
        return "All fields are required "
    session = get_session()
    character = Character(name=Name, description=Description, power=Power)
    try:
        session.add(character)
        session.commit()

    except NoResultFound:
        session.rollback()
        return ("failed to add")
    finally:
        session.close()
    message = "added successfully"

    return "successfully added"
Exemple #3
0
 def test_displayAll(self):
     print(
         os.path.dirname(
             os.path.abspath(
                 '/home/misba/Documents/largeapp2/database/database_session/'
             )))
     self.session = get_session()
     self.charactercount = self.session.query(Character).count()
    def test_displayAll(self):
        self.session = get_session()
        self.charactercount = self.session.query(Character).count()
        self.assertEqual(self.charactercount, 5)

        self.assertNotEqual(self.charactercount, 4)

        self.assertRaisesRegex(NoResultFound,
                               "Please add the name of the character")
def getAllTheDataFronTheDatabase():
    session = get_session()
    print(session.query(Character).count())
    info = session.query(Character).filter().all()
    # for result in info:
    #     # print(str(results))
    #     print(result.name + result.description)
    # print(info[0])
    return info
def returndescription():
    #data = request.args.get('name')
    data = request.form['char_name']
    print(data)
    # if not data:
    #      raise NoResultFound('Please add the name of the character')
    try:
        session = get_session()
        info = session.query(Character).filter(Character.name == data).one()
    except NoResultFound:
        return data + 'was not found'

    #return str(info)
    return render_template('returndescription.html', message=info)
    def test_deleteentry(self):

        # database count greater that 4
        self.session = get_session()
        self.charactercount = self.session.query(Character).count()
        self.assertGreater(self.charactercount, 4)

        #if name is not equal
        self.info = self.session.query(Character).filter_by(
            name='Dr. Mario').one()
        self.tempname = 'Dr. Msdhi'
        self.assertNotEqual(self.tempname, self.info.name)

        self.assertRaisesRegex(NoResultFound,
                               "Please add name of the character")
    def test_returndescription(self):
        #self.assertRaises(NoResultFound, returndescription)
        self.session = get_session()
        self.info = self.session.query(Character).filter_by(
            name='Dr. Mario').one()
        self.character = Character(name='Dr. Mario',
                                   description='KO power.',
                                   power='  KO power.')
        self.assertEqual(self.info.name, self.character.name)
        print(self.info.description)
        print(self.character.description)
        self.assertEqual(self.info.description, self.character.description)
        self.assertEqual(self.info.power, self.character.power)

        self.tempname = 'Dr. Msdhi'
        self.assertNotEqual(self.tempname, self.character.name)

        self.assertRaisesRegex(NoResultFound,
                               "Please add the name of the character")
def updateTheEntry():
    # data =  request.get_json()
    # character_name = request.form('Character')
    # print(character_name)
    character_name = request.form['Character']
    description = request.form['character_info']
    if not description:
        return "Please add discription"
    print(character_name)

    session = get_session()
    info = session.query(Character).filter(
        Character.name == character_name).one()
    power = info.power

    Id = info.id
    power = info.power
    power_level = info.power_level
    session.delete(info)

    character = Character(id=Id,
                          name=character_name,
                          description=description,
                          power=power,
                          power_level=power_level)
    print(character)
    try:
        session.add(character)
        session.commit()

    except NoResultFound:
        session.rollback()
        return ("failed to update")
    finally:
        session.close()

    return "successfully Updated"
from sqlalchemy import create_engine, literal, func
from sqlalchemy.orm import session, scoped_session, sessionmaker

from database.database_session import Session, exists, get_session
from mappings.characters_mapping import Character

engine = create_engine('sqlite:///database3.db', echo=True)
character = Character(
    name='Ross Bing',
    description=
    'Chandler Muriel Bing is a fictional character from the NBC sitcom Friends, portrayed by Matthew Perry..',
    power='comedian')
session = get_session()
Session = scoped_session(sessionmaker(bind=engine))
true, false = literal(True), literal(False)
(ret, ), = Session.query(exists().where(Character.name == 'Ross Bing'))

#q = session.query(Character).filter(character.name == 'mka Bing')
#session.query(q.exists())

print(ret)

if ret:
    print("already exists")
else:
    print("Does not exist")
session.commit()
session.close()