예제 #1
0
    def read_all(self):

        result = list()

        companies = Company.select()

        for company in companies:

            result.append(company)

        return result
예제 #2
0
    def get(self):
        response = Response()
        response.status_code = 200
        output = StringIO.StringIO()
        workbook = xlsxwriter.Workbook(output)
        worksheet = workbook.add_worksheet('orders')
        bold = workbook.add_format({'bold': 1})
        head = [
            "address",
            "assigned_date",
            "due_date",
            "company",
            "research_user",
            "data_user",
            "client_code",
            "kind",
            "research_type",
        ]

        worksheet.write_row('A1', head, bold)
        try:
            order = Order.select().get()
            try:
                company = Company.select().where(
                    Company.id == order.company).get()
            except:
                company = None
            try:
                research_user = User.select().where(
                    User.id == order.research_user).get()
            except:
                research_user = None

            try:
                data_user = User.select().where(
                    User.id == order.data_user).get()
            except:
                research_user = None

            try:
                client_code = ClientCode.select().where(
                    ClientCode.id == order.client_code).get()
            except:
                client_code = None

            try:
                kind = OrderType.select().where(
                    OrderType.id == order.kind).get()
            except:
                kind = None
            try:
                reseach_type = ResearchType.select().where(
                    ResearchType.id == order.research_type).get()
            except:
                reseach_type = None

            data = [
                order.address,
                str(order.assigned_date),
                str(order.due_date),
                company.name if company else "",
                research_user.username if research_user else "",
                data_user.username if data_user else "",
                client_code.code if client_code else "",
                kind.order_type if kind else "",
                reseach_type.research_type if reseach_type else "",
            ]
            worksheet.write_row(1, 0, data)
        except:
            None

        workbook.close()
        output = make_response(output.getvalue())
        output.headers[
            "Content-Disposition"] = "attachment; filename=sample-upload.xlsx"
        output.headers["Content-type"] = "text/xlsx"

        return output
예제 #3
0
    def read(self, _id):

        company = Company.select().where(Company.id==_id).get()

        return company
예제 #4
0
    def post(self):
        dbo = app.order_dbo

        file = request.files['xlsx']
        workbook = load_workbook(file, read_only=True)

        for sheet in workbook.sheetnames:
            row_idx = -1
            for row in workbook[sheet]:
                row_idx += 1
                if row_idx == 0:
                    continue
                else:
                    order_data = []

                    col_idx = 0
                    for col in row:
                        if col_idx <= 8:
                            if type(col.value) is unicode:
                                the_col_data = str(col.value)
                            else:
                                the_col_data = col.value
                            print(type(the_col_data))
                            order_data.append(the_col_data or None)
                        else:
                            break

                        col_idx += 1

                    print(order_data)
                    if len(
                            order_data
                    ) == 8:  # The last column gets ignored, if there's no data
                        order_data.append(None)
                    if order_data and len(order_data) == 9:
                        new_order_address = order_data[0]
                        print(new_order_address)
                        try:
                            existing_order = Order.select().where(
                                Order.address == new_order_address).get()
                        except:
                            existing_order = None

                        if not existing_order:
                            if order_data[8]:
                                research_type = ResearchType.select().where(
                                    ResearchType.research_type ==
                                    order_data[8]).get()
                            else:
                                research_type = None

                            if order_data[4]:
                                research_user = User.select().where(
                                    User.username == order_data[4]).get()
                                state = "RESEARCH"
                            else:

                                state = "DATA_ENTRY"
                                research_user = None

                            order_state = OrderState.select().where(
                                OrderState.state == state).get()

                            # due_date = datetime.strptime(str(order_data[2]), "%m-%d-%Y").date()
                            # assigned_date = datetime.strptime(str(order_data[1]), "%m-%d-%Y").date()
                            due_date = str(order_data[2])
                            assigned_date = str(order_data[1])
                            try:
                                full_address = geo_address(new_order_address)
                            except:
                                full_address = None

                            try:
                                _lat, _long = geo_coordinates(
                                    new_order_address)
                            except:
                                _lat = None
                                _long = None

                            if order_data[3]:
                                company = Company.select().where(
                                    Company.name == order_data[3]).get()
                            else:
                                company = None

                            if order_data[5]:
                                data_user = User.select().where(
                                    User.username == order_data[5]).get()
                                print(data_user)
                            else:
                                data_user = None

                            if order_data[6]:
                                client_code = ClientCode.select().where(
                                    ClientCode.code == order_data[6]).get()
                            else:
                                client_code = None

                            if order_data[7]:
                                kind = OrderType.select().where(
                                    OrderType.order_type ==
                                    order_data[7]).get()
                            else:
                                kind = None

                            data = {
                                "address":
                                new_order_address,
                                "full_address":
                                full_address
                                if full_address else new_order_address,
                                "latitude":
                                _lat if _lat else 0,
                                "longitude":
                                _long if _long else 0,
                                "data_user":
                                data_user.id if data_user else None,
                                "research_type":
                                research_type.id if research_type else None,
                                "research_user":
                                research_user.id if research_user else None,
                                "company":
                                company.id if company else None,
                                "due_date":
                                due_date,
                                "assigned_date":
                                assigned_date,
                                "client_code":
                                client_code.id if client_code else None,
                                "kind":
                                kind.id if kind else None,
                                "state":
                                order_state
                            }

                            order = dbo.create(**data)

                            if research_user:
                                send_mail_template(
                                    "ASSIGN_RESEARCH",
                                    order,
                                    username=research_user.username,
                                    from_email="*****@*****.**",
                                    to_emails=[research_user.email])
                            if data_user:
                                send_mail_template("ASSIGN_DATA",
                                                   order,
                                                   username=data_user.username,
                                                   from_email="*****@*****.**",
                                                   to_emails=[data_user.email])

                            order.save()

                    # else:
                    #     return BaseException("Invalid File")

        return True