Example #1
0
    def post(self, start_time, end_time, user_id):
        """
        Handles the creation of a new schedule.

        :param datetime.datetime start_time: The date at which the schedule
        opens.
        :param datetime.datetime end_time: The date at which the schedule
        closes.
        :param str user_id: The user to create the schedule for.
        :rtype: ScheduleTable
        :return: The newly scheduled table.
        """
        self._assert_valid_duration(start_time, end_time)
        self._assert_no_duration_overlap(start_time, end_time, user_id)
        self._assert_not_in_past(start_time, end_time)

        user_info = db.session.query(User).filter(
            User.public_id == user_id).first()

        if user_info is None:
            raise DAOException('Invalid user requested. Try again.')

        new_schedule = Schedule(start_time, end_time, user_id,
                                user_info.local_tz)

        exec_and_commit(db.session.add, new_schedule)

        return new_schedule
    def create_new_user(self,
                        email,
                        password,
                        local_tz,
                        username,
                        *,
                        skip_commit=False):
        """
        Handles the creation of a new user.

        :param str email: The user's email to be registered with the account.
        :param str password: The desired password (not yet hashed).
        :param str local_tz: The local TZ of the user
        :param str username: The desired username for the user.
        :param bool skip_commit: An optional flag used to indicate if we want
        to create the object and add to DB delta, but not commit it just yet.
        Used mostly for facade methods which roll back if the event doesn't
        fully complete (transaction doesn't finalize, &c.).
        :rtype: UserTable
        :return: The newly created user table.
        """
        new_user = User(
            email,
            password,
            local_tz,
            username,
        )

        exec_and_commit(db.session.add, new_user, skip_commit=skip_commit)

        return new_user
    def create_address(self,
                       user_public_id,
                       first_name,
                       last_name,
                       street_address,
                       locality,
                       region,
                       postal_code,
                       country_code_alpha2,
                       is_default=False,
                       extended_address=None,
                       *,
                       skip_commit=False):
        """
        Handles creation of a new address.

        :param str user_public_id: The user's public ID.
        :param str first_name: The user's first name.
        :param str last_name: The user's last name.
        :param str street_address: The first part of the address. The meat of
        it.
        :param str locality: The locality. Might be a city or town.
        :param str region: The region. Typically things like states (in the US)
        :param str postal_code: The postal code associated with the address.
        :param str country_code_alpha2: The country code. ('US', 'CA', 'MX')
        :param bool is_default: Set this to default on creation?
        :param str extended_address: (Default: None) The second part of the
        address. Typically things like apartment # etc.
        :param bool skip_commit: An optional flag used to indicate if we want
        to create the object and add to DB delta, but not commit it just yet.
        Used mostly for facade methods which roll back if the event doesn't
        fully complete (transaction doesn't finalize, &c.).
        :rtype: AddressTable
        :return: The newly created address.
        """
        new_address = Address(
            user_public_id,
            first_name,
            last_name,
            street_address,
            locality,
            region,
            postal_code,
            country_code_alpha2,
            is_default,
            extended_address,
        )

        if is_default:
            self._unset_default_address(user_public_id)

        exec_and_commit(db.session.add, new_address, skip_commit=skip_commit)

        return new_address
Example #4
0
    def create_submerchant(self,
                           submerchant_info,
                           register_as_business,
                           user_id,
                           *,
                           skip_commit=False):
        """
        Handles adding the submerchant into the database for tracking purposes.

        :param dict submerchant_info: The information to be used in
        submerchant creation.
        :param bool register_as_business: Boolean indiicating if registering
        as a individual (false) or as a business (True).
        :param bool skip_commit: An optional flag used to indicate if we want
        to create the object and add to DB delta, but not commit it just yet.
        Used mostly for facade methods which roll back if the event doesn't
        fully complete (transaction doesn't finalize, &c.).
        :rtype: dict
        :return: The submerchant's info.
        """

        new_submerchant = SubMerchant(
            user_id,
            submerchant_info['id'],
            submerchant_info['individual']['first_name'],
            submerchant_info['individual']['last_name'],
            submerchant_info['individual']['email'],
            submerchant_info['individual']['date_of_birth'],
            submerchant_info['individual']['address']['street_address'],
            submerchant_info['individual']['address']['locality'],
            submerchant_info['individual']['address']['region'],
            submerchant_info['individual']['address']['postal_code'],
        )

        if register_as_business:
            new_submerchant.legal_name = submerchant_info['business'][
                'legal_name']
            new_submerchant.dba_name = submerchant_info['business']['dba_name']
            new_submerchant.bus_address_street_address = submerchant_info[
                'business']['address']['street_address']
            new_submerchant.bus_address_locality = submerchant_info[
                'business']['address']['locality']
            new_submerchant.bus_address_region = submerchant_info['business'][
                'address']['region']
            new_submerchant.bus_address_zip = submerchant_info['business'][
                'address']['postal_code']

        exec_and_commit(db.session.add,
                        new_submerchant,
                        skip_commit=skip_commit)

        return submerchant_info
