def create_object(self, data: dict): course_mapper = MapperRegistry.get_current_mapper('course') try: name = data['name'] name = website_engine.decode_value(name) validator.word_validation(name) if course_mapper.check_course_exists(course_name=name): self.validation_error = f'Course with this name: {name} already exists' raise ValidationException(f'Course with this name: {name} already exists') location = data['location'] location = website_engine.decode_value(location) validator.word_validation(location) start_date = data['start_date'] start_date = website_engine.decode_value(start_date) validator.date_validation(start_date) category_name = data['category'] mapper = MapperRegistry.get_current_mapper('category') category = mapper.get_category_by_name(category_name=category_name) course = website_engine.create_course(type_='video', name=name, category=category, location=location, start_date=start_date) category.courses.append(course) category.mark_dirty() course.mark_new() UnitOfWork.get_thread().commit() logger.log(f'Course {course} has been successfully created') self.success_text = f'Successfully added course {course}!' course.observers.append(email_notifier) course.observers.append(sms_notifier) except ValidationException as e: logger.log(f'Validation failed: {e}') self.validation_error = e
def delete_object(self, data: dict): try: course_name = data['course_name'] course_mapper = MapperRegistry.get_current_mapper('course') course = course_mapper.get_course_by_name(course_name) course.mark_removed() UnitOfWork.get_thread().commit() logger.log(f'Successfully deleted course {course_name}!') except Exception as e: logger.log(f'An error occurred: {e}') self.error_message = e
def delete_object(self, data: dict): try: student_name = data['student_name'] student_mapper = MapperRegistry.get_current_mapper('student') student = student_mapper.get_student_by_name(student_name) student.mark_removed() UnitOfWork.get_thread().commit() logger.log(f'Successfully deleted student {student_name}!') except Exception as e: logger.log(f'An error occurred: {e}') self.error_message = e
def update_object(self, data: dict): try: email = data['email'] validator.email_validation(email) student_name = data['student_name'] student_name = website_engine.decode_value(student_name) mapper = MapperRegistry.get_current_mapper('student') student = mapper.get_student_by_name(student_name) student.update_email(email) student.mark_dirty() UnitOfWork.get_thread().commit() logger.log(f'Updated student is: {student}') except ValidationException as e: self.error_message = e logger.log(f'An error occurred: {self.error_message}')
def update_object(self, data: dict): try: course_name = data['course_name'] course_mapper = MapperRegistry.get_current_mapper('course') course = course_mapper.get_course_by_name(course_name) location = data['location'] validator.word_validation(location) start_date = data['start_date'] validator.date_validation(start_date) course.update_location(location) course.update_start_date(start_date) course.mark_dirty() UnitOfWork.get_thread().commit() except ValidationException as e: self.error_message = e logger.log(f'An error occurred: {self.error_message}')
def create_object(self, data: dict): try: category_name = data['name'] category_name = website_engine.decode_value(category_name) validator.word_validation(category_name) mapper = MapperRegistry.get_current_mapper('category') if mapper.check_category_exists(category_name=category_name): self.validation_error = f'Category with this name: {category_name} already exists' raise ValidationException(self.validation_error) category_id = randint(1000, 5000) category = None new_category = website_engine.create_category(name=category_name, category=category, category_id=category_id) new_category.mark_new() UnitOfWork.get_thread().commit() logger.log(f'Successfully added new category {new_category}') except ValidationException as e: logger.log(f'{e}') self.validation_error = e
def create_object(self, data: dict): try: name = data['name'] name = website_engine.decode_value(name) validator.word_validation(name) email, role = data['email'], data['role'] validator.email_validation(email) mapper = MapperRegistry.get_current_mapper('student') if mapper.check_student_exists(student_name=name): self.error_message = f'Student with this name "{name}" already exists' raise ValidationException(self.error_message) if role == 'student': user = website_engine.create_user(type_=role, name=name, email=email) user.mark_new() UnitOfWork.get_thread().commit() elif role == 'teacher': user = website_engine.create_user(type_=role, name=name, email=email) website_engine.teachers.append(user) logger.log(website_engine.teachers) except ValidationException as e: self.error_message = e logger.log(f'Validation error occurred: {e}')
def __call__(self, request): request_params = request['request_params'] try: name = request_params['name'] logger.log(f'copying the course {name}') mapper = MapperRegistry.get_current_mapper('course') existing_course = mapper.get_course_by_name(name) if existing_course: new_name = f'{name}_copy' new_course = existing_course.clone() new_course.name = new_name new_course.mark_new() UnitOfWork.get_thread().commit() courses = mapper.all() logger.log(f"new list of courses is {courses}") courses_count = [len(courses)] return '200 OK', render('courses.html', objects_list=courses, courses_count=courses_count) except IntegrityError: return '400 NOK', render('error_page.html', error_text=f'Unable to copy course: course with this name already exists') except Exception as e: return '400 NOK', render('error_page.html', error_text=f'Unable to copy course: {e}')
def create_object(self, data): name = site.decode_value(data['name']) new_obj = site.create_user('student', name) site.students.append(new_obj) new_obj.mark_as_new() UnitOfWork.get_current().commit()
from datetime import date from lo_framework.lo_templator import render from patterns.behavioral import BaseSerializer, CreateView, EMAILNotifier, SMSNotifier, ListView from patterns.creational import Engine, Logger, MapperRegistry from patterns.structural import RouteDecorator, DebugDecorator from patterns.unit_of_work import UnitOfWork site = Engine() logger = Logger('main') email_notifier = EMAILNotifier() sms_notifier = SMSNotifier() UnitOfWork.new_current() UnitOfWork.get_current().set_mapper_registry(MapperRegistry) routes = {} @RouteDecorator(routes=routes, url='/') class Index: @DebugDecorator(name='Index') def __call__(self, request): return '200 OK', render('index.html', object_list=site.categories) # return '200 OK', render('index.html', data=request.get('data', None)) @RouteDecorator(routes=routes, url='/about/') class About: @DebugDecorator(name='About') def __call__(self, request): return '200 OK', render('about.html', data=request.get('data', None))
from patterns.engine import Engine from patterns.validators import Validator, ValidationException from patterns.logger import Logger from patterns.mapper_registry import MapperRegistry from patterns.decorators import timer, app_route from patterns.views_templates import ListView, CreateView, UpdateView, DeleteView from patterns.api_module import BaseSerializer from patterns.updater import EmailNotify, SMSNotify from patterns.unit_of_work import UnitOfWork website_engine = Engine() logger = Logger('views') email_notifier = EmailNotify() sms_notifier = SMSNotify() validator = Validator() UnitOfWork.new_thread() UnitOfWork.get_thread().set_mapper_registry(MapperRegistry) routes = {} @app_route(routes=routes, url='/') class Index: @timer(name="Index") def __call__(self, request): return '200 OK', render('index.html', date=date.today()) @app_route(routes=routes, url='/categories/') class Categories(ListView): template_name = 'categories.html'