Skip to content

Commit

Permalink
We are two commented lines away from ordering pizza!
Browse files Browse the repository at this point in the history
  • Loading branch information
gareth authored and gareth committed Jan 9, 2011
1 parent b7ff894 commit 6e6b52a
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 63 deletions.
23 changes: 22 additions & 1 deletion android/src/android/pizzabutton/APIWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

import android.util.Log;

/**
* Wraps the platform calls. All these functions handle formatting the HTTP calls, and then
* pass them off to HTTPHelper to make the actual call
*
* @author gareth
*
*/
public class APIWrapper {
public static final String apiURL = "http://10.0.2.2:8080/"; // hack for the android simulator
public static final String apiURL = "http://thepizzabutton.appspot.com/"; // hack for the android simulator
public static final String orderPizzaEndpoint = apiURL + "order_pizza";
public static final String pizzaListEndpoint = apiURL + "pizza_list";
public static final String addUserEndpoint = apiURL + "add_user";
Expand All @@ -19,6 +26,12 @@ public static String pizza_list() {
return response;
}

/**
* Orders a pizza using the user's settings
*
* @param userId the user id of the user ordering pizza
* @return the response from the HTTP call
*/
public static String order_pizza(String userId) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("userId", userId));
Expand All @@ -28,6 +41,14 @@ public static String order_pizza(String userId) {
return response;
}

/**
*
* @param userJSON a properly formatted JSON object containing all the fields
* necessary to create a new user object in our database. See the API spec for
* more information on these fields
*
* @return the response from the HTTP call
*/
public static String add_user(String userJSON) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("user", userJSON));
Expand Down
7 changes: 7 additions & 0 deletions android/src/android/pizzabutton/Address.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package android.pizzabutton;

