Ejemplo n.º 1
0
 def join_room(cls, roomcode, username):
     target = RoomModel.find_by_name(roomcode)
     if target:
         new_member = MemberModel(username, 1, target.id)
         new_member.save_to_db()
         return "", 200
     else:
         return "Invalid Room Name", 400
Ejemplo n.º 2
0
 def make_room(cls, roomcode, username):
     target = RoomModel.find_by_name(roomcode)
     if target:
         return "Room already exists with that name", 400
     else:
         new_room = RoomModel(roomcode)
         new_room.save_to_db()
         new_member = MemberModel(username, 100, new_room.id)
         new_member.save_to_db()
         return "", 200
Ejemplo n.º 3
0
    def get_profile(cls, fam_id, member_id):
        member = MemberModel.find_by_id(member_id)
        family = FamilyModel.find_by_id(fam_id)
        # get first, last name from member
        first_name = member.first_name
        last_name = member.last_name
        # get admin_invite and member invite from family
        admin_invite = family.admin_invite
        member_invite = family.member_invite

        total = 0
        all_checkout = []
        # get family total(run through checkout)
        try:
            fam_checkout = CheckoutModel.filter_by_fam_id(fam_id)
            for each in fam_checkout:
                all_checkout.append(each.json())
                total += each.total
        except:
            cls.logger.exception("Error in getting all checkouts by family")
            return "Internal Server Error", 500, None
        result = {
            "first_name": first_name,
            "last_name": last_name,
            "total": total,
            "admin_invite": admin_invite,
            "member_invite": member_invite,
            "order_history": sorted(all_checkout, key=lambda x: x['id'], reverse=True),
        }
        return "", 200, result
Ejemplo n.º 4
0
    def save_feedback(cls, caller, record_url, record_duration):
        # make feedback model
        member = MemberModel.find_by_phone(caller[2:])
        if not member:
            cls.logger.exception("Called from a non-member.")
            return "Ill-formed Request", 400, None

        try:
            new_feedback = FeedbackModel(record_duration, record_url, "",
                                         member.id)
            new_feedback.save_to_db()
        except:
            cls.logger.exception("Error in creating a feedback model")
            return "Internal Server Error", 500, None

        # send eamil with feedback link
        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())

        loop = asyncio.get_event_loop()
        # send email to alert Umur, Ivan, Chris, and me
        tasks = [
            asyncio.ensure_future(
                Emailer.feedback(record_url, record_duration,
                                 member.first_name + ' ' + member.last_name,
                                 member.fam_id))
        ]

        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()
        return "", 201, "Success"
Ejemplo n.º 5
0
 def update_token(cls, token):
     try:
         updated_token = MemberModel.update_token(token)
         return "", 200, updated_token
     except:
         cls.logger("Error in updating token")
         return "Internal Server Error", 500, None
Ejemplo n.º 6
0
    def verify_member(cls, verification, member_id):
        member = MemberModel.find_by_id(member_id)
        if member and member.verified:
            return "", 200

        all_verifications = VerificationModel.filter_by_member_id(member_id)
        verified = False
        for each in all_verifications:
            if each.value == verification:
                verified = True
                break

        if verified:
            member = MemberModel.find_by_id(member_id)
            member.verified = True
            member.save_to_db()
            for each in all_verifications:
                each.delete_from_db()
            return "", 200
        else:
            return "Failed to verify", 400
Ejemplo n.º 7
0
 def signin(cls, data):
     member = MemberModel.find_by_email(data['email'])
     if member:
         if member.validate_password(data['password']):
             try:
                 token = member.generate_token()
             except:
                 cls.logger.exception("Error generating token")
                 return "", 500, None
             return "", 200, token
         else:
             return "Invalid combination", 403, None
     else:
         return "An account doesn't exist with this email.", 400, None
