How to communicate between C++ and django channels? - django

Iam doing django project using django channels. In my project I need to integrate and communicate between C++ code. In between i have to send some values to C++.
I checked without django channels and it is working fine,Can any one help me out how can I implement below code in consumers.py of django channels.
proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

I have found the answer using Syncconsumer with synchronous function
class CameraParallelAsync(SyncConsumer):
def websocket_connect(self,event):
print("connected",self.channel_name)
print('Data after delay')
self.channel_group_name = "task_single_snap"
self.send({
"type":"websocket.accept"
})
def websocket_receive(self,event):
print("receive",event)
channel_data = event.get('text',None)
user_name = self.scope['url_route']['kwargs']['user']
team_name = self.scope['url_route']['kwargs']['team']
media_path = settings.MEDIA_ROOT
channel_name = self.channel_name
if channel_data is not None:
if channel_data =='CAMERA_ON':
self.scope['session']['camera'] = True
print('Camera Enabled')
global proc
os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"
import subprocess
proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
self.send({
"type":"websocket.send",
"text":"Camera Enabled"
})
elif channel_data == 'CAMERA_OFF':
print('Camera disabled')
self.scope['session']['camera'] = False
import signal
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
elif channel_data == 'SINGLE_SNAP':
self.send({
"type":"websocket.send",
"text":"We are processing your request"
})
print('Single Snap')
task = tasks.camera_single_task.delay(user_name,team_name,media_path,channel_name)
print(task.status,'status')
elif channel_data == 'GROUP_SNAP':
print('Group Snap')
os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"
import subprocess
proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoGroupFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print(proc.communicate())
self.send({
"type":"websocket.send",
"text":"Group snap Snaptured"
})
else:
print('No detection')
def websocket_disconnect(self,event):
print("disconnected",event)
# Receive message from room group
def task_message(self, event):
message = event['message']
print('hello')
# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message
}))

Related

Is there a best practice to create a Django query with extensible filters?

Suppose I have a "find invoice page" where users can input (or not) different fields in order to search for invoices to display them in a list.
Suppose this list of fields might grow over time with additional db fields.
My current (working) solution is the following but I'm wondering if there is a better way to allow possible additional fields and adapt the query accordingly.
query.py [custom query composer]
from .models import Invoice
class FindQuery:
def __init__(self):
self.filters = {}
self.fq_sql = ''
self.fq_select = 'SELECT * '
self.fq_from = 'FROM invoice_invoice '
self.fq_where = 'WHERE 1=1 '
self.fq_order = ' ORDER BY iv_created_at DESC'
def set_filters(self, user, year, client):
if user:
self.filters['request_user'] = user
if year:
self.filters['selected_year'] = year
if client:
self.filters['selected_client'] = client
# adding here new fields
def apply_filter(self):
if 'request_user' in self.filters:
self.fq_where += ' AND iv_created_by = ' + str(self.filters.get('request_user'))
if 'selected_year' in self.filters:
self.fq_where += ' AND iv_year = ' + str(self.filters.get('selected_year'))
if 'selected_client' in self.filters:
self.fq_where += ' AND iv_client = ' + str(self.filters.get('selected_client'))
# adding here new fields
self.fq_sql = self.fq_select + self.fq_from + self.fq_where + self.fq_order
return Invoice.objects.raw(self.fq_sql)
views.py (Django Rest Framework)
from .query import FindQuery
class InvoiceViewSet(viewsets.ModelViewSet):
serializer_class = InvoiceSerializer
def get_queryset(self):
q = FindQuery()
user = self.request.user.id
year = self.request.GET.get('selected_year')
client = self.request.GET.get('selected_client')
# adding here new fields
q.set_filters(user, year, client)
qs = q.apply_filter()
result = Invoice.objects.filter(iv_id__in=[q.iv_id for q in qs])
return result
Here the solution with ORM approach. Short and fast! Also solves the problem of additional/optional search fields.
class InvoiceViewSet(viewsets.ModelViewSet):
serializer_class = InvoiceSerializer
def get_queryset(self):
# input value from search page
user = self.request.user.id
year = self.request.GET.get('selected_year')
client = self.request.GET.get('selected_client')
filters = {
# model name: input value
}
if user:
filters['iv_created_by'] = user
if year:
filters['iv_year'] = year
if client:
filters['iv_client'] = client
result = Invoice.objects.filter(**filters)
return result

Convert CGI to Django framework

