Example #1
0
def add_new_client():
    new_client = Client.Client()
    new_client.fill_from_dict(request.get_json())
    clients_database = ClientsDb.ClientsDb()
    clients_database.addNew(new_client)
    new_client.id = str(new_client._id)
    del new_client._id
    return jsonify(new_client.__dict__), 202
Example #2
0
    def register(self, client, clientConnection):

        try:
            client = Client(fullname=client['fullname'],
                            username=client['username'],
                            password=client['password'],
                            color=client['color'],
                            email=client['email'],
                            gender=client['gender'],
                            status=client['status'])
            client.ClientConnection = clientConnection
            return (True,
                    "Registered Successfully!, Hello: " + client.username,
                    client)

        except dberrors.DuplicateEntryError:
            return (False, "Usernaame or Email is Already exist.", None)
Example #3
0
def main():
   c = Client('127.0.0.1', 8820)
   c.send_textfile('text.txt')
Example #4
0
def aggregate_data(contents):
    """Function to aggregate sales data and write the results to a file called output.csv
    
        Parameters:
        contents (String): data to be aggregated
 
       """
    # create result string that will be outputed to the CSV file
    writeToOutput("Client,Product,Month,total amount,count\n", "output.csv")

    # Used to check if the first line with column names hasn't be read yet
    firstLineRead = False
    #dictionary of all unique clients in CSV file. The key is the client name and the value is the associated client object
    clients = {}
    for line in contents:
        if (not firstLineRead):
            #skip first line of file and continue to the next one
            firstLineRead = True
            continue
        #remove single quotes and turn comma separated string into list
        line = line.replace("'", "")
        lineArray = line.split(",")

        #client name is on the second index of the list (index 1 on zero rated indicice)
        if lineArray[1] in clients:
            #if client name is already in local clients dictionary, get the client object from the dictionary
            client = clients[lineArray[1]]
        else:
            #client is not in dictionary, so create a new Client object with the data (client name) from the CSV file
            client = Client.Client(lineArray[1])

        #create Product and Date objects with data from CSV file
        product = Product.Product(lineArray[3])
        date = Date.Date(lineArray[2].split("-")[0],
                         lineArray[2].split("-")[1],
                         lineArray[2].split("-")[2])

        #get the amount an currency
        amountString = float(lineArray[4])
        amountValue = 0
        currency = "ZAR"
        if (str(amountString).replace('.', '', 1).isdigit()):
            amountValue = amountString
        else:
            for i, character in enumerate(amountString):
                if character.isdigit():
                    currency = amountString[0:i]
                    amountValue = amountString[i:]
                    break

        amount = Amount.Amount(amountValue, currency)
        #create sale with newly created product and date objects. Get sale amount from CSV file and store as a float in sale object
        sale = Sale.Sale(product, date, amount)

        #associate sale with client to whom the sale was made
        client.buy(sale)
        #update client object in client dictionary
        clients[client.get_name()] = client

    #for all clients, aggregate the sales by the tuple (client, product, month)
    for client in clients:
        #get all products bought by this client
        product_names = clients[client].get_unique_products()
        #get all months on which a sale was made to the client
        sale_months = clients[client].get_unique_months()

        #for each product, get the aggregates for each month
        for product_name in product_names:
            for sale_month in sale_months:
                #write result to file
                writeToOutput(
                    clients[client].aggregate_sale_by_month(
                        product_name, sale_month), "output.csv")
    print(
        "Data successfully aggregated and saved to output.csv in the current directory"
    )
Example #5
0
 def get_all(self):
     result = []
     resource = mongo_client.db.clients.find({})
     for each_client in resource:
         result.append(Client.Client(each_client))
     return result
Example #6
0
 def get_client_for_attempt(self, id):
     resource = mongo_client.db.clients.find_one({"_id": id})
     if resource is not None:
         return  Client.Client(resource)
     return None
Example #7
0
 def get_client_by_id(self, id):
     resource = mongo_client.db.clients.find_one({"_id": ObjectId(id)})
     if resource is not None:
         return  Client.Client(resource)
     return None