djangotutsmeの django-extensions を使って mysqlNFC.py スクリプトを実行してみました
これを実行した後./manage.py runscript mysqlNFC
エラーメッセージ:
/usr/local/lib/python2.7/dist-packages/django/core/management/base.py:265: RemovedInDjango110Warning: OptionParser Usage for Django Management commands is deprecated, use ArgumentParser instead
RemovedInDjango110Warning)
error libnfc.driver.acr122_usb Unable to claim USB interface (operation not permitted
nfs-list: EREROR: Unable to open NFC device: acr122_usb:001:007
no (valid module for script 'mysqlNFC' found Try running with a higher verbosity level like: -v2 or -v3
であっても./manage.py unscript -v 2 mysqlNFC
、django モデルを前哨します
mysqlNFC.py
スクリプトを実行するには?
settings.py で:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'nfcapp',
'django_extensions',
]
スクリプト ディレクトリ:
scripts/
├── __init__.py
├── __init__.pyc
├── mysqlNFC.py
└── mysqlNFC.pyc
実行しようとしているスクリプトmysqlNFC.py
:
#!usr/bin/python
import MySQLdb, datetime, shlex, subprocess, re, time
from nfcapp.models import USER, CLASS, TIMETABLE, ATTENDANCE
db = MySQLdb.connect("localhost","root","","AttendanceDB")
curs = db.cursor()
output = subprocess.check_output(["nfc-list"])
output = output.decode("utf8")
uid = re.search(r'((\w){2}\s\s){4}',output).group(0).strip().split(" ")
uid = ' '.join(uid).upper()
print ("\n\nUID is: ")
print (uid)
#verify connectivity to database
sql = "SELECT * FROM USER WHERE USER.uid = '%s'" % (uid)
try:
curs.execute(sql)
result = curs.fetchone()
except:
print "unable to get info from DB"
if result is None:
print("UID non existant, would you like to add user?")
#id change log
id = raw_input("Please enter your id card no: ")
name = raw_input("Please enter your name: ")
lastname = raw_input("Please enter your surname: ")
s = "Thank you, your id card is: %s, your name is: %s, your surname is %s" % (id, name,lastname)
print(s)
sql = "INSERT INTO USER (id,uid,name,surname) VALUES ('%s','%s','%s','%s')" % (id, uid,name,lastname)
#cha
try:
curs.execute(sql)
db.commit()
except:
print "unable to get info from DB"
model.py
冗長性のため、一部のクラスは以下に記載されていません
from __future__ import unicode_literals
from django.db import models
##############################################################################
# Section: Maintain USER Objects --------------------------------------- #
##############################################################################
class USER(models.Model):
# verbose is visible in any pages, but uid=models.chardiled is in the db
uid = models.CharField(max_length = 20, verbose_name = 'uid')
name = models.CharField(max_length = 15, verbose_name = 'name')
surname = models.CharField(max_length = 15, verbose_name = 'surname')
# in sqlite
# surname VARCHAR(15)
def __str__(self):
''' Note: Return Human Readable Object '''
return 'USER %s: %s: %s: %s:' % (self.id, self.uid, self.name, self.surname)
#constsructers
@classmethod
def initialise(cls, id, uid, name, surname):
''' Note: Overloading Instances Method '''
return cls(id = id, uid = uid, name = name, surname = surname)
class Meta:
''' Note: Model Meta Options '''
verbose_name_plural = 'USER'
db_table = 'USER'
admin.py
from django.contrib import admin
from .models import USER, CLASS, TIMETABLE, ATTENDANCE
# Register your models here.
##############################################################################
# Section: Maintain USER Admin -------------------------------------------- #
##############################################################################
@admin.register(USER)
class UserAdmin(admin.ModelAdmin):
''' Note: Class to maintain USER Admin Interface '''
list_display = [ '__str__' ]
fields = [ ]
search_fields = [ ]
ordering = [ 'id' ]
list_max_show_all = True
inlines = [ ]