def clean_trash(request): file_list = File.objects.filter(trashed=True) print "CLEAN ALL" for a in file_list: os.remove(os.path.realpath(os.path.dirname(__file__)) + "/media/" + a.file.name) a.delete() Log.create(request.user, "Permanently deleted a file", a, None).save() return HttpResponseRedirect("/trash/")
def write_log(date, title, time, notes): """Write the work log to the database.""" username = get_username() Log.create(username=username, task_date=date, task_title=title, task_time=time, task_notes=notes)
def save_logs_of_logging_call_xml(call_str, change_type, file_path, head_commit_db, method_name, component): Log.create(commit_id=head_commit_db.hexsha, file_path=file_path, embed_method=method_name, change_type=change_type, content_update_to=call_str, component=component)
def setUp(self): test_db.bind(MODELS, bind_refs=False, bind_backrefs=False) test_db.connect() test_db.create_tables(MODELS) Log.create(username='******', task_date=datetime.datetime(2011, 11, 11, 0, 0), task_title='Test', task_time=60, task_notes='Notes')
def test_search_username_term2(self): Log.create(username='******', task_date=datetime.datetime(2011, 11, 11, 0, 0), task_title='Test', task_time=60, task_notes='Notes') expected = 1 with patch('search.input', side_effect=['user', '1']): actual = search_username_term() self.assertEqual(actual.select().count(), expected)
def test_view_logs3(self): Log.create(username='******', task_date=datetime.datetime(2011, 11, 11, 0, 0), task_title='Test', task_time=60, task_notes='Notes') new_viewer = Viewer(Log.select()) expected = 'neds' with patch('viewer.input', return_value='s'): new_viewer.view_logs() actual = new_viewer.menu_options self.assertEqual(actual, expected)
def trash(request): if request.method=="POST": file_list = File.objects.filter(trashed=True) for a in file_list: if request.POST.get(str(a.id))!=None: a.trashed=False a.save() Log.create(request.user, "Restored a file", a, None).save() return HttpResponseRedirect("/trash/") else: is_admin = request.user.is_staff file_list = File.objects.filter(trashed=True) return render_to_response('trash.html', {'user': request.user, 'file_list':file_list, 'is_admin':is_admin}, context_instance=RequestContext(request))
def request_delete(request, document_number): if request.method=="POST": doc= Dokument.objects.get(id= int(document_number)) for k in doc.files.filter(trashed=False): if request.POST.get(str(k.id))!=None: k.trashed=1 # else: # k.trashed=0 k.save() Log.create(request.user, "Deleted a file", k, doc).save() return HttpResponseRedirect("/records") else: doc= Dokument.objects.get(id= int(document_number)) return render_to_response('request_delete.html', {'doc':doc})
def test_delete_log(self): Log.create(username='******', task_date=datetime.datetime(2011, 11, 11, 0, 0), task_title='Test', task_time=60, task_notes='Notes') expected = 1 new_viewer = Viewer(Log.select()) with patch('viewer.input', side_effect=['n', 'y', '']): new_viewer.delete_log() new_viewer.delete_log() logs = Log.select() actual = logs.select().count() self.assertEqual(actual, expected)
def upload(request): if request.POST: userid = request.POST.get('userid') # Queries all non-expired sessions sessions = Session.objects.filter(expire_date__gte=datetime.now()) # Checks if session is active for session in sessions: data = session.get_decoded() found_userid=data.get('_auth_user_id') # Prceeds when user id is validated if found_userid!=None and long(userid)==found_userid: user = User.objects.filter(id=userid)[0] faculty=None faculty=Faculty.objects.filter(id=request.POST.get('fid'))[0] transaction = Transaction.objects.get(id=request.POST.get('transaction')) document = Dokument() document.faculty= faculty document.transaction= transaction document.save() #Generates a random alphanum string for filename template while True: fnameTemplate='' fnameTemplate = ''.join(random.choice(string.ascii_lowercase)) fnameTemplate += ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(4)) + '_' if len(File.objects.filter(filename__startswith = fnameTemplate))==0: break #Processes uploaded files, page by page for key in request.FILES: files = request.FILES[key] filename = fnameTemplate + key.split('_')[1] + '.bmp' with open('DCSArchivingSystem/testapp/media/files/' + filename, 'wb+') as destination: for chunk in files.chunks(): destination.write(chunk) file = File() file.filename = filename file.file = 'files/' + filename file.save() document.files.add(file) Log.create(user, "Uploaded file", file, document).save() Log.create(user, "Created Document", None, document).save() return HttpResponseRedirect("/dashboard/") else: return render_to_response('upload.html', context_instance=RequestContext(request))
def post(self, application_name): sender = self.request.get("sender") if not sender: sender = self.request.get("email") if not sender: sender = self.request.get("username") if not sender: sender = self.request.get("facebookId") if not sender: sender = self.request.get("fullname") log = Log.create( application_name, sender, self.request.get("body"), email=self.request.params.get("email"), facebookId=self.request.params.get("facebookId"), fullname=self.request.params.get("fullname"), username=self.request.params.get("username")) log.links = list(enum_request_files(log, self.get_file_infos())) log.put()
def emit(self, record): trace = "" exc = record.__dict__['exc_info'] if exc: trace = traceback.format_exc().replace("\n", " ") log = Log.create(logger=record.__dict__['name'], level=record.__dict__['levelname'], trace=trace, msg=record.__dict__['msg'])
def log_in(request): if request.user.is_authenticated(): return render_to_response('dashboard.html', {'user': request.user}) state = "" username = password = '' if request.POST: username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) state = "Login ok!" Log.create(user, "Logged in", None, None).save() return HttpResponseRedirect("/dashboard/") else: state = "Account not active." else: state = "* Wrong username or password." return render_to_response('login.html',RequestContext(request, {'state':state}))
def change(request, document_number): doc= Dokument.objects.get(id=int(document_number)) prevfaculty = doc.faculty users_list= Faculty.objects.all() if request.method=='POST': faculty= request.POST.get('faculty') current_faculty = faculty faculty_name= faculty faculty= faculty.replace(',', "") for person in users_list: if faculty.split(' ')[0]==person.last_name: for k in faculty.split(' '): if k== person.last_name: continue elif k not in person.first_name: break doc.faculty_id=person.id doc.save() Log.create(request.user, "Changed owenership of a record owned by " + str(prevfaculty) + " to " + str(current_faculty), None, doc).save() return HttpResponseRedirect("/records") else: print 'hi' faculty_list= Faculty.objects.all() return render_to_response('change.html', {'doc':doc, 'faculty_list':faculty_list})
def store_email(cls, app_name, body): message = mail.InboundEmailMessage(body) message_id = message.original.get("Message-Id") parent_id = message.original.get("In-Reply-To") _, body = message.bodies("text/plain").next() created_at = parser.parse(message.date) created_at = created_at.replace(tzinfo=None) log = Log.create( app_name, message.sender, body, created_at=created_at, message_id=message_id, parent_id=parent_id) log.links = list(enum_message_files(log, message)) log.put()
def compare_logging_method_calls(head_commit_db, file_diff, method_name, logging_method_calls_in_parent, logging_method_calls_in_head, src, dst, component): method_mapping_list = [] method_added_logging_loc = 0 method_deleted_logging_loc = 0 method_updated_logging_loc = 0 # Add index to make each call unique. method_calls_str_parent = \ [str(index) + '#' + etree.tostring(call).decode('utf-8') for index, call in enumerate(logging_method_calls_in_parent)] method_calls_str_head = \ [str(index) + '#' + etree.tostring(call).decode('utf-8') for index, call in enumerate(logging_method_calls_in_head)] for call_str_in_parent in method_calls_str_parent: for call_str_in_head in method_calls_str_head: distance_ratio = Levenshtein.ratio( c_transformation.get_string_xml_vars( etree.fromstring(remove_hash(call_str_in_parent)), src), c_transformation.get_string_xml_vars( etree.fromstring(remove_hash(call_str_in_head)), dst)) if distance_ratio > config.LEVENSHTEIN_RATIO_THRESHOLD: is_parent_in_mapping = False # Check mapping list for mapping in method_mapping_list: call_mapping_parent = mapping[0] mapping_ratio = mapping[2] if call_str_in_parent == call_mapping_parent: is_parent_in_mapping = True if distance_ratio > mapping_ratio: mapping[1] = call_str_in_head mapping[2] = Levenshtein.ratio( c_transformation.get_string_xml_vars( etree.fromstring( remove_hash(call_str_in_parent)), src), c_transformation.get_string_xml_vars( etree.fromstring( remove_hash(call_str_in_head)), dst)) if not is_parent_in_mapping: is_head_in_mapping = False for mapping in method_mapping_list: call_mapping_head = mapping[1] mapping_ratio = mapping[2] if call_str_in_head == call_mapping_head: is_head_in_mapping = True if distance_ratio > mapping_ratio: mapping[0] = call_str_in_parent mapping[2] = Levenshtein.ratio( c_transformation.get_string_xml_vars( etree.fromstring( remove_hash(call_str_in_parent)), src), c_transformation.get_string_xml_vars( etree.fromstring( remove_hash(call_str_in_head)), dst)) if not is_head_in_mapping: method_mapping_list.append([ str(call_str_in_parent), str(call_str_in_head), distance_ratio ]) method_calls_mapping_in_parent = [] method_calls_mapping_in_head = [] for mapping in method_mapping_list: change_from = etree.fromstring(remove_hash(mapping[0])) change_to = etree.fromstring(remove_hash(mapping[1])) method_calls_mapping_in_parent.append( str(c_transformation.get_string_xml_vars(change_from, src).encode(), encoding='unicode_escape')) method_calls_mapping_in_head.append( str(c_transformation.get_string_xml_vars(change_to, dst).encode(), encoding='unicode_escape')) before = [] after = [] for m in method_calls_str_parent: b = etree.fromstring(remove_hash(m)) before.append( str(c_transformation.get_string_xml_vars(b, src).encode(), encoding='unicode_escape')) for m in method_calls_str_head: a = etree.fromstring(remove_hash(m)) after.append( str(c_transformation.get_string_xml_vars(a, dst).encode(), encoding='unicode_escape')) deleted_logging_calls_str = list( set(before) - set(method_calls_mapping_in_parent)) added_logging_calls_str = list( set(after) - set(method_calls_mapping_in_head)) if added_logging_calls_str or deleted_logging_calls_str: print("Added Calls : ") print(added_logging_calls_str) print("Deleted Calls :") print(deleted_logging_calls_str) method_deleted_logging_loc += len(deleted_logging_calls_str) method_added_logging_loc += len(added_logging_calls_str) for call_str in deleted_logging_calls_str: save_logs_of_logging_call_xml(call_str, LogChangeType.DELETED_INSIDE_METHOD, file_diff.a_path, head_commit_db, method_name, component) for call_str in added_logging_calls_str: save_logs_of_logging_call_xml(call_str, LogChangeType.ADDED_INSIDE_METHOD, file_diff.b_path, head_commit_db, method_name, component) for mapping in method_mapping_list: change_from = etree.fromstring(remove_hash(mapping[0])) change_to = etree.fromstring(remove_hash(mapping[1])) if c_transformation.get_string_xml_vars( change_from, src) != c_transformation.get_string_xml_vars( change_to, dst): print("Change from : ") print(c_transformation.get_string_xml_vars(change_from, src)) print("Change to : ") print(c_transformation.get_string_xml_vars(change_to, dst)) update_type = None method_updated_logging_loc += 1 log = Log.create( commit_id=head_commit_db.hexsha, file_path=file_diff.b_path, embed_method=method_name, change_type=LogChangeType.UPDATED, content_update_from=c_transformation.get_string_xml_vars( change_from, src), content_update_to=c_transformation.get_string_xml_vars( change_to, dst), update_type=update_type, component=component) log.update_type = c_metrics.get_log_update_detail( change_from, change_to, src, dst) print(log.update_type) old_caller_object = c_transformation.get_method_call_name( change_from) new_caller_object = c_transformation.get_method_call_name( change_to) verb = re.compile( "KERN_EMERG|KERN_ALERT|KERN_CRIT|KERN_ERR|KERN_WARNING|KERN_NOTICE|KERN_INFO|KERN_DEBUG|KERN_CONT" ) if old_caller_object != new_caller_object: log.old_log_method = old_caller_object log.new_log_method = new_caller_object else: old_match = re.search( verb, c_transformation.get_string_xml_vars(change_from, src)) new_match = re.search( verb, c_transformation.get_string_xml_vars(change_to, dst)) if old_match and new_match: if old_match.group(0) != new_match.group(0): log.old_verb = old_match.group(0) log.new_verb = new_match.group(0) else: if old_match or new_match: if old_match: log.old_verb = old_match.group(0) if new_match: log.new_verb = new_match.group(0) log.save() return method_added_logging_loc, method_deleted_logging_loc, method_updated_logging_loc
def log_out(request): Log.create(request.user, "Logged out", None, None).save() logout(request) return HttpResponseRedirect('/')
def restore(request, file_number): file = File.objects.get(id=int(file_number)) file.trashed = False file.save() Log.create(request.user, "Restored a file", file, None).save() return HttpResponseRedirect("/trash/")
def _save(self): Log.create(**self.__dict__)
def create_log_entry(category, method, user): log = Log.create(category=category, method=method, user=user) return jsonify({"status": "success"}), 200