Example #5
0
    def create_customer(self,
                        bt_customer_id,
                        cc_token,
                        first_name,
                        last_name,
                        user_id,
                        *,
                        is_default=False,
                        skip_commit=False):
        """
        Handles creating a new mirrored customer in our database which
        represents the necessary information in our system. Note: these values
        also persist in Braintree.

        :param str bt_customer_id: The unique identifier generated by
        Braintree.
        :param str cc_token: A uniquely generated (by us) value for the payment
        method.
        :param str first_name: The customer's first name.
        :param str last_name: The customer's last name.
        :param str user_id: The customer's UUID.
        :param bool is_default: If we want this new payment method to be the
        default payment method.
        :rtype: CustomerTable
        :return: The newly created customer.
        """
        new_customer = Customer(bt_customer_id,
                                cc_token,
                                first_name,
                                last_name,
                                user_id,
                                is_default=is_default)

        try:
            exec_and_commit(
                db.session.add,
                new_customer,
                skip_commit=skip_commit,
            )
        except Exception as e:
            logging.critical(
                'Failed to create a new customer in our database: {0} with'
                'exception of: {1}'.format(
                    new_customer,
                    e,
                ))
            raise DAOException('Failed to create new customer.')

        return new_customer
Example #6
0
    def add_new_contact_message(self, name, message, email):
        """
        Handles arhciving a contact us email.

        :param str name: The user's name.
        :param str message: The user's message.
        :param str email: The user's email.
        :rtype: ContactTable
        :return: The new contact message.
        """
        new_submission = Contact(name, email, message)

        exec_and_commit(db.session.add, new_submission)

        return new_submission
Example #7
0
    def create_new_event(self,
                         scheduling_user_id,
                         scheduled_user_id,
                         localized_start_time,
                         localized_end_time,
                         local_tz,
                         notes=None,
                         *,
                         skip_commit=False):
        """
        Creates a new event for the scheduled user.

        :param str scheduling_user_id: The user creating the event.
        :param str scheduled_user_id: The user who's time is being purchased
        :param datetime.datetime localized_start_time: The localized start.
          Converted to UTC for insert.
        :param datetime.datetime localized_end_time: The localized end
        :param str local_tz: The timezone in which the event was created.
        :param str notes: Any notes written by the scheduling user.
        :param bool skip_commit: An optional flag used to indicate if we want
        to create the object and add to DB delta, but not commit it just yet.
        Used mostly for facade methods which roll back if the event doesn't
        fully complete (transaction doesn't finalize, &c.).
        :rtype: EventTable
        :return: The newly created event.
        """
        start_time = pytz.timezone(local_tz).localize(
            dateutil.parser.parse(localized_start_time))
        end_time = pytz.timezone(local_tz).localize(
            dateutil.parser.parse(localized_end_time))

        start_time = start_time.astimezone(pytz.timezone('UTC'))
        end_time = end_time.astimezone(pytz.timezone('UTC'))

        self._assert_not_in_past(start_time, end_time)

        self._assert_schedule_exists(start_time, end_time, scheduled_user_id)

        self._assert_valid_duration(start_time, end_time)

        new_event = Event(start_time, end_time, scheduling_user_id,
                          scheduled_user_id, notes)

        exec_and_commit(db.session.add, new_event, skip_commit=skip_commit)

        return new_event
Example #8
0
    def eradicate_event(self, event_public_id):
        """
        Handles event rollbacks in case the payment fails.

        :param str event_public_id: The event we're wanting to delete
        :rtype: EventTable
        :return: The event which was eradicated.
        """
        found_event = db.session.query(Event).filter_by(
            public_id=event_public_id).first()

        if found_event is None:
            raise DAOException('Failed to delete event. Event does not exist.')

        exec_and_commit(db.session.delete, found_event)

        return found_event
    def create_subscription(self, bt_sub_id, user_id, *, plan_id='Basic0x01',
                            skip_commit=False):
        """
        Handles creating a subscription record in the db.


        :param str bt_sub_id: The unique identifier generated by braintree
        when subscriptions are created.
        :param str user_id: The customer's UUID.
        :param str plan_id: The plan we want to subscribe to.
        :param bool skip_commit: Handles skipping the commit to create a pseudo
        transaction.
        :rtype: SubscriptionTable
        :return: The newly created subscription
        """
        new_subscription = Subscription(
            self.customer_dao.get_customer_by_user_id(user_id).public_id,
            user_id,
            bt_sub_id,
            plan_id=plan_id,
        )

        try:
            exec_and_commit(
                db.session.add,
                new_subscription,
                skip_commit=skip_commit,
            )
        except Exception as e:
            logging.critical(
                'Failed to create a new subscription in our database: {0} with'
                'exception of: {1}'.format(
                    new_subscription,
                    e,
                )
            )
            raise DAOException(
                'Failed to create new subscription.'
            )

        return new_subscription
    def push_to_queue(self, email_to, email_from, subject, body):
        """
        Handles adding a new email to the mail queue.

        :param str email_to: The email we'll be mailing.
        :param str email_from: The origin email.
        :param str subject: The subject line of the email.
        :param str body: The body of the email message.
        :rtype: EmailQueueTable
        :return: The newly pushed email.
        """
        new_email = Email(
            email_to,
            email_from,
            subject,
            body
        )

        exec_and_commit(db.session.add, new_email)

        return new_email
    def insert_new_transaction(self,
                               submerchant,
                               amount,
                               service_fee,
                               event,
                               *,
                               skip_commit=False):
        """
        Handles logging a new transaction

        :param SubmerchantTable submerchant:
        :param Float amount: The total amount for booked time.
        :param Float service_fee: The service fee being added onto the
        transaction.
        :param EventTable event: The event that is being booked.
        :param bool skip_commit: Tells if we should add, but not commit yet.
        :rtype: PaymentTable
        :return: The newly created payment row.
        """
        new_payment = PaymentTable(
            amount,
            service_fee,
            submerchant,
            event,
        )

        try:
            exec_and_commit(
                db.session.add,
                new_payment,
                skip_commit=skip_commit,
            )
        except Exception as e:
            logging.critical(
                'Failed to log payment {0} with exception of {1}'.format(
                    new_payment, e))

        return new_payment