Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create problem from command lines #2255

Closed
DapperX opened this issue May 10, 2023 · 2 comments
Closed

Create problem from command lines #2255

DapperX opened this issue May 10, 2023 · 2 comments

Comments

@DapperX
Copy link

DapperX commented May 10, 2023

For some automation reasons, I was trying to create problems from command lines.
What I did was to invoke python manage.py create_problem ... since I accidently found this command though it seems to not appear in the docs.
Then I got an error saying TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use types.set() instead, at the line problem.types = [ProblemType.objects.get(name=options['type'])], in judge/management/commands/create_problem.py
After doing some searches online, I had a workaround that changes the code as

    def handle(self, *args, **options):
        problem = Problem()
        problem.code = options['code']
        problem.name = options['name']
        problem.description = options['body']
        problem.group = ProblemGroup.objects.get(name=options['group'])
        #problem.types = [ProblemType.objects.get(name=options['type'])]  # <---changes begin here
        types = ProblemType.objects.get(name=options['type'])
        problem.save()
        problem.types.set([types])  # <---changes end here
        problem.save()

Then the error became django.db.utils.IntegrityError: (1048, "Column 'time_limit' cannot be null") which was solved by adding a line problem.time_limit = ... After repeating this for the fields .memory_limit and .points and adding corresponding arguments to the parser, it eventually works, and the newly added problem seems good on the webpage.

While a few questions come to me:

  1. I am not familiar with Django so I wonder if my code above is correct/safe to operate on the models. Is there a better way to do the same?
  2. What is the standard way to add problems from command lines? python manage.py create_problem is not documented and it needs quite a few changes to be functional.
@Xyene
Copy link
Member

Xyene commented May 10, 2023

Thanks for the report!

  1. I am not familiar with Django so I wonder if my code above is correct/safe to operate on the models. Is there a better way to do the same?

Your code looks fine to me. There was a change back some versions of Django that made direct assignment illegal, and it seems we forgot to update that command.

Seems like Django also got stricter around enforcing integrity and not generating defaults for time_limit and memory_limit.

  1. What is the standard way to add problems from command lines?

python manage.py create_problem is the way to go, we just don't use it on https://dmoj.ca so it has bitrotted over time.

I think your changes seem reasonable, would you be open to creating a pull request with them?

@DapperX
Copy link
Author

DapperX commented May 11, 2023

Thanks for the quick response!
It is good to know my changes look fine. I am happy to submit a PR with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants