def renderInventoryListings(itemType='ALL',
                            status='ALL',
                            quality='ALL',
                            searchSerial=None,
                            searchModal=None):

    deviceList = models.getDevicesWithLog(itemType, status, quality)
    length = models.getDevices()

    error = None
    if 'error' in session:
        error = session['error']
        session.pop('error', None)

    return render_template("page/PageIndex_Inventory.html",
                           filter_Type=itemType,
                           filter_Status=status,
                           filter_quality=quality,
                           query=deviceList,
                           types=models.getDeviceTypes(),
                           states=models.getStates(),
                           totalItems=len(length),
                           totalSignedOut=len(
                               models.getDevicesWithLog('ALL', 'out', 'ALL')),
                           data_id=searchSerial,
                           queueModal=searchModal,
                           name=escape(getName()),
                           error=error)
def renderInventoryListings(itemType = 'ALL', status = 'ALL', quality = 'ALL', searchSerial = None, searchModal = None):
	
	deviceList = models.getDevicesWithLog(itemType, status, quality)
	length = models.getDevices()
	
	error = None
	if 'error' in session:
		error = session['error']
		session.pop('error', None)
	
	return render_template("page/PageIndex_Inventory.html",
			filter_Type = itemType,
			filter_Status = status,
			filter_quality = quality,
			
			query = deviceList,
			types = models.getDeviceTypes(),
			states = models.getStates(),
			
			totalItems = len(length),
			totalSignedOut = len(models.getDevicesWithLog('ALL', 'out', 'ALL')),
			
			data_id = searchSerial,
			queueModal = searchModal,
			
			name = escape(getName()),
			error = error
		)
def viewItem(serial):
	item = models.Device.select().where(models.Device.serialNumber == serial)
	types = models.getDeviceTypes()
	states = models.getStates()
	return render_template('viewItem.html',
			item=item,
			types=types,
			states=states,
			logoutURL=url_for('logout')
		)
def search():
    if not request.method == 'POST':
        return getIndexURL()

    searchPhrase = str(request.form['searchField'])

    if searchPhrase.startswith("scan:"):
        serial = searchPhrase[5:]
        query = models.getDeviceLog(serial)
        isOut, obj = models.getStatus(query)
        if isOut:
            modal = "signIn"
        else:
            modal = "signOut"
        return renderInventoryListings(searchSerial=serial, searchModal=modal)

    if models.isSearchUser(searchPhrase):
        logs = models.Log.select().where(
            models.Log.AuthorizerIn.contains(searchPhrase)
            | models.Log.AuthorizerOut.contains(searchPhrase)
            | models.Log.UserIn.contains(searchPhrase)
            | models.Log.UserOut.contains(searchPhrase)).order_by(
                -models.Log.DateOut)

        return render_template("page/PageUserLogs.html",
                               query=logs,
                               searchPhrase=searchPhrase)

    if len(searchPhrase) == 4 and searchPhrase.isdigit():
        searchPhrase = "LCDI-" + searchPhrase

    if (len(models.Device.select().where(
            models.Device.SerialNumber == searchPhrase)) == 1):
        return renderPage_View(searchPhrase)
    else:
        query = models.getDevicesAndLogs().where(
            models.Device.SerialNumber.contains(searchPhrase)
            | models.Device.SerialDevice.contains(searchPhrase)
            | models.Device.Type.contains(searchPhrase)
            | models.Device.Description.contains(searchPhrase)
            | models.Log.Purpose.contains(searchPhrase))
        deviceList = models.getDeviceAndLogListForQuery(query)

        return render_template("page/PageSearchResults.html",
                               query=deviceList,
                               types=models.getDeviceTypes(),
                               params=searchPhrase,
                               searchPhrase=searchPhrase)
def renderPage_View(serial, error=None):
    device = models.Device.select().where(
        models.Device.SerialNumber == serial).get()
    log = models.getDeviceLog(serial)

    if len(log) > 0:
        device.statusIsOut = not log.get().DateIn
    else:
        device.statusIsOut = False

    return render_template("page/PageViewItem.html",
                           device=device,
                           types=models.getDeviceTypes(),
                           states=models.getStates(),
                           log=log,
                           error=error)
def renderPage_View(serial, error = None):
	device = models.Device.select().where(models.Device.SerialNumber == serial).get()
	log = models.getDeviceLog(serial)
	
	if len(log) > 0:
		device.statusIsOut = not log.get().DateIn
	else:
		device.statusIsOut = False
	
	return render_template("page/PageViewItem.html",
			device = device,
			types = models.getDeviceTypes(),
			states = models.getStates(),
			log = log,
			error = error
		)
def search():
	if not request.method == 'POST':
		return getIndexURL()
	
	searchPhrase = str(request.form['searchField'])
	
	if searchPhrase.startswith("scan:"):
		serial = searchPhrase[5:]
		query = models.getDeviceLog(serial)
		isOut, obj = models.getStatus(query)
		if isOut:
			modal = "signIn"
		else:
			modal = "signOut"
		return renderInventoryListings(searchSerial = serial, searchModal = modal)
		
	if models.isSearchUser(searchPhrase):
		logs = models.Log.select().where(
				models.Log.AuthorizerIn.contains(searchPhrase) | 
				models.Log.AuthorizerOut.contains(searchPhrase) | 
				models.Log.UserIn.contains(searchPhrase) | 
				models.Log.UserOut.contains(searchPhrase)
			).order_by(-models.Log.DateOut)
		
		return render_template("page/PageUserLogs.html", query = logs, searchPhrase = searchPhrase)
		
	if len(searchPhrase) == 4 and searchPhrase.isdigit():
		searchPhrase = "LCDI-" + searchPhrase
	
	if (len(models.Device.select().where(models.Device.SerialNumber == searchPhrase)) == 1):
		return renderPage_View(searchPhrase)
	else:
		query = models.getDevicesAndLogs().where(
			models.Device.SerialNumber.contains(searchPhrase) |
			models.Device.SerialDevice.contains(searchPhrase) |
			models.Device.Type.contains(searchPhrase) |
			models.Device.Description.contains(searchPhrase) |
			models.Log.Purpose.contains(searchPhrase)
		)
		deviceList = models.getDeviceAndLogListForQuery(query)
		
		return render_template("page/PageSearchResults.html",
				query = deviceList,
				types = models.getDeviceTypes(),
				params = searchPhrase,
				searchPhrase = searchPhrase)
def search_results(search):
	#query = models.Device.query.whoosh_search(search)

	query = models.Device.select(models.Device.serialNumber,
								 models.Device.typeCategory,
								 models.Device.description,
								 models.Device.issues,
								 models.Device.state
								 ).where(models.Device.serialNumber.contains(search) | 
								 		 models.Device.typeCategory.contains(search) |
								 		 models.Device.description.contains(search)).order_by(models.Device.serialNumber)

	types = models.getDeviceTypes()
	return render_template('searchResults.html',
			query=query,
			types=types,
			logoutURL=url_for('logout')
		)
def renderMainPage(serialNumber = '', itemType = 'ALL', state = 'ALL', status = 'All'):
	# TODO filter results, set page form filters, and remove prints
	print(serialNumber)
	print(itemType)
	print(state)
	print(status)
	
	query = models.Device.select().order_by(models.Device.serialNumber)
	types = models.getDeviceTypes()
	states = models.getStates()
	return render_template('index.html',
			query=query,
			types=types,
			states=states,
			totalItems=len(query),
			
			name=escape(session['displayName']),
			logoutURL=url_for('logout'),
			indexURL=url_for('index'),
			hasEditAccess=True
		)