Esempio n. 1
0
 def post(self):
     data = request.json
     user_id = get_jwt_identity()
     checkuser = session.query(Users).filter(Users.id == user_id).one()
     if checkuser.status == "admin":
         return Response(response=json.dumps(
             {"message": "Not allowed for admins"}),
                         status=400,
                         mimetype="application/json")
     try:
         credit = Credits(data["start_date"], data["end_date"],
                          data["start_sum"], data["current_sum"],
                          data["bank_id"], user_id)
         session.add(credit)
         session.commit()
         usercredit = UserCredit(credit.user_id, credit.id)
         session.add(usercredit)
         session.commit()
         return Response(response=json.dumps({"message": "Success"}),
                         status=200,
                         mimetype="application/json")
     except:
         return Response(response=json.dumps({"message": "Invalid input"}),
                         status=405,
                         mimetype="application/json")
Esempio n. 2
0
    def post(self):
        data = request.json
        try:
            user = Users(data["login"], data["password"], data["name"],
                         data["passport"], data["address"], data["email"],
                         data["phone_number"], data["status"])
            user.password = generate_password_hash(data['password'])

            checkuser = session.query(Users).filter(
                Users.login == user.login).all()
            if checkuser:
                return Response(response=json.dumps(
                    {"message": "user with such login already exist"}),
                                status=409,
                                mimetype="application/json")
            session.add(user)
            session.flush()
            session.commit()
            token = user.get_token()
            return Response(response=json.dumps({"message": "Success"}),
                            status=200,
                            mimetype="application/json")
        except:
            return Response(response=json.dumps({"message": "Invalid input"}),
                            status=405,
                            mimetype="application/json")
Esempio n. 3
0
 def put(self):
     data = request.json
     user_id = get_jwt_identity()
     try:
         user = session.query(Users).get(user_id)
         if not user:
             return Response(response=json.dumps({"message": "invalid id"}),
                             status=400,
                             mimetype="application/json")
         if "login" in data:
             user.login = data["login"]
         if "password" in data:
             user.password = generate_password_hash(data['password'])
         if "name" in data:
             user.name = data["name"]
         if "passport" in data:
             user.passport = data["passport"]
         if "address" in data:
             user.address = data["address"]
         if "email" in data:
             user.email = data["email"]
         if "phone_number" in data:
             user.phone_number = data["phone_number"]
         session.commit()
         return Response(response=json.dumps({"message": "Success"}),
                         status=200,
                         mimetype="application/json")
     except Exception as e:
         return Response(response=json.dumps({"message": "Invalid input"}),
                         status=405,
                         mimetype="application/json")
Esempio n. 4
0
 def delete(self):
     try:
         session.delete(self)
         session.commit()
     except Exception:
         session.rollback()
         raise
Esempio n. 5
0
 def save(self):
     try:
         session.add(self)
         session.commit()
     except Exception:
         session.rollback()
         raise
Esempio n. 6
0
def delete_contact(username=""):
    current_contact = session.query(Contact).filter_by(username=f'{username}', valid_till_date = None).first()
    if current_contact:
        current_contact.valid_till_date = func.now()
        session.commit()
        return jsonify(dict(message="Record deleted!"))
    else:
        return jsonify(dict(message="No record found!"))
Esempio n. 7
0
def add_email(username="",value=""):
    current_contact = session.query(Contact).filter_by(username=f'{username}').first()
    if current_contact:
        current_contact.emails.append(ContactEmail(value=value))
        session.commit()
        return jsonify(session.query(Contact).filter_by(id=current_contact.id).first().to_json())
    else:
        return jsonify(dict(message="Record not found!"))
Esempio n. 8
0
 def get_user_list(cls, user_id):
     try:
         videos = cls.query.filter(cls.user_id == user_id).all()
         session.commit()
     except Exception:
         session.rollback()
         raise
     return videos
Esempio n. 9
0
def create_contact(username="",first_name="",last_name="",email=""):
    new_contact = Contact(
        username=username,
        first_name=ContactFirstName(value=first_name),
        last_name=ContactLastName(value=last_name),
        emails=[ContactEmail(value=email)]
    )
    session.add(new_contact)
    session.commit()
    
    return jsonify([x.to_json() for x in session.query(Contact).filter_by(username=f'{username}')])
