def list(request): """List all crawlers and link to their log files. :param request: the current HTTP Request object :type request: HttpRequest :return: the HTTP response object :rtype: HttpResponse """ try: logs_whitelist = settings.CRAWLER_LOGS_WHITELIST except AttributeError: logs_whitelist = set() crawlers = [] for process_class in permissions.get_all_addable_physical_process_models().keys(): if process_class.__name__ in logs_whitelist or \ permissions.has_permission_to_add_physical_process(request.user, process_class): process_class_name = camel_case_to_underscores(process_class.__name__) filepath = os.path.join(settings.CRAWLER_LOGS_ROOT, process_class_name + ".log") if os.path.exists(filepath): crawlers.append((process_class._meta.verbose_name_plural, process_class_name)) crawlers.sort() return render(request, "samples/list_crawlers.html", {"title": _("Crawler logs"), "crawlers": crawlers})
def list(request): """List all crawlers and link to their log files. :param request: the current HTTP Request object :type request: HttpRequest :return: the HTTP response object :rtype: HttpResponse """ try: logs_whitelist = settings.CRAWLER_LOGS_WHITELIST except AttributeError: logs_whitelist = set() crawlers = [] for process_class in permissions.get_all_addable_physical_process_models( ).keys(): if process_class.__name__ in logs_whitelist or \ permissions.has_permission_to_add_physical_process(request.user, process_class): process_class_name = camel_case_to_underscores( process_class.__name__) filepath = os.path.join(settings.CRAWLER_LOGS_ROOT, process_class_name + ".log") if os.path.exists(filepath): crawlers.append((process_class._meta.verbose_name_plural, process_class_name)) crawlers.sort() return render(request, "samples/list_crawlers.html", { "title": _("Crawler logs"), "crawlers": crawlers })
def view(request, process_class_name): """View for log files of crawlers. :param request: the current HTTP Request object :param process_class_name: the name of the crawler whose log file is about to be shown :type request: HttpRequest :type process_class_name: unicode :return: the HTTP response object :rtype: HttpResponse """ for process_class in permissions.get_all_addable_physical_process_models().keys(): if camel_case_to_underscores(process_class.__name__) == process_class_name: break else: raise Http404("Process class not found.") try: logs_whitelist = settings.CRAWLER_LOGS_WHITELIST except AttributeError: logs_whitelist = set() if process_class.__name__ not in logs_whitelist: permissions.assert_can_add_physical_process(request.user, process_class) assert "." not in process_class_name and "/" not in process_class_name filepath = os.path.join(settings.CRAWLER_LOGS_ROOT, process_class_name + ".log") log_content, log_timestamp = read_crawler_log(filepath) return render(request, "samples/log_viewer.html", {"title": _("Log of crawler “{process_class_name}”").format( process_class_name=process_class._meta.verbose_name_plural), "log_content": log_content, "log_timestamp": log_timestamp})
def __init__(self, user, data=None, **kwargs): self.task = kwargs.get("instance") self.user = user self.fixed_fields = set() if self.task: eligible_operators = set(permissions.get_all_adders(self.task.process_class.model_class())) if self.task.operator: eligible_operators.add(self.task.operator) self.fixed_fields.add("process_class") if self.task.status == "1 new": self.fixed_fields.add("finished_process") if self.user not in eligible_operators: self.fixed_fields.update(["operator", "status"]) elif self.task.status in ["2 accepted", "3 in progress"]: if self.user != self.task.operator: self.fixed_fields.update(["status", "priority", "finished_process", "operator"]) if self.user != self.task.customer: self.fixed_fields.add("comments") else: self.fixed_fields.update(["priority", "finished_process", "operator"]) if self.user != self.task.operator: self.fixed_fields.add("status") if self.user != self.task.customer: self.fixed_fields.add("comments") else: self.fixed_fields.update(["status", "finished_process", "operator"]) if data is not None: data = data.copy() if self.task: data.update(forms.model_to_dict(self.task, self.fixed_fields)) else: initial = kwargs.get("initial", {}) initial.update({"status": "1 new"}) data.update(initial) super(TaskForm, self).__init__(data, **kwargs) self.fields["customer"].required = False self.fields["operator"].choices = [("", "---------")] if self.task: self.fields["operator"].choices.extend((user.pk, common_utils.get_really_full_name(user)) for user in common_utils.sorted_users_by_first_name(eligible_operators)) self.fields["process_class"].choices = utils.choices_of_content_types( permissions.get_all_addable_physical_process_models()) self.fields["finished_process"].choices = [("", "---------")] if self.task: old_finished_process_id = self.task.finished_process.id if self.task.finished_process else None if self.user == self.task.operator: self.fields["finished_process"].choices.extend( [(process.id, process.actual_instance) for process in Process.objects.filter( Q(operator=self.user, content_type=self.task.process_class) | Q(id=old_finished_process_id)).filter(finished=True).order_by("-timestamp")[:10]]) elif old_finished_process_id: self.fields["finished_process"].choices.append((old_finished_process_id, self.task.finished_process)) self.fields["comments"].widget.attrs["cols"] = 30 self.fields["comments"].widget.attrs["rows"] = 5 for field_name in self.fixed_fields: self.fields[field_name].widget.attrs["disabled"] = "disabled" self.fields[field_name].required = False
def __init__(self, user, *args, **kwargs): super(StatusForm, self).__init__(*args, **kwargs) self.user = user self.fields["operator"].set_operator(user, user.is_superuser) self.fields["operator"].initial = user.pk self.fields["timestamp"].initial = datetime.datetime.now() self.fields["process_classes"].choices = utils.choices_of_content_types( cls for cls in get_all_addable_physical_process_models() if self.is_editable(cls)) self.fields["process_classes"].widget.attrs["size"] = 24
def __init__(self, user, *args, **kwargs): super(StatusForm, self).__init__(*args, **kwargs) self.user = user self.fields["operator"].set_operator(user, user.is_staff) self.fields["operator"].initial = user.pk self.fields["timestamp"].initial = datetime.datetime.now() self.fields[ "process_classes"].choices = utils.choices_of_content_types( cls for cls in get_all_addable_physical_process_models() if self.is_editable(cls)) self.fields["process_classes"].widget.attrs["size"] = 24
def __init__(self, user, data=None, **kwargs): super(ChooseTaskListsForm, self).__init__(data, **kwargs) choices = [] for department in user.samples_user_details.show_users_from_departments.iterator(): process_from_department = set(process for process in permissions.get_all_addable_physical_process_models().keys() if process._meta.app_label == department.app_label) choices.append((department.name, utils.choices_of_content_types(process_from_department))) if len(choices) == 1: choices = choices[0][1] if not choices: choices = (("", 9 * "-"),) self.fields["visible_task_lists"].choices = choices self.fields["visible_task_lists"].initial = [content_type.id for content_type in user.samples_user_details.visible_task_lists.iterator()] self.fields["visible_task_lists"].widget.attrs["size"] = "15"
def view(request, process_class_name): """View for log files of crawlers. :param request: the current HTTP Request object :param process_class_name: the name of the crawler whose log file is about to be shown :type request: HttpRequest :type process_class_name: unicode :return: the HTTP response object :rtype: HttpResponse """ for process_class in permissions.get_all_addable_physical_process_models( ).keys(): if camel_case_to_underscores( process_class.__name__) == process_class_name: break else: raise Http404("Process class not found.") try: logs_whitelist = settings.CRAWLER_LOGS_WHITELIST except AttributeError: logs_whitelist = set() if process_class.__name__ not in logs_whitelist: permissions.assert_can_add_physical_process(request.user, process_class) assert "." not in process_class_name and "/" not in process_class_name filepath = os.path.join(settings.CRAWLER_LOGS_ROOT, process_class_name + ".log") log_content, log_timestamp = read_crawler_log(filepath) return render( request, "samples/log_viewer.html", { "title": _("Log of crawler “{process_class_name}”").format( process_class_name=process_class._meta.verbose_name_plural), "log_content": log_content, "log_timestamp": log_timestamp })
def __init__(self, user, data=None, **kwargs): super(ChooseTaskListsForm, self).__init__(data, **kwargs) choices = [] for department in user.samples_user_details.show_users_from_departments.iterator( ): process_from_department = set( process for process in permissions.get_all_addable_physical_process_models().keys() if process._meta.app_label == department.app_label) choices.append( (department.name, utils.choices_of_content_types(process_from_department))) if len(choices) == 1: choices = choices[0][1] if not choices: choices = (("", 9 * "-"), ) self.fields["visible_task_lists"].choices = choices self.fields["visible_task_lists"].initial = [ content_type.id for content_type in user.samples_user_details.visible_task_lists.iterator() ] self.fields["visible_task_lists"].widget.attrs["size"] = "15"
def __init__(self, user, *args, **kwargs): super(UserDetailsForm, self).__init__(*args, **kwargs) self.fields["auto_addition_topics"].queryset = user.topics choices = [] processes = [process_class for process_class in jb_common_utils.get_all_models().values() if issubclass(process_class, models.Process) and not process_class._meta.abstract and process_class not in [models.Process, models.Deposition]] for department in user.samples_user_details.show_users_from_departments.iterator(): process_from_department = set(process for process in processes if process._meta.app_label == department.app_label) choices.append((department.name, utils.choices_of_content_types(process_from_department))) if not choices: choices = (("", 9 * "-"),) self.fields["default_folded_process_classes"].choices = choices self.fields["default_folded_process_classes"].initial = [content_type.id for content_type in user.samples_user_details.default_folded_process_classes.iterator()] self.fields["default_folded_process_classes"].widget.attrs["size"] = "15" self.fields["subscribed_feeds"].choices = utils.choices_of_content_types( list(get_all_addable_physical_process_models()) + [models.Sample, models.SampleSeries, Topic]) self.fields["subscribed_feeds"].widget.attrs["size"] = "15" self.fields["show_users_from_departments"].choices = [(department.pk, department.name) for department in Department.objects.iterator()] self.fields["show_users_from_departments"].initial = \ user.samples_user_details.show_users_from_departments.values_list("id", flat=True)
def __init__(self, user, *args, **kwargs): super(UserDetailsForm, self).__init__(*args, **kwargs) self.fields["auto_addition_topics"].queryset = user.topics choices = [] processes = [process_class for process_class in jb_common_utils.get_all_models().values() if issubclass(process_class, models.Process) and not process_class._meta.abstract and process_class not in [models.Process, models.Deposition]] for department in user.samples_user_details.show_users_from_departments.iterator(): processes_from_department = set(process for process in processes if process._meta.app_label == department.app_label) choices.append((department.name, utils.choices_of_content_types(processes_from_department))) if not choices: choices = (("", 9 * "-"),) self.fields["default_folded_process_classes"].choices = choices self.fields["default_folded_process_classes"].initial = [content_type.id for content_type in user.samples_user_details.default_folded_process_classes.iterator()] self.fields["default_folded_process_classes"].widget.attrs["size"] = "15" self.fields["subscribed_feeds"].choices = utils.choices_of_content_types( list(get_all_addable_physical_process_models()) + [models.Sample, models.SampleSeries, Topic]) self.fields["subscribed_feeds"].widget.attrs["size"] = "15" self.fields["show_users_from_departments"].choices = [(department.pk, department.name) for department in Department.objects.iterator()] self.fields["show_users_from_departments"].initial = \ user.samples_user_details.show_users_from_departments.values_list("id", flat=True)
def __init__(self, user, data=None, **kwargs): self.task = kwargs.get("instance") self.user = user self.fixed_fields = set() if self.task: eligible_operators = set( permissions.get_all_adders( self.task.process_class.model_class())) if self.task.operator: eligible_operators.add(self.task.operator) self.fixed_fields.add("process_class") if self.task.status == "1 new": self.fixed_fields.add("finished_process") if self.user not in eligible_operators: self.fixed_fields.update(["operator", "status"]) elif self.task.status in ["2 accepted", "3 in progress"]: if self.user != self.task.operator: self.fixed_fields.update( ["status", "priority", "finished_process", "operator"]) if self.user != self.task.customer: self.fixed_fields.add("comments") else: self.fixed_fields.update( ["priority", "finished_process", "operator"]) if self.user != self.task.operator: self.fixed_fields.add("status") if self.user != self.task.customer: self.fixed_fields.add("comments") else: self.fixed_fields.update( ["status", "finished_process", "operator"]) if data is not None: data = data.copy() if self.task: data.update(forms.model_to_dict(self.task, self.fixed_fields)) else: initial = kwargs.get("initial", {}) initial.update({"status": "1 new"}) data.update(initial) super(TaskForm, self).__init__(data, **kwargs) self.fields["customer"].required = False self.fields["operator"].choices = [("", "---------")] if self.task: self.fields["operator"].choices.extend( (user.pk, common_utils.get_really_full_name(user)) for user in common_utils.sorted_users_by_first_name( eligible_operators)) self.fields["process_class"].choices = utils.choices_of_content_types( permissions.get_all_addable_physical_process_models()) self.fields["finished_process"].choices = [("", "---------")] if self.task: old_finished_process_id = self.task.finished_process.id if self.task.finished_process else None if self.user == self.task.operator: self.fields["finished_process"].choices.extend([( process.id, process.actual_instance ) for process in Process.objects.filter( Q(operator=self.user, content_type=self.task.process_class) | Q(id=old_finished_process_id)).filter( finished=True).order_by("-timestamp")[:10]]) elif old_finished_process_id: self.fields["finished_process"].choices.append( (old_finished_process_id, self.task.finished_process)) self.fields["comments"].widget.attrs["cols"] = 30 self.fields["comments"].widget.attrs["rows"] = 5 for field_name in self.fixed_fields: self.fields[field_name].disabled = True self.fields[field_name].required = False
def main_menu(request): """The main menu view. It displays the “My Samples” list in a dynamic way, and the actions that depend on the specific permissions a user has. The rest is served static. :param request: the current HTTP Request object :type request: HttpRequest :return: the HTTP response object :rtype: HttpResponse """ my_topics, topicless_samples = utils.build_structured_sample_list( request.user) allowed_physical_processes = permissions.get_allowed_physical_processes( request.user) lab_notebooks = [] for process_class, process in permissions.get_all_addable_physical_process_models( ).items(): try: url = django.core.urlresolvers.reverse( "lab_notebook_" + camel_case_to_underscores(process["type"]), kwargs={"year_and_month": ""}) except django.core.urlresolvers.NoReverseMatch: pass else: if permissions.has_permission_to_view_lab_notebook( request.user, process_class): lab_notebooks.append({ "label": process["label_plural"], "url": url }) if lab_notebooks: lab_notebooks.sort(key=lambda process: process["label"].lower()) return render( request, "samples/main_menu.html", { "title": _("Main menu"), "my_topics": my_topics, "topicless_samples": topicless_samples, "add_sample_url": django.core.urlresolvers.reverse(settings.ADD_SAMPLES_VIEW), "user_hash": permissions.get_user_hash(request.user), "can_add_topic": permissions.has_permission_to_edit_users_topics(request.user), "can_edit_topics": any( permissions.has_permission_to_edit_topic(request.user, topic) for topic in Topic.objects.all()), "can_add_external_operator": permissions.has_permission_to_add_external_operator(request.user), "has_external_contacts": request.user.external_contacts.exists() or (ExternalOperator.objects.exists() and request.user.is_superuser), "can_rename_samples": request.user.has_perm("samples.rename_samples") or request.user.is_superuser, "physical_processes": allowed_physical_processes, "lab_notebooks": lab_notebooks })