def add_node_view(request): if request.method != 'POST': form = NodeForm(initial={'parent': request.current_node}) current_language = get_language_from_request(request) formset = NodeLanguageVersionFormset( initial=[{ 'language': lang_short, 'DELETE': lang_short != current_language } for lang_short, lang_name in settings.LANGUAGES], queryset=NodeLanguageVersion.objects.none(), ) else: instance = Node(parent=request.current_node) form = NodeForm(request.POST, instance=instance) formset = NodeLanguageVersionFormset(request.POST) if form.is_valid(): node = form.save(commit=False) formset = NodeLanguageVersionFormset(request.POST, instance=node) if formset.is_valid(): node.save() formset.save() return redirect(portal_url(node=node)) return render(request, 'portals/add-node.html', { 'form': form, 'formset': formset, })
def handle(self, *args, **options): try: in_data = json.load(open(options['filename_json'], "r")) user_portal = Portal.objects.get(owner=(User.objects.get( username=options['username']) if options['username'] else None )) except IOError as e: raise CommandError(e.message) except ValueError: raise CommandError(_("Some lines of the file are not JSON")) except ObjectDoesNotExist: raise CommandError(_("User or portal not found")) root = Node(parent=user_portal.root) root.save() make_problemtree(in_data, root, self.stdout)
def handle(self, *args, **options): try: in_data = json.load(open(options['filename_json'], "r")) user_portal = \ Portal.objects.get( owner=(User.objects.get( username=options['username']) if options['username'] else None)) except IOError as e: raise CommandError(e.message) except ValueError: raise CommandError(_("Some lines of the file are not JSON")) except ObjectDoesNotExist: raise CommandError(_("User or portal not found")) root = Node(parent=user_portal.root) root.save() make_problemtree(in_data, root, self.stdout)
def add_node_view(request): if request.method != 'POST': form = NodeForm(initial={'parent': request.current_node}) else: instance = Node(parent=request.current_node) form = NodeForm(request.POST, instance=instance) if form.is_valid(): node = form.save() return redirect(portal_url(node=node)) return render(request, 'portals/add-node.html', {'form': form})
def handle(self, *args, **options): if len(args) < 1: raise CommandError(_("Expected at least one argument")) if len(args) > 2: raise CommandError(_("Expected two arguments at most")) try: in_data = json.load(open(args[0], "r")) user_portal = \ Portal.objects.get( owner=(User.objects.get( username=args[1]) if len(args) == 2 else None)) except IOError as e: raise CommandError(e.message) except ValueError: raise CommandError(_("Some lines of the file are not JSON")) except ObjectDoesNotExist: raise CommandError(_("User or portal not found")) root = Node(parent=user_portal.root) root.save() make_problemtree(in_data, root, self.stdout)
def make_problemtree(tree, root, stdout): for family in tree: family_node = Node(parent=root) family_node.short_name = family['name'] family_node.save() family_node_content = NodeLanguageVersion( node=family_node, language=settings.LANGUAGE_CODE) family_node_content.full_name = family['long_name'] family_node_content.panel_code = family['description'] family_node_content.save() for edition in family['editions']: edition_node = Node(parent=family_node) edition_node.short_name = edition['name'] edition_node.save() edition_node_content = NodeLanguageVersion( node=edition_node, language=settings.LANGUAGE_CODE) edition_node_content.full_name = edition['long_name'] try: edition_tag = Tag.objects.get( name=edition['tag']).problems.all() except ObjectDoesNotExist: stdout.write( 'Ignoring edition with tag {}'.format(edition['tag'])) edition_node_content.panel_code = '' edition_node_content.save() continue edition_node_content.panel_code = '' for header, tasks in edition['tasks']: l = [] for task in tasks: try: taskname_tag = Tag.objects.get( name=task).problems.all() try: task_obj = (edition_tag & taskname_tag)[0] except IndexError: stdout.write( 'Ignoring task: {} from {}, no such task in DB' .format(task, edition['tag'])) continue l.append(reverse('problem_site', kwargs={ 'site_key': task_obj.problemsite.url_key})) except ObjectDoesNotExist: stdout.write('Ignoring task: {} from {}, task\'s \ tag does not exist'.format(task, edition['tag'])) edition_node_content.panel_code += u'[[ProblemTable:{}|{}]]' \ u'\n\n'.format(header, u';'.join(l)) edition_node_content.save() root.short_name = u'problemset' root.save() root_content = NodeLanguageVersion( node=root, language=settings.LANGUAGE_CODE) root_content.full_name = u'Problem Tree' root_content.panel_code = u'All of the public tasks are (should be) here.' root_content.save()
def make_problemtree(tree, root, stdout): for family in tree: family_node = Node(parent=root) family_node.short_name = family['name'] family_node.save() family_node_content = NodeLanguageVersion( node=family_node, language=settings.LANGUAGE_CODE) family_node_content.full_name = family['long_name'] family_node_content.panel_code = family['description'] family_node_content.save() for edition in family['editions']: edition_node = Node(parent=family_node) edition_node.short_name = edition['name'] edition_node.save() edition_node_content = NodeLanguageVersion( node=edition_node, language=settings.LANGUAGE_CODE) edition_node_content.full_name = edition['long_name'] try: edition_tag = Tag.objects.get( name=edition['tag']).problems.all() except ObjectDoesNotExist: stdout.write('Ignoring edition with tag {}'.format( edition['tag'])) edition_node_content.panel_code = '' edition_node_content.save() continue edition_node_content.panel_code = '' for header, tasks in edition['tasks']: l = [] for task in tasks: try: taskname_tag = Tag.objects.get( name=task).problems.all() try: task_obj = (edition_tag & taskname_tag)[0] except IndexError: stdout.write( 'Ignoring task: {} from {}, no such task in DB' .format(task, edition['tag'])) continue l.append( reverse('problem_site', kwargs={ 'site_key': task_obj.problemsite.url_key })) except ObjectDoesNotExist: stdout.write('Ignoring task: {} from {}, task\'s \ tag does not exist'.format(task, edition['tag'])) edition_node_content.panel_code += u'[[ProblemTable:{}|{}]]' \ u'\n\n'.format(header, u';'.join(l)) edition_node_content.save() root.short_name = u'problemset' root.save() root_content = NodeLanguageVersion(node=root, language=settings.LANGUAGE_CODE) root_content.full_name = u'Problem Tree' root_content.panel_code = u'All of the public tasks are (should be) here.' root_content.save()