def list(self, data, token): offset = data['offset'] limit = data['max'] users = sess.query(User).filter(User.token==token).offset(offset).limit(limit) returns = [] for user in users: customfields = sess.query(CustomField).filter(CustomField.user_id==user.id).all() returns.append(\ {\ "firstname": user.firstname, "lastname": user.lastname, "email": user.email, "avatar_url": user.avatar_url, "password": user.password.decode(encoding='UTF-8'), "master" : user.master, "id": user.id, "created": user.created, "custom_fields":\ [{"key": field.key, "value": field.value} for field in customfields] } ) return {'status' : 200, 'users' : returns}
def get(self, data, token): if data is None: return throw_error(400, 'Data is null') email = data['email'] user = sess.query(User).filter(User.email==email).first() if user is None: return throw_error(400, 'No such user') returns = [] customfields = sess.query(CustomField).filter(CustomField.user_id==user.id).all() returns.append(\ {\ "firstname": user.firstname, "lastname": user.lastname, "email": user.email, "avatar_url": user.avatar_url, "password": user.password.decode(encoding='UTF-8'), "master" : user.master, "id": user.id, "created": user.created, "custom_fields":\ [{"key": field.key, "value": field.value} for field in customfields] } ) return {'status' : 200, 'users' : returns, "errors" : None}
def index(): if request.method == 'POST': # refresh dates return redirect(url_for('index', from_date=request.form['from_date'], to_date=request.form['to_date'])) else: if request.args.get('from_date'): from_date=datetime.strptime(request.args.get('from_date'), "%d.%m.%Y") else : from_date = date.today() + timedelta( weeks = -1 ) if request.args.get('to_date'): to_date=datetime.strptime(request.args.get('to_date'), "%d.%m.%Y") else: to_date = date.today() # fetch values images = sess.query(Image).filter(Image.check_time>=from_date, Image.check_time<=to_date+timedelta(days=1), Image.result!='').order_by(Image.check_time).all() data = [] if len(images)>0: prev_check = None first = images[0] last = images[-1] # calculate full time interval full_interval = last.check_time-first.check_time # define min time step min_step = full_interval/200 # calculate full average average_consumption = round((float(last.result)-float(first.result))/100/full_interval.total_seconds() * 60 * 60 * 24,2) for image in images: check = {'time':image.check_time, 'value':float(image.result)/100} if None != prev_check: # caclulate time delta with previous check td = check['time']-prev_check['time'] # skip very often checks if td<min_step: continue # caclulate tic average consumption check['diff'] = round(float(check['value']-prev_check['value'])/td.total_seconds() * 60 * 60 * 24, 2) # set diff value of first check equal to diff value of second check if prev_check['diff']==None: prev_check['diff'] = check['diff'] else: # first check point check['diff'] = None # push chekpoint to series data.append(check) prev_check = check else: average_consumption = 0; unrecognized_images_cnt = sess.query(Image).filter_by(result='').count() return render_template('index.html', from_date=from_date, to_date=to_date, series=data, average_consumption=average_consumption, unrecognized_images_cnt=unrecognized_images_cnt)
def login_blueprint(): if session.get('user') != None: return redirect('/') form = MyForm(csrf_enabled=False) msg = '' if form.validate_on_submit(): fetched_user = sess.query(User).filter(User.email==form.email.data).first() if fetched_user == None: msg = 'No such user' else: if fetched_user.password != form.password.data: session.clear() msg = 'Wrong password' else: if fetched_user.password == form.password.data: session['user'] = fetched_user.id session['loggedin'] = True if get_user(session.get('user')).admin == 1: return redirect('/admin') return redirect('/') return render_template('login.html', form=form, msg=msg)
def delete(self, data, token): user = sess.query(User).filter(User.id==data['id']).first() if user is not None: if user.token != token: return throw_error(400, 'Bad token') sess.delete(user) sess.commit() return throw_error(200, 'null') else: return throw_error(400, 'No such user')
def register_blueprint(): form = MyForm(csrf_enabled=False) msg = '' if form.validate_on_submit(): if form.password.data != form.password_confirm.data: msg = 'Passwords does not match' else: if sess.query(User).filter(User.email==form.email.data).count() > 0: msg = 'This email is already registered' else: user = User(email=form.email.data, password=form.password.data) sess.add(user) sess.commit() msg = 'Thank you for registering' return render_template('register.html', form=form, msg=msg)
def recognize(): unrecognized_images = sess.query(Image).filter_by( result='').limit(100).all() return render_template('recognize.html', images=unrecognized_images)
import re from gdrive import downloadImageFromGDrive from models import Image, sess, mylogger if __name__ == '__main__': # fetch unrecognized images unrecognized_images = sess.query(Image).filter_by(result='').all() for image in unrecognized_images: mylogger.info("Process %s" % image.file_name) # try to recognize digits using new training data m = re.search('id=(.*)', image.img_link) image.img = downloadImageFromGDrive(image.download_url, file_id=m.group(1)) image.identifyDigits() sess.commit()
def recognize(): unrecognized_images = sess.query(Image).filter_by(result='').limit(100).all() return render_template('recognize.html', images=unrecognized_images)
def get_options(offset, limit): return sess.query(Option).offset(offset).limit(limit)
def get_option(key): return sess.query(Option).filter(Option.key==key).first()
''' -- Gaz counter values recognizing @author: malefic @contact: [email protected] ''' import re from gdrive import downloadImageFromGDrive from models import Image, sess, mylogger if __name__ == '__main__': # fetch unrecognized images unrecognized_images = sess.query(Image).filter_by(result='').all() for image in unrecognized_images: mylogger.info("Process %s" % image.file_name) # try to recognize digits using new training data m = re.search('id=(.*)', image.img_link) image.img = downloadImageFromGDrive(image.download_url, file_id=m.group(1)) image.identifyDigits() sess.commit()
def register(self, data, token): ids = [] if 'users' not in data: return throw_error(422, 'JSON missing users array') for user in data['users']: existing_user = sess.query(User).filter(User.email==user['email']).first() if existing_user is not None: return throw_error(202, 'User already exists') try: u = User(\ firstname=user['firstname'],\ lastname=user['lastname'],\ email=user['email'],\ avatar_url=user['avatar_url'],\ password=encrypt(user['password']),\ master=user['master'],\ token=token ) except KeyError: return throw_error(422, 'Invalid data') # Validating the password, is it equal to the confirmation password? if u.password != encrypt(user['password_confirm']): return throw_error(202, 'Passwords does not match!') # Validating each field of the user for attr, value in u.__dict__.items(): if value is '' or value is ' ' or value is None: return throw_error(422, 'Value of {attribute} is empty.'.format(attribute=attr)) # adding user to database sess.add(u) # flushing the session sess.flush() # refreshing the user object to obtain the new id sess.refresh(u) # collecting the id of the user ids.append(u.id) # Adding custom_fields if there are any if 'custom_fields' in user: print(user['custom_fields']) for field in user['custom_fields']: try: customfield = CustomField( key=field['key'], value=field['value'], user_id=u.id\ ) except (TypeError, KeyError): return throw_error(422, 'custom_fields is malformed') sess.add(customfield) # Finally, we are saving the user sess.commit() return {'status' : 201, 'ids' : ids, "errors" : None}
def get_user(id): return sess.query(User).filter(User.id==id).first()
def get_users(offset, limit): return sess.query(User).limit(limit).offset(offset)