Can anyone help me on what approache should be used to convert a CGI script to Django Framework?
I will need to use HTTP Get method request from an HTML Form to obtain user inputs and inject them into a python script.
Here is a sample of CGI python script:
#!/usr/bin/python
import cgi, cgitb
from RTTexec import *
# from RTTexecCustom import *
from globals import *
form = cgi.FieldStorage()
print "Content-Type: text/html"
print ""
print "<html>"
print "<head>"
print "<title>RTT</title>"
print '<link rel="stylesheet" href="../jquery-ui-1.11.4.custom/jquery-ui.css">'
print "</head>"
print "<body>"
print '<div class="inside">'
print '<p>'
print "The user entered data are:<br>"
step = 0
try:
execTypeExtract = ''
execTypeExec = ''
try:
execTypeExtract = form["execTypeExtract"].value
except:
None
try:
execTypeExec = form["execTypeExec"].value
except:
None
try:
info = form["information"].value
except:
None
if execTypeExtract != '' and execTypeExec != '':
executionIncludes = execTypeExtract+'_'+execTypeExec
elif execTypeExtract != '':
executionIncludes = execTypeExtract
elif execTypeExec != '':
executionIncludes = execTypeExec
else:
step = 1
print "<b>execution Includes:</b> " + executionIncludes + "<br>"

NameError: global name 'hashed_pwd' is not defined

i am new with python and Flask .I make a login and signup page and everything is working fine.Now, i use werkzeug for password security at signup page password is generated in encrypted form and stored in database but when i tried to login then there i got an error "NameError: global name 'hashed_pwd' is not defined" please provide me solution.
#app.route('/signUp', methods=['POST','GET'])
def signUp():
_name = request.form['user_name'] #database connectivity
_password = request.form['user_password']
_pname = request.form['patient_name']
_email = request.form['user_email']
_cont = request.form['user_contact']
_add = request.form['user_address']
if _name and _password and _pname and _email and _cont and _add:
conn = mysql.connect()
cursor = conn.cursor()
hashed_pwd=generate_password_hash(_password) #password generated
query_string = """INSERT INTO tbl_puser (user_name,user_password,patient_name,user_email,user_contact,user_address) VALUES('%s','%s','%s','%s','%s','%s');"""%(_name,hashed_pwd,_pname,_email,_cont,_add)
print query_string
cursor.execute(query_string)
conn.commit()
cursor.close()
conn.close()
flash('You were successfully registered')
return render_template('select.html')
#app.route('/Login',methods=['POST','GET'])
def login():
user_name=request.form['uname']
user_password=request.form['psw']
# NameError: global name 'hashed_pwd' is not defined error
check_password_hash(hashed_pwd,user_password)
if user_name and user_password:
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute("SELECT * from tbl_puser where user_name='" + user_name + "' and user_password='"+ user_password +"'")
print "SELECT * from tbl_puser where user_name='" + user_name + "' and user_password='"+ user_password +"';"
data = cursor.fetchall()
print data
if len(data) != 0:
return render_template("select.html")
else:
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()
you simple forgot defined it
user_password = request.form.get('psw', '')
# Add next line
hashed_pwd = generate_password_hash(user_password)
check_password_hash(hashed_pwd,user_password)
and better use get method of the request.form to get the values of items.

django save not working as update in views.py with model having an overridden save