Ejemplo n.º 8
0
    def register_family(cls, data, member_id):
        # check if family with the same name exist
        fam_already = FamilyModel.find_by_name(data['name'])
        if fam_already:
            return "Name should be unique", 400, None
        registerer = MemberModel.find_by_id(member_id)

        try:
            admin_invite = FamilyController.generate_invite(8)
            member_invite = FamilyController.generate_invite(8)
            new_fam = FamilyModel(data['address_line1'], data['address_line2'],
                                  data['city'], data['state'],
                                  data['zip_code'], data['name'],
                                  registerer.phone, registerer.email,
                                  admin_invite, member_invite)
            new_fam.save_to_db()
        except:
            cls.logger.exception("Error creating a family model")
            return "Internal Server Error", 500, None

        registerer.authority = 100
        registerer.fam_id = new_fam.id
        registerer.save_to_db()

        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())

        loop = asyncio.get_event_loop()

        # send email to who registered
        tasks = [
            asyncio.ensure_future(
                Emailer.create_fam(registerer.email, registerer.first_name,
                                   new_fam.admin_invite, new_fam.member_invite,
                                   new_fam.name))
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()

        return "", 201, {
            "admin": new_fam.admin_invite,
            "member": new_fam.member_invite
        }
Ejemplo n.º 9
0
    def register_list(cls, member_id, fam_id, alias, in_store):
        # make list_to_cart object
        member = MemberModel.find_by_id(member_id)
        already = ListToCartModel.get_fam_list(fam_id)
        for each in already:
            if each.alias == alias:
                return "Duplicate item in list", 400
        try:
            new_list_to_cart = ListToCartModel(
                alias, in_store, fam_id,
                member.first_name + " " + member.last_name)
            new_list_to_cart.save_to_db()
        except:
            cls.logger.exception("Error making a new ListToCart object")
            return "Internal Server Error", 500

        return "", 201
Ejemplo n.º 10
0
    def register_member(cls, data):
        # check if family with the same name exist
        member_already = MemberModel.find_by_email(data['email'])
        if member_already:
            return "Try a different E-mail", 400, None
        member_already = MemberModel.find_by_phone(data['phone'])
        if member_already:
            return "Try a different Phone number", 400, None

        try: 
            new_member = MemberModel(data['first_name'], data['last_name'], data['email'], data['phone'], data['password'])

            if os.environ.get("SECRET", "dev") == "dev":
                rand_string = "test"
            else:
                valid = True
                alphanums = string.digits + string.ascii_lowercase
                while valid:
                    rand_string = ""
                    for i in range(7):
                        rand_string += choice(alphanums)
                    if not VerificationModel.find_by_value(rand_string):
                        valid = False
            new_member.save_to_db()
            new_verification = VerificationModel(rand_string, new_member.id)
            new_verification.save_to_db()
        except:
            cls.logger.exception("Error creating a member.")
            return "Internal Server Error", 500, None

        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())
        
        loop = asyncio.get_event_loop()

        # send verification code to who registered
        tasks = [
            asyncio.ensure_future(Emailer.send_verification(new_member.email, new_member.first_name, new_verification.value))
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()

        return "", 201, new_member.generate_token()
Ejemplo n.º 11
0
    def resend_verification(cls, member_id):
        if os.environ.get("SECRET", "dev") == "dev":
            rand_string = "retest"
        else:
            try:
                valid = True
                alphanums = string.digits + string.ascii_lowercase
                rand_string = ""
                while valid:
                    rand_string = ""
                    for i in range(7):
                        rand_string += choice(alphanums)
                    if not VerificationModel.find_by_value(rand_string):
                        valid = False
            except:
                cls.logger.exception("Error while creating random string")
                return "Internal Server Error", 500
        try:
            new_verification = VerificationModel(rand_string, member_id)
            new_verification.save_to_db()
        except:
            cls.logger.exception("Error while saving to db")
            return "Internal Server Error", 500

        member = MemberModel.find_by_id(member_id)
        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())

        loop = asyncio.get_event_loop()

        # send verification code to who registered
        tasks = [
            asyncio.ensure_future(
                Emailer.send_verification(member.email, member.first_name,
                                          new_verification.value))
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()

        return "", 201
Ejemplo n.º 12
0
    def join_family(cls, invite_code, member_id):
        family = FamilyModel.find_by_invite_admin(invite_code)
        member = MemberModel.find_by_id(member_id)
        if family:
            fam_id = family.id
            invite_code_admin = family.admin_invite
            invite_code_member = family.member_invite
            authority = 100
        else:
            family = FamilyModel.find_by_invite_member(invite_code)
            if family:
                fam_id = family.id
                authority = 50
            else:
                cls.logger.exception("Invalid invite_code")
                return "Ill-formed Request", 400, None
        member.authority = authority
        member.fam_id = fam_id
        member.save_to_db()

        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())

        loop = asyncio.get_event_loop()

        # send email to who joined
        tasks = [
            asyncio.ensure_future(
                Emailer.join_fam(member.email, member.first_name, family.name))
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()

        return "", 200, family.name
Ejemplo n.º 13
0
    def checkout(cls, member_id, fam_id, total, items):
        # make checkout object
        member = MemberModel.find_by_id(member_id)
        if member.authority != 100:
            return "Unauthorized Request", 403

        try:
            new_checkout = CheckoutModel(total, fam_id, member_id)
            new_checkout.save_to_db()
        except:
            cls.logger.exception("Error creating new Checkout object")
            return "Internal Server Error", 500

        item_data = []
        for list_to_cart_id in items:
            target = ListToCartModel.find_by_id(list_to_cart_id)
            if target:
                if target.in_cart and target.item_name and target.item_image and target.item_price and target.item_quantity and not target.bought:
                    item_data.append(target.json())
                    target.bought = True
                    target.save_to_db()
                else:
                    cls.logger.exception(
                        "Requested a list_to_cart that doens't have sufficient info for checkout"
                    )
                    return "Ill-formed Request", 400
            else:
                cls.logger.exception(
                    "Reqeusts a list_to_cart that doesn't exist")
                return "Ill-formed Request", 400

        # make the list of objects
        for product in item_data:
            try:
                new_product = ProductModel(product['in_store'],
                                           product['item_price'],
                                           product['item_image'],
                                           product['item_name'],
                                           new_checkout.id)
                new_product.save_to_db()
            except:
                cls.logger.exception("Error creating new Product")
                return "Internal Server Error", 500

        family = FamilyModel.find_by_id(fam_id)

        try:
            loop = asyncio.get_event_loop()
            if loop.is_closed():
                asyncio.set_event_loop(asyncio.new_event_loop())
        except:
            asyncio.set_event_loop(asyncio.new_event_loop())

        loop = asyncio.get_event_loop()
        # send email to deliver :)
        tasks = [
            asyncio.ensure_future(
                Emailer.checkout(total,
                                 member.first_name + ' ' + member.last_name,
                                 family.name, item_data, new_checkout.id))
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()

        return "", 201