I have a system whereby it allows to import the student attendance file into the system. However, it did not import it successfully. I tried to debug and i found out that when it goes to the importer.py function, it does not go to the "def _do_save(self, row)", hence that is the problem why it was not saved into database.
importer.py
class AttendanceImporter(CsvImporter):
field_names=["username", "mark"]
#it does not go to the method.
def _handle_row(self,row):
print("this")
if (not self._is_row_valid(row)):
return self._FAILED
username=row["username"]
if (self._is_username_exist(username)):
if (self._is_mark_exist(username)):
if (self._do_update(row)):
return self._UPDATED
else:
return self._FAILED
else:
if (self._do_save(row)):
return self._CREATED
else:
return self._FAILED
else:
return self._FAILED
def _is_row_valid(self, row):
for item in self.field_names:
if (len(row[item])==0):
return False
return True
def _is_username_exist(self, username):
return len(User.objects.filter(username=username))>0
print(username)
def _is_mark_exist(self, username):
user = User.objects.get(username=username)
return len(Attendance.objects.filter(user=user))>0
print(username)
def _do_save(self, row):
# create attendace mark
try:
attendance=Attendance()
user=User.objects.get(username=row["username"])
attendance=Attendance.objects.create(user=user, mark=row["mark"])
print("save?")
attendance.save()
except:
return False
return True
def _do_update(self, row):
# update attendance mark
try:
user=User.objects.get(username=row["username"])
attendance=Attendance.objects.get(user=user)
attendance.mark = row["mark"]
attendance.save()
except Exception as e:
print(e)
return False
return True
Views.py
#transaction.atomic
#csrf_exempt
def data_import(request, file_type):
# TODO: currently only support importing users, later can support importing groups
fields_required = None
if(file_type == "user"):
fields_required = "username, password, email, matric_number, fullname, groups"
elif (file_type == "attendance"):
fields_required = "username, mark"
if request.FILES:
successful = False
try:
im = None
if(file_type == "user"):
upload_file = request.FILES['user_file']
file_path = save_uploaded_file(request.FILES['file'], filename=generate_unique_file_name(extension="csv"),
filedir=USER_DATA_UPLOAD_PATH)
im = StudentImporter(source=open(file_path))
elif (file_type == "attendance"):
upload_file = request.FILES['attendance_file']
file_path = save_uploaded_file(upload_file, filename=generate_unique_file_name(extension="csv"),
filedir=USER_DATA_UPLOAD_PATH)
im = AttendanceImporter(source=open(file_path))
successful, result = im.import_to_database()
except Exception:
pass
if successful:
messages.info(request, "The import is successful!\n" + result)
else:
messages.warning(request, "The import is NOT successful, no data is imported!")
return HttpResponseRedirect(reverse("student_user_profile_list"))
return render(request,
"app-appglobal/import-data.html",
{'type': file_type, 'fields_required': fields_required})
Model.py:
class Attendance(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE)
mark=models.IntegerField(default=0)
class Meta:
ordering=['user']
def get_mark(self):
return self.mark
Urls
url(r'^student/attendance/$' ,views.data_import,{'file_type':'attendance'},name='attendance_import'),
CSVIMPORTER
class CsvImporter(object, metaclass=ABCMeta):
def __init__(self, source):
"""
:param source: a file object
:return:
"""
self.source=source
def import_to_database(self):
"""
:return: (successful:boolean,result:string)
"""
pass
example:
Related
I have a POST function like this:
def post(self, request):
try:
serializer = CCTDSPostSerializer(data=request.data)
print("serializer", serializer)
print("is valid", serializer.is_valid())
The Serializer is as follows, its not a model serializer for specific reasons.
class CCTDSPostSerializer(serializers.Serializer):
status = serializers.CharField()
transaction = serializers.CharField(allow_blank=True, allow_null=True)
comment = serializers.CharField(allow_blank=True, allow_null=True)
tds_id = serializers.ListField(child=serializers.IntegerField())
def check_tds_eligibility(self, data):
tds_ids = data.get('tds_id', None)
if tds_ids is not None:
tds_obj = TDS.objects.filter(id__in=tds_ids, status='open')
if tds_obj.count() == len(tds_ids):
return tds_ids
return None
def validate_status_transaction(self, obj):
status = obj.get('status', None)
transaction = obj.get('transaction', None)
if status == 'closed' and transaction is not None:
return True
elif status == 'rejected' and transaction is None:
return True
return False
def validate(self, obj):
validate_status_transaction = self.validate_status_transaction(obj)
tds_ids = self.check_tds_eligibility(obj)
if validate_status_transaction and tds_ids:
print("returning obj")
return obj
print("returning false")
return False
The data that I am passing it is:
{
"tds_id":[1],
"status":"closed",
"transaction":"ABC",
"comment":"Boom"
}
Now based on the conditions on the data present in the database, it comes to the statement print("returning false") i.e. it is returning False, but on the view side, the statement serializer.is_valid() gives the output as True
How come the validate function returns False and the is_valid returns True?
validate method returns validated date or raise error. Since your validate method doesnt raise any error is_valid() returns True. Change method like this to fix:
def validate(self, obj):
validate_status_transaction = self.validate_status_transaction(obj)
tds_ids = self.check_tds_eligibility(obj)
if validate_status_transaction and tds_ids:
print("returning obj")
return obj
print("returning false")
raise serializers.ValidationError("Some error")
I have simply 'to do' app in python 2.7 and I wrote for this some unit test. This is my first time with python unit test and I just wanna to know idea of unit test.
Someone can tell me whether I'm going in the right direction?
How to improve this tests?
How to check message in IndexError is correct? for this ("IndexError('Note doesn\'t exist')" or IndexError('Returned more then one entry') )
App:
# coding: utf-8
from __future__ import unicode_literals
from shutil import copyfile
import json
import os
DATABASE = 'notes_data/notes.json'
BOARDS = ['to do', 'in progress', 'done']
class NotesManagerMixin(object):
def count(self):
return len(self.notes)
def filter(self, *args, **kwargs):
result = self.notes
for key, value in kwargs.iteritems():
result = [
note for note in result
if getattr(note, key, None) == value or
note.message.startswith(str(value)) or
note.message.endswith(str(value))
]
return NotesQueryset(result)
def get(self, *args, **kwargs):
notes = self.filter(*args,**kwargs)
if notes.count() == 0:
raise IndexError('Note doesn\'t exist')
elif notes.count() == 1:
return notes[0]
else:
raise IndexError('Returned more then one entry')
def first(self):
return self.notes[0]
def last(self):
return self.notes[-1]
class NotesQueryset(NotesManagerMixin):
def __init__(self, notes):
self.notes = [note for note in notes]
def update(self, *args, **kwargs):
for note in self.notes:
for key, value in kwargs.items():
setattr(note, key, value)
note.save()
return self
def delete(self):
for note in self.notes:
note.delete()
return self
def __getitem__(self, idx):
return self.notes[idx]
def __str__(self):
return str(self.notes)
def __repr__(self):
return self.__str__()
class NotesManager(NotesManagerMixin):
def __init__(self):
self.notes = []
def __iter__(self):
return self.next()
def __generate_id(self):
"""
Funkcja pomocnicza do pobrania pewnej wolnej wartoĊci indexu.
"""
try:
return max(note.id for note in self.notes) + 1
except ValueError:
return 1
def all(self):
return NotesQueryset(self.notes)
def add(self, idx, board, message):
self.notes.append(Note(idx=idx, board=board, message=message))
def create(self, board, message):
note = Note(
idx=self.__generate_id(),
board=board,
message=message
)
note.clean()
self.notes.append(note)
note.save()
return note
def next(self):
for note in self.notes:
yield note
def to_dict(self):
return [note.to_dict() for note in self.notes]
class Note(object):
objects = NotesManager()
def __init__(self, idx, board, message):
self.id = idx
self.board = board
self.message = message
def __str__(self):
return 'ID: {}, Board: {}, Message: {}'.format(
self.id,
self.board,
self.message
)
def __repr__(self):
return self.__str__()
def clean(self):
if not self.message:
raise ValueError('Message is required')
if self.board not in BOARDS:
raise ValueError('Board "{}" doesn\'t exists'.format(self.board))
if type(self.id) != int:
raise ValueError('Note id "{}" is invalid'.format(self.id))
def save(self):
for key, note in enumerate(self.objects):
if note.id == self.id:
self.objects.notes[key] = self
break
with open(DATABASE, 'w') as database_file:
json.dump(self.objects.to_dict(), database_file, indent=4)
return True
def delete(self):
for key, note in enumerate(self.objects.notes):
if note.id == self.id:
self.objects.notes.pop(key)
with open(DATABASE, 'w') as database_file:
json.dump(self.objects.to_dict(), database_file, indent=4)
def to_dict(self):
return {
'id': self.id,
'message': self.message,
'board': self.board
}
def load_initial_data():
with open(DATABASE, 'r') as database_file:
json_data = json.load(database_file, encoding='utf-8')
for item in json_data:
Note.objects.add(
idx=item['id'],
board=item['board'],
message=item['message'],
)
load_initial_data()
unit tests:
import unittest
from notes_manager_v2 import NotesQueryset, Note, load_initial_data, NotesManagerMixin
class TestNotesQueryset(unittest.TestCase):
def test_filter(self):
actual = Note.objects.all().filter(board='in progress') # get all notes with board 'in progress'
expected = []
for note in Note.objects.all():
if note.board == 'in progress':
expected.append(note)
self.assertItemsEqual(actual, expected)
def test_get(self):
actual = [Note.objects.all().get(id=1)] # get note with method get
expected = []
for note in Note.objects.all(): # search note with index 1
if note.id == 1:
expected.append(note)
self.assertEqual(actual, expected)
def test_get_fail_1(self):
self.assertRaises(IndexError, lambda:Note.objects.all().get(id=9868976)) # thos note dont exist should raise IndexError
def test_update(self):
from_board = 'to do'
to_board = 'done'
before_change = Note.objects.filter(board=from_board) # use filter method to get all notes with board 'to do'
actual = Note.objects.filter(board=from_board).update(board=to_board) # update all board
self.assertNotEqual(before_change, actual) # check for difference
notes = Note.objects.all()
for note in actual:
self.assertIn(note, notes) # check notes are updated
def test_delete(self):
to_delete = Note.objects.filter(id=2).delete() # find note with filter method and delete it
notes = Note.objects.all()
self.assertNotIn(to_delete, notes)
def test_create(self):
new_note = Note.objects.create(message='lorem ipsum', board='in progress') # create new note
notes = Note.objects.all()
self.assertIn(new_note, notes) #
if __name__ == '__main__':
unittest.main()
Have you looked at the documentation? See doctest. They are an easy way to integrate unit tests into python code. Another option is the unittest framework.
from CodernityDB.database import Database
from CodernityDB.hash_index import HashIndex
class WithXIndex(HashIndex):
def __init__(self, *args, **kwargs):
kwargs['key_format'] = '16s'
super(WithXIndex, self).__init__(*args, **kwargs)
def make_key_value(self, data):
username = data['username']
# if not isinstance(login, basestring):
# login = str(login)
return md5(username).digest(), None
def make_key(self, key):
return md5(key).digest()
def main():
db = Database('l1.db')
if db.exists():
db.open()
else:
db.create()
x_ind = WithXIndex(db.path, 'username')
db.add_index(x_ind)
db.insert(dict( username='lamar', age='33', frm='new jersey'))
for dt in db.all('username',with_doc=True):
print dt
print db.get('username', 'lamar') # throws an exception
I'm trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:
(reading_threshold =
int(request.POST['reading_threshold']))
I do not see anything that I can notice as wrong in that statement or the syntax in custom.py.
What is my mistake here?
A slightly sanitized version of the file reads:
from django.http import HttpResponse
def threshold_check(user, item, text = None):
if user.issuperuser():
if text == None:
return True
else:
return text
else:
if (user.status >= item.threshold and user.reading_threshold <=
item.threshold):
if text == None:
return True
else:
return text
else:
if text == None:
return False
else:
return ''
def threshold_check_required(item):
def outer_wrap(view_function):
def inner_wrap(request, *arguments, **keywords):
if request.user.issuperuser():
return view_function(request, *arguments, **keywords)
else:
if (request.user.status >= item.threshold and
request.user.reading_threshold <= item.threshold):
return view_function(request, *arguments, **keywords)
else:
return HttpResponse('')
return inner_wrap
return outer_wrap
def threshold_controls(request):
user = request.user
if user and user.status != None:
if request.method == 'POST':
try:
(reading_threshold =
int(request.POST['reading_threshold']))
except ValueError:
reading_threshold = user.reading_threshold
try:
(writing_threshold =
int(request.POST['writing_threshold']))
except ValueError:
writing_threshold = user.writing_threshold
writing_threshold = min(user.status, writing_threshold)
reading_threshold = min(writing_threshold,
reading_threshold)
user.reading_threshold = reading_threshhold
user.writing_threshold = writing_threshold
user.save()
return render_to_response('threshold_controls.html',
{
'user': user
})
else:
return render_to_response('threshold_controls_blank.html')
You're looking right at the error. Python isn't C; assignment is a statement, rather than an expression, so you can't parenthesize it.
I wrote a custom auth script and it is giving me an Internal Server error, not quite sure what is causing this or how to get detailed error reporting.
allowed_hosts is set to [] but DEBUG is set to True so that shouldn't matter. I'm accessing the site directly via IP address.
This is for a Startup Weekend so quick assistance would be the best! THANK YOU! Using Django 1.5.5
import uuid, datetime, pickle
import project.models as models
from django.contrib import auth
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseServerError, HttpResponseRedirect
from django.shortcuts import render_to_response, redirect
class user(object):
userName = str()
userID = uuid.UUID(int=0)
_exists = False
_authenticated = False
def is_authenticated(self):
return self._authenticated
def __str__(self):
return str(self.__dict__.copy())
def __getstate__(self):
return self.__dict__.copy()
def __setstate__(self, dict):
self.__dict__ = dict
def __init__(self, username=None):
if username:
self.initialize(username)
def initialize(self, username):
self.userName = username
model = models.User.objects.filter(userName=self.userName).all()
if len(model) == 1:
model = model[0]
self.data = model
self._exists = True
self.userID = model.id
else:
self._exists = False
def authenticate(self, password):
self._authenticated = False
if self._exists:
import hashlib
hash = hashlib.md5('%s%s' % (str(password), self.data.pwSalt)).hexdigest()
if hash == self.data.pwHash:
self._authenticated = True
return True
return False
def updateUser(self):
self.initialize(models.User.objects.filter(id=self.userID).get().userName)
def mkContext(self, context):
c = context
c['user'] = self
return c
class userMiddleWare(object):
def authenticated(self, request):
if (request.session.get('user', False)):
request.user = request.session["user"]
return True
return False
def authenticate(self, request):
if (request.method == 'POST' and ('username' in request.POST) and ('password' in request.POST)):
u = user(str(request.POST['username']))
if (u.authenticate(str(request.POST['password']))):
request.session['user'] = u
request.user = u
try:
request.session.save()
except:
e = "Session wasn't saved or something?"
return True
request.user = user()
return False
def authorize(self, request):
return True
def process_request(self, request):
next = False
if request.method == 'POST':
if request.POST.get('username', False) and request.POST.get('password', False):
next = request.POST.get('next', False)
if not self.authenticated(request):
self.authenticate(request)
if request.session.has_key('user'):
if not request.session['user']._authenticated:
return redirect('project.views.main')
else:
return redirect('project.views.main')
# context = Context(request)
if request.session.has_key('user'):
request.user = request.session['user']
if next:
return redirect(next)
else:
request.user = user()
def process_response(self, request, response):
try:
if hasattr(request, 'session'):
request.session.save()
except:
e = "Session wasn't saved or something?"
return response
def process_exception(self, request, exception):
return
# import settings
# # if getattr(settings, 'DEBUG', False):
# # return
# import traceback
# timestamp = str(datetime.datetime.now())
# if exception == 'process_request' or exception=='process_response':
# errtype = exception
# else:
# errtype = 'Application Error'
# if hasattr(exception, 'args'):
# message = getattr(exception, 'args')
# else:
# message = 'Unknown'
# if hasattr(request, 'path'):
# path = getattr(request, 'path', None)
# else:
# path = 'Unknown'
# if (hasattr(request, 'session') and getattr(request, 'session').get('user', None)):
# user = request.session['user'].userName
# try:
# tb = ''
# for l in traceback.format_stack():
# tb += l
# trace = tb
# except:
# trace = []
# context = Context({'errtype':errtype, 'message':message, 'path':path, 'trace':trace, 'user':user, 'client':client})
# return HttpResponseServerError(get_template('error.html').render(context))