I've looked at all the related answers but none fixed my issue. I'm trying to save an object that's already created with no luck. I could see the view getting executed and the values I updated for the object but changes are not reflecting in the database. Here is the code snippet of the view and the model.
views.py
class Workspace(_LoggedInMixin, View):
def get(self, request):
user = self.request.user
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
component_status = request.session.get('component', None)
request.session['component'] = None
params = dict(components=components, status=component_status)
return render(request, 'workspace.html', params)
def post(self, request):
data = request.POST
formtype = data['form-type']
error = None
user = self.request.user
if formtype == 'component':
if data['action'] == 'create':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel(user=user, name=data['cname'], time=dt.now(), stats=data['cstats'],
output=data['coutput'], parent=cparent)
component.save()
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
component.codefile = 'D:/' + self.request.user.username + '/' + str(component.id) + '.py'
component.save()
request.session['component'] = {'name': data['cname'], 'message': 'Component created successfully!'}
except Exception as e:
component.delete()
error = e.message
elif data['action'] == 'delete':
try:
c = ComponentModel.objects.get(pk=data['compid'])
cname = c.name
c.delete()
os.remove('D:/' + self.request.user.username + '/' + data['compid'] + '.py')
request.session['component'] = {'name': cname, 'message': 'Component deleted successfully!'}
except Exception as e:
error = e.message
elif data['action'] == 'save':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel.objects.get(pk=data['compid'])
component.user = user
component.name = data['cname']
component.time = dt.now()
component.stats = data['cstats']
component.output = data['coutput']
component.parent = cparent
component.save()
print component
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
request.session['component'] = {'name': data['cname'], 'message': 'Component saved successfully!'}
except Exception as e:
error = e.message
if error is not None:
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
params = dict(error=error, components=components)
return render(request, 'workspace.html', params)
return redirect('/workspace')
models.py
class ComponentModel(models.Model):
class Meta:
# overriding the default table name with the following name
db_table = 'components'
verbose_name = 'components'
get_latest_by = 'time'
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=255)
time = models.DateTimeField(db_column='created_on')
codefile = models.CharField(max_length=100, db_column='code_file_name', null=True)
stats = models.TextField(db_column='statistical_fields', null=True)
output = models.TextField(db_column='output_fields')
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
def save(self, *args, **kwargs):
try:
super(ComponentModel, self).save(self, *args, **kwargs)
except Exception as e:
return "Something went wrong while saving the component. Details - %s" % e.message
def __str__(self):
return "{id: %s, name: %s, user: %s, time: %s, parent: %s}" % (
self.id, self.name, self.user, self.time, self.parent)
The second save call in data['action'] == 'create' and the save call in data['action'] == 'save' are not updating the database. I appreciate any help. Thank you.
Your super call is incorrect. Update so that self is not called as a method argument - as you have it written it will always fail.
In addition, you are not raising the exception when a save is not completed, so you have no idea if there was an error unless viewing the std output. You probably want this to actually raise an error.
Update as such -
def save(self, *args, **kwargs):
try:
super(ComponentModel, self).save(*args, **kwargs)
except Exception as e:
raise Exception("Something went wrong while saving the component. Details - %s" % (e.message,))

Using classes and methods defined in another module from within a django view