Esempio n. 10
0
    def update(cls, tutorials_id, user_id, kwargs):
        try:
            # for key,value in kwargs.items():
            # setattr(self,key,value)
            stmt = update(cls).where(
                and_(cls.id == tutorials_id,
                     cls.user_id == user_id)).values(**kwargs)
            session.execute(stmt)
            session.commit()
            item = cls.query.filter(cls.id == tutorials_id).first()
            # item = session.execute(""" select * from videos where id='{id_}' """.format(id_ = tutorials_id)).first()

        except Exception:
            session.rollback()
            raise
        return item
Esempio n. 11
0
def update_contact(username="", column="",value=""):
    current_contact = session.query(Contact).filter_by(username=f'{username}').first()
    if current_contact:
        if column == "first_name":
            session.query(ContactFirstName).filter_by(contact_id=current_contact.id, valid_till_date = None).first().valid_till_date = func.now()
            session.query(Contact).filter_by(id=current_contact.id).first().first_name = ContactFirstName(value=value)
        elif column == "last_name":
            session.query(ContactLastName).filter_by(contact_id=current_contact.id, valid_till_date = None).first().valid_till_date = func.now()
            session.query(Contact).filter_by(id=current_contact.id).first().last_name = ContactLastName(value=value)
        elif column == "email":
            for e in session.query(ContactEmail).filter_by(contact_id=current_contact.id, valid_till_date = None).all():
                e.valid_till_date = func.now()
            session.query(Contact).filter_by(id=current_contact.id).first().emails = [ContactEmail(value=value)]
    else:
        return jsonify(dict(message="No record found!"))
    
    session.commit()
    
    return jsonify(session.query(Contact).filter_by(username=f'{username}').first().to_json())
Esempio n. 12
0
 def post(self, credit_id):
     data = request.json
     user_id = get_jwt_identity()
     checkuser = session.query(Users).filter(Users.id == user_id).one()
     if checkuser.status == "admin":
         return Response(response=json.dumps(
             {"message": "Not allowed for admins"}),
                         status=400,
                         mimetype="application/json")
     try:
         transaction = Transactions(data["date"], data["summ"], credit_id)
         session.add(transaction)
         session.commit()
         return Response(response=json.dumps({"message": "Success"}),
                         status=200,
                         mimetype="application/json")
     except:
         return Response(response=json.dumps({"message": "Invalid input"}),
                         status=405,
                         mimetype="application/json")
Esempio n. 13
0
 def post(self):
     data = request.json
     user_id = get_jwt_identity()
     checkuser = session.query(Users).filter(Users.id == user_id).one()
     if checkuser.status != "admin":
         return Response(response=json.dumps(
             {"message": "Not allowed for users"}),
                         status=400,
                         mimetype="application/json")
     try:
         bank = Banks(data["all_money"], data["per_cent"])
         session.add(bank)
         session.commit()
         return Response(response=json.dumps({"message": "Success"}),
                         status=200,
                         mimetype="application/json")
     except:
         return Response(response=json.dumps({"message": "Invalid input"}),
                         status=405,
                         mimetype="application/json")
Esempio n. 14
0
 def put(self, credit_id):
     data = request.json
     user_id = get_jwt_identity()
     checkuser = session.query(Users).filter(Users.id == user_id).one()
     if checkuser.status == "admin":
         return Response(response=json.dumps(
             {"message": "Not allowed for admins"}),
                         status=400,
                         mimetype="application/json")
     try:
         credit = session.query(Credits).get(credit_id)
         if "current_sum" in data:
             credit.current_sum = data["current_sum"]
         session.commit()
         return Response(response=json.dumps({"message": "Success"}),
                         status=200,
                         mimetype="application/json")
     except Exception as e:
         return Response(response=json.dumps({"message": "Invalid input"}),
                         status=405,
                         mimetype="application/json")
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'
"""Скрипт удаляет последнюю ревизию."""

if __name__ == '__main__':
    from main import session, TextRevision

    text_revision = session.query(TextRevision).order_by(
        TextRevision.id.desc()).first()
    print(text_revision)
    session.delete(text_revision)
    session.commit()