“libnfc - Django”如何使用Django运行外部python脚本?

时间:2016-04-01 13:54:31

标签: python mysql django django-extensions

我尝试使用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  = [  ]

0 个答案:

没有答案