I have a module that handles upload of CSV's
Within the module,I have a file called twzworkscsvreader.py that contains classes and functions that check if the file uploaded is a csv file by looking at the header(defined) and throws errors if the file didn't upload successfully.presently, i can see the errors from the console.
I want to add a functionality within my view that returns this errors to the page in the browser instead of just printing them to the console.
here is the twzworkscsvreader.py part that handles this errors
import logging
import csv
import time
import warnings
import datetime
import dateutil.parser as dparser
import os
class CSVFileReader:
def __init__(self, filename, evidence, mysql, filetype):
""" Class constructor.
Args:
- filename: csv file name to process
- evidence: evidence number supplied from the command line
- mysql: instance of the mySQL database
- filetype: type of the file to process
"""
self.errorCount = 0
self.path = filename
self.evidence = evidence
self.mysql = mysql
self.dictInstance = {}
self.filetype = filetype
#TO DO: move this outside maybe back into function. I dont think this is right place this this.
self.artifacts = ['AuditPolicy','App Paths','exefile open\command','cmdfile open\command','batfile open\command','htafile open\command','piffile open\command',\
'http open\command','browsers','Run keys','NetworkCards','Browser Helper Objects']
def is_arbitrary_text(self,row):
"""
Check if an TZworks CSV header is present
Args:
row: the row as it was read from the csv
Returns:
Boolean: True or False
"""
##TO DO CLEAN UP HOW THIS IS FORMATTED BELOW. MAYBE PULL ALL THIS INTO A SEPERATE FILE.
if self.filetype == "jmp" and str(row) == "['source path/filename', 'source type ', 'appid ', 'MRU/MFU', 'stream#', 'MRU date ', 'MRU-UTC ', 'file mdate ', 'mtime-UTC ', 'file adate ', 'atime-UTC ', 'file cdate ', 'ctime-UTC ', 'tgt mdate ', 'mtime-UTC ', 'tgt adate ', 'atime-UTC] ', 'tgt cdate ', 'ctime-UTC ', 'ObjID cdate ', 'ctime-UTC ', 'tgt attrib', 'inode ', 'seq# ', 'file size ', 'target name ', 'idlist extra info ', 'vol type', 'vol serial', 'vol label', 'local path', 'common path', 'network/device info', 'extra info', 'netbios name', 'volume id', 'object id', 'mac addr']":
return True
elif self.filetype == "usb" and str(row) == "['device name', 'vid/pid', ' time-utc', 'install', ' time-local', \
'disk dev', ' time-utc', 'vol dev', ' time-utc', 'type', 'vid', 'pid', 'hub', 'port', \
'vendor', 'product', 'rev', 'volume guid', 'vol', 'vol name', 'users [ date/time - utc]', \
'instance id/serial #', 'Other dates defined by explicit property keys', 'Readyboost (freeform list of EMDMgmt entries) vol serial# / vol name / last modify regtimes [utc] and * = test time']":
return True
elif self.filetype == "link" and str(row) == "['source path/filename', 'source type', 'file mdate', ' time-UTC', 'file adate', ' time-UTC', \
'file cdate', 'ctime-UTC', 'tgt mdate', ' time-UTC', 'tgt adate', ' time-UTC]', 'tgt cdate', ' time-UTC', 'ObjID cdate', ' ctime-UTC', 'tgt attrib', \
'inode', 'seq#', 'file size', 'target name', 'idlist extra info', 'vol type', 'vol serial', 'vol label', 'local path', 'common path', 'network/device info', \
'extra info', 'netbios name', 'volume id', 'object id', 'mac addr']":
return True
elif self.filetype == "prefetch" and str(row) == "['prefetch file name ',' app name ',' times ran',' last run ', \
' time-utc ',' mdate ',' time-utc',' adate ',' time-utc',' cdate ',' time-utc',' path/appname ',' Num Vols',' volume(s)', \
' volume serial(s)',' volume date/time(s) (utc)',' Num modules',' Module list']":
return True
elif self.filetype == "sbag" and str(row) == "['regdate', ' reg-UTC', 'mru', 'mdate', ' time-UTC', 'adate', ' time-UTC', 'cdate', ' time-UTC', \
'type', 'bag', 'file size', 'inode', 'seq#', 'full path', 'source subkey/value name', 'user acct', 'extra metadata', 'bag registry dates related to this entry']":
return True
elif self.filetype == "AuditPolicy" and row == "regdate, reg-UTC,policy name,type audit":
return True
elif self.filetype == "App Paths" and row == "reg date, reg-UTC,subkey,value name,value data":
return True
elif self.filetype == "exefile open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "cmdfile open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "batfile open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "htafile open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "piffile open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "http open\command" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "browsers" and row == "reg date, reg-UTC,subkey,value name,value data":
return True
elif self.filetype == "Run keys" and row == "reg date, reg-UTC,value name,value data":
return True
elif self.filetype == "NetworkCards" and row == "reg date, reg-UTC,subkey,value name,value data":
return True
elif self.filetype == "Browser Helper Objects" and row == "reg date, reg-UTC,subkey,value name,value data":
return True
else:
return False
#TODO: Move this to its own file
def StringToDatetime(self, datetime, dt_type):
"""Converts a string timestamp into a datetime object.
Args:
datetime: A string formatted as a timestamp (i.e 1970-01-01 00:00:00.000).
dt_type: which datetime field we are parsing. Used for logging purposes
Returns:
A datetime object or None if the parsing fails."""
# Here some dates have space (like "install" and some are just empty (like "userDateTime)
# so we need to check for both conditions
if datetime == " " or datetime == "" or datetime == " ":
self.errorCount += 1
#TO DO: Use %s
message = dt_type + " is missing the date at line "
message += str(self.rowcount)
logging.warn(message)
return None
else:
try:
datetimeobject = dparser.parse(str(datetime))
return datetimeobject
except:
self.errorCount += 1
#TO DO: Use %s
message = dt_type + " has incorrect date at line "
message += str(self.rowcount)
logging.error(message)
return None
def readyBoostParse(self, string):
"""Parse the last field of the csv file
Args:
string: string representation of the last field in the csv
Returns:
readyboostDict: dictionary of the items in the readyboost field
"""
#Readyboost (freeform list of EMDMgmt entries) vol serial# / vol name / last modify regtimes [utc] and * = test time
readyboostDict = {}
readyboost_items = string.split(';')
item_counter = 1
for item in readyboost_items:
if len(readyboost_items) > 5:
self.errorCount += 1
logging.error("Too many readyboost items at line " + str(self.rowcount))
try:
vol_sn_and_vol_name , _, last_mod_regdatetime = item.partition("[")
vol_sn, _, vol_name = vol_sn_and_vol_name.partition("/")
readyboostDict['volume_serial' + str(item_counter)] = vol_sn.replace('-', '')
readyboostDict['volume_name' + str(item_counter)] = vol_name
readyboostDict['last_modify' + str(item_counter)] = self.StringToDatetime(last_mod_regdatetime.strip("]"), "readyboost")
item_counter += 1
except Exception as e:
self.errorCount += 1
#TO DO add better error handeling and logging.
readyboostDict['volume_serial' + str(item_counter)] = ""
readyboostDict['volume_name' + str(item_counter)] = ""
readyboostDict['last_modify' + str(item_counter)] = None
item_counter += 1
return readyboostDict
def readAdditionalData(self):
""" Read the data from the additional CSV file
Args:
None
Return:
None
"""
in_header = False
headerPassed = False
internalHeader = False
#Why not use the other readData?
with open(self.path, "rb") as csvfile:
for line in csvfile:
line = line.rstrip('\r\n')
if not line:
continue
if internalHeader and headerPassed:
if line != "--------------------------------------------------------------------------------------------------------- ":# and not in_header:
if self.filetype == "AuditPolicy":
self.processAuditPolicyData(line)
if self.filetype == "App Paths":
self.processAppPathData(line)
if self.filetype == "exefile open\command":
self.processExeFileOpenCommand(line)
if self.filetype == "cmdfile open\command":
self.processCmdFileOpenCommand(line)
if self.filetype == "batfile open\command":
self.processBatFileOpenCommand(line)
if self.filetype == "htafile open\command":
self.processHtaFileOpenCommand(line)
if self.filetype == "piffile open\command":
self.processPifFileOpenCommand(line)
if self.filetype == "http open\command":
self.processHttpOpenCommand(line)
if self.filetype == "browsers":
self.processBrowsers(line)
if self.filetype == "Run keys":
self.processRunKeys(line)
if self.filetype == "NetworkCards":
self.processNetworkCards(line)
if self.filetype == "Browser Helper Objects":
self.processBrowserHelperObject(line)
else:
internalHeader = False
headerPassed = False
self.filetype = ""
if line == "--------------------------------------------------------------------------------------------------------- ":# and not in_header:
if not in_header:
in_header = True
else:
in_header = False
headerPassed = True
continue
#if line == "--------------------------------------------------------------------------------------------------------- " and in_header:
#in_header = False
#headerPassed = True
#continue
if in_header:
(key,value) = line.split(':')
value = value.strip()
if key == "Artifact":
if value not in self.artifacts:
print "Unknown artifact found."
logging.error("Unknown artifact found in file: {0:s}".format(self.path))
return
else:
self.filetype = value
continue
else:
continue
elif headerPassed and not internalHeader:
if not self.is_arbitrary_text(line):
return False
else:
internalHeader = True
def readData(self):
""" Read the data from the CSV file.
Args:
None
Return:
None
"""
try:
import codecs
with open(self.path, "rU") as data_initial:
reader = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",")
##oldcode
#data_initial = open("staff.csv", "rU")
#reader = csv.reader((line.replace('\0','') for line in csvfile), delimiter=",")
#reader = csv.reader(csvfile, delimiter=',', quotechar='"')
#Row number where header exists
rangecount = 0
#Check if more then one header exists in file.
header = False
#Count number of rows. Need to test to see what happens if error on one row.
self.rowcount = 1
for row in reader:
#Check if expected header is in place and if there are no other headers in file.
if self.is_arbitrary_text(row) == True and header == False:
for _ in range(rangecount):
next(reader, None)
self.is_arbitrary_text(reader)
reader = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",")
#Do processing for each row
for row in reader:
if self.filetype == "usb":
self.processUSBData(row)
if self.filetype == "lnk":
self.processLNKData(row)
if self.filetype == "prefetch":
self.processPreFetchData(row)
if self.filetype == "sbag":
self.processSBagData(row)
if self.filetype == "jmp":
self.processJmpData(row)
self.rowcount += 1
header = True
else:
rangecount +=1
if header == False:
logging.error("Did not find expected header in CSV file: " + self.path)
print "Did not find expected header in CSV file %s." % (self.path)
except IOError as e:
print "Error parsing file: %s". e.args[1]
can anyone write me a prototype of using this class within my view to so that it displays the errors to page? the class is defined within a module i have called mysq_db_loader and in a file called twzworkscsvreader.py
If you are using Django this package does a lot nice abstraction on top of CSV files: https://github.com/fusionbox/django-separated