Skip to content
This repository was archived by the owner on May 23, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules/


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# check-mate
app to check students' balance


## development
make sure you have `yarn` installed globally
run `yarn install`
run `npm run api` to start the api
...
56 changes: 56 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import urllib.request
import urllib.parse
import urllib.error
import ast
import requests

# Important Note:
# the functions here serve to use different APIs
# to change which function is used, navigate to main.py and change the function there
# I (Lacayo) will be using login instead of mock for my GUI until mock is up and running

api_url = "http://localhost:3000/api/"

def get_credit_with_login(username, password):
# function using NCAI login to get credit_student and credit_family

api = 'http://db.nca.edu.ni/api/api_ewapp.php?'
data = {
'mode': 'student',
'query': 'login',
'username': username,
'password': password,
}
url = api + urllib.parse.urlencode(data)
# data must be encoded for url to function properly

try:
with urllib.request.urlopen(url) as response:
page_raw = response.read() # get page source; but it's encoded
page_str = page_raw.decode() # decode the source; now a string

if 'null' in page_str:
return "Error with login, please try again."
# when there's a login error, nulls will be present
# we can check a login error by checking for nulls
else:
page_dict = ast.literal_eval(page_str) # evaluate the page str; now it's a python dictionary
credit_student = page_dict['credit_student'] # get value of key 'credit_student'
credit_family = page_dict['credit_family'] # get value of key 'credit_family'
return "Student Credit: ${} \nFamily Credit: ${}".format(credit_student, credit_family)

except urllib.error.URLError:
return 'No internet connection.'

# gets an id as param
# returns a dictionary with name, balance, id, and picture in base64
def get_data(id):
try:
response = requests.get(api_url + id).json()
if len(response.keys()) != 0:
return response
else:
return {"id not found": True}
except requests.exceptions.RequestException as e:
print(e)
return {}
16 changes: 16 additions & 0 deletions db.json

Large diffs are not rendered by default.

73 changes: 29 additions & 44 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import urllib.request
import urllib.parse
import ast
import tkinter as tk


def get_student_credit(username, password):
# function that takes the NCA username and password, and returns the balance

api = 'http://db.nca.edu.ni/api/api_ewapp.php?'
data = {
'mode': 'student',
'query': 'login',
'username': username,
'password': password,
}
url = api + urllib.parse.urlencode(data)
# data must be encoded for url to function properly

with urllib.request.urlopen(url) as response:
page_raw = response.read() # get page source; but it's encoded
page_str = page_raw.decode() # decode the source; now a string

if 'null' in page_str:
return "Error with login, please try again."
# when there's a login error, nulls will be present
# we can check a login error by checking for nulls
else:
page_dict = ast.literal_eval(page_str) # evaluate the page str; now it's a python dictionary
credit_student_balance = page_dict['credit_student'] # get value of key 'credit_student'
return "Student Credit: ${} \n".format(credit_student_balance) # return the balance
import api


class Application(tk.Frame): # for there's only one frame, so this will be the main one
Expand All @@ -38,26 +9,40 @@ def __init__(self, master=None):
self.init_window() # after initial setup; we now setup the window and its parts

def init_window(self):
def display_credit(): # use our get_student_credit function and output it to the text1 Tk variable
def display_credit(): # use our get_student_credit function and output it to the balance Tk variable
try:
output = get_student_credit(entry_username.get(), entry_password.get())
except SyntaxError: # If the entries are empty and you use get(), returns SyntaxError
output = api.get_credit_with_login(entry_username.get(), entry_password.get())
except SyntaxError:
# This happens when the entries are empty
output = 'Please enter username and password.'
text1.config(text=output)

entry_username = tk.Entry(self) # creating the Tk widgets
entry_password = tk.Entry(self, show='*')
text1 = tk.Message(self, width='200')
button_get_credit = tk.Button(self, text="Fetch Credit", command=display_credit)
balance.config(text=output)

def display_data():
json = api.get_data( entry_id.get() )
if len(json.keys()) != 0:
try:
text = json["name"] + ": " + str(json["balance"])
except KeyError as e:
print(e, "key not found in response")
text = "Wrong ID. Try again"
else:
text = "Network Error"

balance.config(text=text)

# creating the Tk widgets
entry_id = tk.Entry(self)
balance = tk.Message(self, width='200')
button_get_credit = tk.Button(self, text="Fetch Credit", command=display_data)

self.master.title("Student Credit")
self.pack(fill='both', expand=1) # displaying the Tk widgets with pack()
entry_username.pack()
entry_password.pack()
# displaying the Tk widgets with pack()
self.pack(fill='both', expand=1)
entry_id.pack()
button_get_credit.pack()
text1.pack()
balance.pack()

root = tk.Tk()
root.geometry("250x100") # sets window size
root.geometry("350x200") # sets window size
app = Application(root)
root.mainloop() # this kicks off tkinter window
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "check-mate",
"version": "1.0.0",
"repository": "https://github.com/ncai-developers/check-mate.git",
"author": "ncai-developers",
"license": "MIT",
"devDependencies": {
"json-server": "^0.9.6"
},
"scripts": {
"api": "json-server db.json --watch"
}
}
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==3.0.7
requests-mock==1.3.0
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.13.0
urllib3==1.13.1
19 changes: 19 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import api
import requests_mock

def test_get_data():
user_id = "017770"
expected_json = {
"name":"",
"balance":20,
"photo":"",
"id":"017770"
}
with requests_mock.Mocker() as mock:
# mock get request to api url
mock.get(api.api_url + user_id, json=expected_json)
json = api.get_data(user_id)
assert type(json["name"]) is str
assert type(json["balance"]) is int
assert type(json["photo"]) is str
assert type(json["id"]) is str
Loading