Best Python code snippet using avocado_python
views_organizer.py
Source:views_organizer.py
1# -*- coding: utf-8 -*-2from django.shortcuts import get_object_or_404, render, redirect3from django.db.models.query import Prefetch4from django.contrib import messages5from django.db.models import Count6from django.db import transaction7import logging8from results import models9from editor import forms10from results.views.views_common import user_edit_vars11from .views_user_actions import log_form_change12from .views_common import group_required, changes_history, update_organizer13from tools.flock_mutex import Flock_mutex14from starrating.constants import LOCK_FILE_FOR_RATED_TREE_MODIFICATIONS15from starrating.aggr.rated_tree_modifications import change_parent, transfer_children_before_node_deletion16from starrating.exceptions import UpdatedRecordExistsError17@group_required('admins')18def organizer_details(request, organizer_id=None):19 if organizer_id:20 organizer = get_object_or_404(models.Organizer, pk=organizer_id)21 str_action = u'обновлÑн'22 else:23 organizer = models.Organizer(created_by=request.user)24 str_action = u'Ñоздан'25 if 'frmOrganizer_submit' in request.POST:26 form = forms.OrganizerForm(request.POST, instance=organizer, files=request.FILES)27 if form.is_valid():28 form.save()29 log_form_change(request.user, form, action=models.ACTION_UPDATE if organizer_id else models.ACTION_CREATE)30 messages.success(request, u'ÐÑганизаÑÐ¾Ñ ÑÑпеÑно {}. ÐÑовеÑÑÑе, вÑÑ Ð»Ð¸ пÑавилÑно.'.format(str_action))31 return redirect(organizer.get_editor_url())32 else:33 messages.warning(request, u'ÐÑганизаÑÐ¾Ñ Ð½Ðµ {}. ÐожалÑйÑÑа, иÑпÑавÑÑе оÑибки в ÑоÑме.'.format(str_action))34 else:35 form = forms.OrganizerForm(instance=organizer)36 context = {}37 context['organizer'] = organizer38 context['form'] = form39 return render(request, 'editor/organizer_details.html', context)40@group_required('admins')41def organizer_changes_history(request, organizer_id):42 organizer = get_object_or_404(models.Organizer, pk=organizer_id)43 context = user_edit_vars(request.user)44 return changes_history(request, context, organizer, organizer.get_absolute_url())45@group_required('admins')46def organizer_delete(request, organizer_id):47 organizer = get_object_or_404(models.Organizer, pk=organizer_id)48 has_dependent_objects = organizer.has_dependent_objects()49 ok_to_delete = False50 if 'frmForOrganizer_submit' in request.POST:51 form = forms.ForOrganizerForm(request.POST, auto_id='frmForOrganizer_%s')52 if form.is_valid():53 if has_dependent_objects:54 organizer_for = form.cleaned_data['organizer']55 if organizer_for != organizer:56 ok_to_delete = True57 else:58 messages.warning(request, u'ÐелÑÐ·Ñ Ð·Ð°Ð¼ÐµÐ½Ð¸ÑÑ Ð¾ÑганизаÑоÑа на его Ñамого.')59 else: # There are no dependent objects of the organizer, so we just delete it60 ok_to_delete = True61 else:62 messages.warning(request, u'ÐÑганизаÑÐ¾Ñ Ð½Ðµ ÑдалÑн. ÐожалÑйÑÑа, иÑпÑавÑÑе оÑибки в ÑоÑме.')63 else:64 form = None65 messages.warning(request, u'ÐÑ Ð½Ðµ Ñказали гоÑод Ð´Ð»Ñ ÑдалениÑ.')66 if ok_to_delete:67 # NOT TESTED !68 if has_dependent_objects:69 organizer_for_id = organizer_for.id70 else:71 organizer_for_id = 072 organizer_for = None73 log = logging.getLogger('structure_modification')74 log_prefix = 'organizer_delete: organizer {}->{}, by user {}.'.format(75 organizer_id, organizer_for_id, request.user.id76 )77 log_exc_info = False78 oranizer_name_full = organizer.name_full()79 log.debug('{} before flock'.format(log_prefix))80 with Flock_mutex(LOCK_FILE_FOR_RATED_TREE_MODIFICATIONS):81 try:82 with transaction.atomic():83 if has_dependent_objects:84 update_organizer(request, organizer, organizer_for)85 models.log_obj_delete(request.user, organizer)86 organizer.delete()87 log.debug('{} trnsctn end'.format(log_prefix))88 except (UpdatedRecordExistsError, AssertionError) as e:89 error_msg = repr(e)90 if isinstance(e, AssertionError):91 log_exc_info = True92 except Exception as e:93 log.error('{} Unexpected error: {}'.format(log_prefix, repr(e)), exc_info=True)94 raise95 else:96 error_msg = None97 if error_msg is None:98 log.info('{} OK'.format(log_prefix))99 messages.success(request, u'ÐÑганизаÑÐ¾Ñ Â«{}» ÑÑпеÑно ÑдалÑн.'.format(organizer_name_full))100 else:101 log.error('{} {}'.format(log_prefix, error_msg), exc_info=log_exc_info)102 messages.warning(103 request, u'Ðе ÑдалоÑÑ ÑдалиÑÑ Ð¾ÑганизаÑоÑа «{}» ({}).'.format(104 organizer_name_full, error_msg105 )106 )107 if has_dependent_objects:108 return redirect(organizer_for.get_editor_url())109 else:110 return redirect('editor:organizer_create')111 return organizer_details(request, organizer_id=organizer_id)112@group_required('admins')113def add_series(request, organizer_id):114 organizer = get_object_or_404(models.Organizer, pk=organizer_id)115 if 'select_series' in request.POST:116 series_id = models.int_safe(request.POST['select_series'])117 series = get_object_or_404(models.Series, pk=series_id)118 if series.organizer_id != models.FAKE_ORGANIZER_ID:119 messages.warning(request, u'У ÑеÑии {} (id {}) Ñже Ñказан оÑганизаÑÐ¾Ñ {} (id {}).'.format(120 series.name, series_id, series.organizer.name, series.organizer.id))121 else:122 log = logging.getLogger('structure_modification')123 log_prefix = 'add_series {} to organizer {} by user {}.'.format(series.id, organizer_id, request.user.id)124 log.debug('{} before flock'.format(log_prefix))125 log_exc_info = False126 with Flock_mutex(LOCK_FILE_FOR_RATED_TREE_MODIFICATIONS):127 log.debug('{} trnsctn start'.format(log_prefix))128 try:129 with transaction.atomic():130 change_parent(series, organizer) # to adapt the starrating data)131 series.organizer = organizer132 series.save()133 models.log_obj_create(request.user, series, models.ACTION_UPDATE, field_list=['organizer'])134 log.debug('{} trnsctn end'.format(log_prefix))135 except (UpdatedRecordExistsError, AssertionError) as e:136 error_msg = repr(e)137 if isinstance(e, AssertionError):138 log_exc_info = True139 else:140 error_msg = None141 if error_msg is None:142 log.info('{} OK'.format(log_prefix))143 messages.success(request, u'СеÑÐ¸Ñ Â«{}» (id {}) ÑÑпеÑно добавлена ÑÑÐ¾Ð¼Ñ Ð¾ÑганизаÑоÑÑ.'.format(series.name, series_id))144 else:145 log.error('{} {}'.format(log_prefix, error_msg), exc_info=log_exc_info)146 messages.warning(request, u'Ðе ÑдалоÑÑ Ð´Ð¾Ð±Ð°Ð²Ð¸ÑÑ ÑеÑÐ¸Ñ Â«{}» (id {}) ÑÑÐ¾Ð¼Ñ Ð¾ÑганизаÑоÑÑ ({}).'.format(series.name, series_id, error_msg))147 return redirect(organizer)148@group_required('admins')149def remove_series(request, organizer_id, series_id):150 organizer = get_object_or_404(models.Organizer, pk=organizer_id)151 if 'btnRemoveSeries' in request.POST:152 series = get_object_or_404(models.Series, pk=series_id)153 if series.organizer != organizer:154 messages.warning(request, u'У ÑеÑии {} (id {}) и Ñак не Ñказан оÑганизаÑÐ¾Ñ {} (id {}).'.format(155 series.name, series_id, series.organizer.name, series.organizer.id))156 else:157 log = logging.getLogger('structure_modification')158 log_prefix = 'remove_series {} from organizer {} by user {}.'.format(series.id, organizer_id, request.user.id)159 log.debug('{} before flock'.format(log_prefix))160 log_exc_info = False161 with Flock_mutex(LOCK_FILE_FOR_RATED_TREE_MODIFICATIONS):162 log.debug('{} trnsctn start'.format(log_prefix))163 try:164 with transaction.atomic():165 change_parent(series, models.Organizer.objects.fake_object)166 # ^ to adapt the starrating data) ^167 series.organizer_id = models.FAKE_ORGANIZER_ID168 series.save()169 models.log_obj_create(request.user, series, models.ACTION_UPDATE, field_list=['organizer'])170 log.debug('{} trnsctn end'.format(log_prefix))171 except (UpdatedRecordExistsError, AssertionError) as e:172 error_msg = repr(e)173 if isinstance(e, AssertionError):174 log_exc_info = True175 else:176 error_msg = None177 if error_msg is None:178 log.info('{} OK'.format(log_prefix))179 messages.success(request, u'СеÑÐ¸Ñ Â«{}» (id {}) ÑÑпеÑно оÑвÑзана Ð¾Ñ ÑÑого оÑганизаÑоÑа.'.format(series.name, series_id))180 else:181 log.error('{} {}'.format(log_prefix, error_msg), exc_info=log_exc_info)182 messages.warning(request, u'Ðе ÑдалоÑÑ Ð¾ÑвÑзаÑÑ ÑеÑÐ¸Ñ Â«{}» (id {}) Ð¾Ñ ÑÑого оÑганизаÑоÑа ({}).'.format(series.name, series_id, error_msg))...
_logger.py
Source:_logger.py
1""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine2 )3 Copyright 2003 Paul Scott-Murphy, 2014 William McBrine45 This module provides a framework for the use of DNS Service Discovery6 using IP multicast.78 This library is free software; you can redistribute it and/or9 modify it under the terms of the GNU Lesser General Public10 License as published by the Free Software Foundation; either11 version 2.1 of the License, or (at your option) any later version.1213 This library is distributed in the hope that it will be useful,14 but WITHOUT ANY WARRANTY; without even the implied warranty of15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU16 Lesser General Public License for more details.1718 You should have received a copy of the GNU Lesser General Public19 License along with this library; if not, write to the Free Software20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-130121 USA22"""2324import logging25import sys26from typing import Any, Dict, Union, cast2728log = logging.getLogger(__name__.split('.', maxsplit=1)[0])29log.addHandler(logging.NullHandler())303132def set_logger_level_if_unset() -> None:33 if log.level == logging.NOTSET:34 log.setLevel(logging.WARN)353637set_logger_level_if_unset()383940class QuietLogger:41 _seen_logs: Dict[str, Union[int, tuple]] = {}4243 @classmethod44 def log_exception_warning(cls, *logger_data: Any) -> None:45 exc_info = sys.exc_info()46 exc_str = str(exc_info[1])47 if exc_str not in cls._seen_logs:48 # log at warning level the first time this is seen49 cls._seen_logs[exc_str] = exc_info50 logger = log.warning51 else:52 logger = log.debug53 logger(*(logger_data or ['Exception occurred']), exc_info=True)5455 @classmethod56 def log_exception_debug(cls, *logger_data: Any) -> None:57 log_exc_info = False58 exc_info = sys.exc_info()59 exc_str = str(exc_info[1])60 if exc_str not in cls._seen_logs:61 # log the trace only on the first time62 cls._seen_logs[exc_str] = exc_info63 log_exc_info = True64 log.debug(*(logger_data or ['Exception occurred']), exc_info=log_exc_info)6566 @classmethod67 def log_warning_once(cls, *args: Any) -> None:68 msg_str = args[0]69 if msg_str not in cls._seen_logs:70 cls._seen_logs[msg_str] = 071 logger = log.warning72 else:73 logger = log.debug74 cls._seen_logs[msg_str] = cast(int, cls._seen_logs[msg_str]) + 175 logger(*args)7677 @classmethod78 def log_exception_once(cls, exc: Exception, *args: Any) -> None:79 msg_str = args[0]80 if msg_str not in cls._seen_logs:81 cls._seen_logs[msg_str] = 082 logger = log.warning83 else:84 logger = log.debug85 cls._seen_logs[msg_str] = cast(int, cls._seen_logs[msg_str]) + 1
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!