4. Y ahora un poco de código...
# class npay used to create object npay
# and make easiest to use on further
# develop needs.
#
# hksck
from bson import json_util
import json
import hashlib
import pymongo
class npay:
def __init__(self, user, password, state = 1):
""" state arg can be :
1, a already registred useropen vis
0, a new account using the args already passed
Anyway, if state = 0, npay will check if the gived
user (user), exists.
"""
self.user = user
self.password = password
self.server = pymongo.MongoClient('mlab_uri', port)
self.db = self.server['mlab_db_name']
# paypoc is the entire db name on my server
self.db.authenticate('mlab_db_user', 'mlab_db_password')
if state == 0:
if self.db.users.find_one({"user": user}) == None:
# Create a dict structure of user and password
dict_user = {
"user": user,
"password": password,
"amount": 0
}
self.db.users.insert_one(dict_user)
else:
print "User already exist"[/size]
def get_balance(self):
x = self.db.users.find_one({"user": self.user,
"password": self.password,
})
# This is pretty unknown from me, but, avoid
# the error JSON serializable and allows
# search across the documents such as python dictionary
y = json.dumps(x, default=json_util.default)
balance = json.loads(y)
return balance['amount']
El code anterior crea un objeto del tipo npay, con el cual, podemos por el momento crear un usuario (o saber si existe cuando introducimos un usuario que ya existe sin saberlo, el programa nos lo indica), logearse y retornar el balance con el método get_balance()
.
Nada interesante quizá por el momento :silbar: Registraos en https://mlab.com (https://mlab.com) y sustituid los datos por los vuestros, cread una función instanciando el objeto npay y obtendréis esto:(https://s19.postimg.org/4fj9lextv/Captura_de_pantalla_de_2016_06_28_02_23_11.png)
5. ¿Y qué opináis...?