/**
* Abstracts an Address object in our database
*
* @author gareth
*
*/
public class Address {
private String number = "";
private String street = "";
Expand All @@ -18,6 +24,7 @@ public Address(String number, String street, String city,
this.province = province;
}

// Getters and Setters for the Address fields
public String getNumber() { return number; }
public String getStreet() { return street; }
public String getCity() { return city; }
Expand Down
16 changes: 15 additions & 1 deletion android/src/android/pizzabutton/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import org.json.JSONException;
import org.json.JSONObject;

/**
* Abstracts a User object from the database
*
* @author gareth
*
*/
public class User {
private String name = "";
private String phoneNumber = "";
Expand All @@ -19,6 +25,11 @@ public User(String name, String phoneNumber, String email, Address address) {
this.address = address;
}

/**
* Format the user's data into a json object we can pass to the platform
*
* @return a properly formatted JSON object containing the user's data
*/
public String getJSON() {
JSONObject jobj = new JSONObject();
try {
Expand All @@ -31,7 +42,8 @@ public String getJSON() {
jobj.put("city", this.address.getCity());
jobj.put("province", this.address.getProvince());
jobj.put("postalCode", this.address.getPostalCode());


// Hardcoded order for now
jobj.put("quantity", "1");
jobj.put("pizzaSize", "1");
jobj.put("pizzaId", "1");
Expand All @@ -44,6 +56,8 @@ public String getJSON() {
return jobj.toString();
}

// Getter's and setters for all the User's fields

public void setUserId(String userId) { this.userId = userId; }
public void setName(String name) { this.name = name; }
public void setEmail(String email) {this.email = email; }
Expand Down
18 changes: 11 additions & 7 deletions android/src/android/pizzabutton/orderpizza.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@

import java.util.List;
import java.util.ArrayList;
import java.util.TimerTask;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.*;

import android.pizzabutton.HttpHelper;


public class orderpizza extends Activity {
public static final String PREFS_NAME = "PizzaPrefs";
private boolean hasUser = false;
private User theUser = new User();

// TODO: Make this accessible to all classes in the app.
private static final String TAG = "PizzaButton";
private static final String TAG = "TPB";

public void ToastMsg(String message) {
Toast msg = Toast.makeText(
Expand All @@ -49,9 +51,9 @@ public void onCreate(Bundle savedInstanceState) {
SharedPreferences.Editor editor = settings.edit();

// clear the preferences for testing purposes
//editor.putBoolean("hasUser", false);
//editor.putString("userId", "");
//editor.commit();
editor.putBoolean("hasUser", false);
editor.putString("userId", "");
editor.commit();

loadPage(R.layout.main);

Expand Down Expand Up @@ -161,6 +163,8 @@ private void orderPizza() {
Log.v("TPB", "Ordering pizza!");
String response = APIWrapper.order_pizza(theUser.getUserId());
Log.v("TPB", response);
// TODO: actually check if it worked
ToastMsg("Fuck yeah, Pizza is on its way!");
}

private void createUser() {
Expand Down Expand Up @@ -196,7 +200,7 @@ private JSONArray getPizzaTypes() {
Log.v(TAG, "Error parsing JSON: "+e);
return null;
}
}
}

///////

Expand Down Expand Up @@ -225,5 +229,5 @@ private String getTextById(int id) {
private void bindButtonToListener(int id, OnClickListener listener) {
Button button = (Button)findViewById(id);
button.setOnClickListener(listener);
}
}
}
12 changes: 12 additions & 0 deletions web/model_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
from model import Order
from model import User


def getUser(userId):
# TODO: error handling in case of a bad id
user = db.get(userId.strip())
print user
return user

def getAddress(addressId):
address = db.get(addressId.strip())
print address
return address

# Takes in a user json string and adds a user, address and order to the database
# Returns a user key
def insert_user(user_json):
Expand Down
3 changes: 2 additions & 1 deletion web/order_pizza.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class OrderPizzaHandler(webapp.RequestHandler):
def post(self):
#TODO: hook this up with the mechanize code
userId = self.request.get('userId')
self.response.out.write("Ordering pizza for user: " + userId)
#self.response.out.write("Ordering pizza for user: " + userId)
getPizza(self, userId)

def main():
application = webapp.WSGIApplication([('/order_pizza',OrderPizzaHandler)], debug=True)
Expand Down
124 changes: 71 additions & 53 deletions web/robot.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,92 @@
# robot.py - a robot that brings you pizza
# Copyright 2010 - 7cubed
# Copyright 2010 - present, 7cubed
#
# @author gmacleod
# @author bemacdo, gilbertleung, gmacleod

import mechanize

def getPizza():
return "Ordering a Pizza, I swear!"
from model import User
from model_wrapper import getUser
from model_wrapper import getAddress

# Gilbert & Barbara's code

#br = mechanize.Browser()
#br.set_handle_equiv(False)
#br.set_handle_robots(False)
#br.open("https://order.pizzanova.com/fcgi-bin/Weborder.pl")
#br.select_form(nr=0)

#br["email"] = "7cubedproject@gmail.com"
#br["password"] = "velocity"
#br.submit()
#self.response.out.write(br.response().get_data())
import mechanize

#br.select_form(nr=0)
#br.open(br.click(nr=0)) #delivery
def getPizza(someObject, userId):

#br.select_form(nr=0)
#br.open(br.click(nr=1)) #re-enter location
#self.response.out.write(br.response().get_data())
user = getUser(userId)
address = user.addressId

#br.select_form(nr=0)
#br["postalcode"] = "N2L 2B5"
#br.find_control("LOCATIONTYPE").get("House").selected = True
#br.submit()
#self.response.out.write(br.response().get_data())
# Gilbert & Barbara's code

#br.select_form(nr=0)
#br.submit()
br = mechanize.Browser()
br.set_handle_equiv(False)
br.set_handle_robots(False)
br.open("https://order.pizzanova.com/fcgi-bin/Weborder.pl")
br.select_form(nr=0)

br["email"] = "7cubedproject@gmail.com"
br["password"] = "velocity"
br.submit()
#someObject.response.out.write("Output: " + br.response().get_data())
#print "Output: " + br.response().get_data()

br.select_form(nr=0)
br.open(br.click(nr=0)) #delivery

br.select_form(nr=0)
br.open(br.click(nr=1)) #re-enter location
#someObject.response.out.write(br.response().get_data())

print "Postal Code: " + address.postalCode
br.select_form(nr=0)
br["postalcode"] = address.postalCode
# TODO: make housing type customizable
br.find_control("LOCATIONTYPE").get("House").selected = True
br.submit()
#someObject.response.out.write(br.response().get_data())

br.select_form(nr=0)
br.submit()
#someObject.response.out.write(br.response().get_data())

print "Number: " + address.number
print "Phone number: " + user.phoneNumber

br.select_form(nr=0)
br["streetno"] = address.number
br["phoneno"] = user.phoneNumber
br.submit()
#someObject.response.out.write(br.response().get_data())

# TODO: Add checking that the street name it got is correct
br.select_form(nr=0)
br.open(br.click(nr=0)) # deliver (instead of re-enter)
#self.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br["streetno"] = "17"
#br["phoneno"] = "2268682698"
#br.submit()
#self.response.out.write(br.response().get_data())
br.select_form(nr=0)
br.open(br.click(nr=0)) # order a pizza;
#someObject.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br.open(br.click(nr=0)) # deliver (instead of re-enter)
# TODO: make pizza order customizable
br.select_form(nr=0)
br.find_control("PIZZASIZE").get("Small").selected = True
br.find_control("CODE03").get("On Whole Pizza").selected = True
br.open(br.click(type="submit", nr=0)) # add to order
#self.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br.open(br.click(nr=0)) # order a pizza;
#self.response.out.write(br.response().get_data())
br.select_form(nr=0)
br.open(br.click(type="submit", nr=6)) #proceed to chekout
# someObject.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br.find_control("PIZZASIZE").get("Small").selected = True
#br.find_control("CODE03").get("On Whole Pizza").selected = True
#br.open(br.click(type="submit", nr=0)) # add to order
#self.response.out.write(br.response().get_data())
br.select_form(nr=0)
br.open(br.click(type="submit", nr=1)) #proceed to payment
#someObject.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br.open(br.click(type="submit", nr=6)) #proceed to chekout
#self.response.out.write(br.response().get_data())
#br.open(br.click(nr=0)) # by cash
#someObject.response.out.write(br.response().get_data())

#br.select_form(nr=0)
#br.open(br.click(type="submit", nr=1)) #proceed to payment
#self.response.out.write(br.response().get_data())
someObject.response.out.write("SUCCESS!")

#br.select_form(nr=0)
#br.open(br.click(nr=0)) # by cash
#self.response.out.write(br.response().get_data())
#DO NOT UNCOMMENT UNLESS YOU WANT TO ACTUALLY ORDER A PIZZA

#br.select_form(nr=0)
#br.open(br.click(nr=0)) # submit

0 comments on commit 6e6b52a

Please sign in to comment.