コード例 #1
0
class VendorTests(CRUDTest):
    """Test cases for the Vendors class"""
    def __init__(self):
        """Constructor"""
        CRUDTest.__init__(self)
        self.vendors = None
        self.last_id = None
        self.vendors = None

    def set_up(self, mysql_x, user=None, passwd=None):
        """Setup the test cases"""
        self.mygarage = self.begin(mysql_x, "Vendors", user, passwd)
        self.vendors = Vendors(self.mygarage)

    def create(self):
        """Run Create test case"""
        print("\nCRUD: Create test case")
        vendor_data = {
            "name": "ACME Bolt Company",
            "url": "www.acme.org",
            "sources": "looney toons"
        }
        self.vendors.create(vendor_data)
        self.last_id = self.vendors.get_last_docid()
        print("\tLast insert id = {0}".format(self.last_id))

    def read_all(self):
        """Run Read(all) test case"""
        print("\nCRUD: Read (all) test case")
        docs = self.vendors.read()
        self.show_docs(docs, 5)

    def read_one(self):
        """Run Read(record) test case"""
        print("\nCRUD: Read (doc) test case")
        docs = self.vendors.read(self.last_id)
        self.show_docs(docs, 1)

    def update(self):
        """Run Update test case"""
        print("\nCRUD: Update test case")
        vendor_data = {
            "_id": self.last_id,
            "name": "ACME Nut Company",
            "url": "www.weesayso.co",
        }
        self.vendors.update(vendor_data)

    def delete(self):
        """Run Delete test case"""
        print("\nCRUD: Delete test case")
        self.vendors.delete(self.last_id)
        docs = self.vendors.read(self.last_id)
        if not docs:
            print("\tNot found (deleted).")
コード例 #2
0
def vendor(vendor_id=None):
    """Manage vendor CRUD operations."""
    vendor_collection = Vendors(mygarage)
    form = VendorForm()
    # Get data from the form if present
    form_vendorid = form.vendorid.data
    form_name = form.name.data
    form_url = form.url.data
    form_sources = form.sources.data
    # If the route with the variable is called, change the create button to update
    # then populate the form with the data from the row in the table. Otherwise,
    # remove the delete button because this will be a new data item.
    if vendor_id:
        form.create_button.label.text = "Update"
        # Here, we get the data and populate the form
        data = vendor_collection.read(vendor_id)
        if data == []:
            flash("Vendor not found!")
        data_dict = dict(data[0])
        form.vendorid.data = data_dict.get("_id", None)
        form.name.data = data_dict.get("name", None)
        form.url.data = data_dict.get("url", None)
        form.sources.data = data_dict.get("sources", None)
    else:
        del form.del_button
    if request.method == 'POST':
        # First, determine if we must create, update, or delete when form posts.
        operation = "Create"
        if form.close_button.data:
            return redirect('/list/vendors')
        if form.create_button.data:
            if form.create_button.label.text == "Update":
                operation = "Update"
        if form.del_button and form.del_button.data:
            operation = "Delete"
        if form.validate_on_submit():
            # Get the data from the form here
            if operation == "Create":
                try:
                    vendor_data = {
                        "name": form_name,
                        "url": form_url,
                        "sources": form_sources,
                    }
                    res = vendor_collection.create(vendor_data)
                    if res[0]:
                        flash("Added.")
                    else:
                        flash("Cannot add vendor: {0}".format(res[1]))
                    return redirect('/list/vendors')
                except Exception as err:
                    flash(err)
            elif operation == "Update":
                try:
                    vendor_data = {
                        "_id": form.vendorid.data,
                        "name": form_name,
                        "url": form_url,
                        "sources": form_sources,
                    }
                    res = vendor_collection.update(vendor_data)
                    if res[0]:
                        flash("Updated.")
                    else:
                        flash("Cannot update vendor: {0}".format(res[1]))
                    return redirect('/list/vendors')
                except Exception as err:
                    flash(err)
            else:
                try:
                    if not mygarage.vendor_in_use(form_vendorid):
                        res = vendor_collection.delete(form_vendorid)
                        if res[0]:
                            flash("Deleted.")
                        else:
                            flash("Cannot delete vendor: {0}".format(res[1]))
                    else:
                        flash("Vendor {0} in use. Cannot delete.".format(
                            form_name))
                    return redirect('/list/vendors')
                except Exception as err:
                    flash(err)
        else:
            flash_errors(form)
    return render_template("vendor.html", form=form)