Commit df7ff74d by Őry Máté

school: fix automatic group handling

parent 6cf601b2
...@@ -41,9 +41,15 @@ class Course(models.Model): ...@@ -41,9 +41,15 @@ class Course(models.Model):
related_name='default_group_of') related_name='default_group_of')
owners = models.ManyToManyField(Person, blank=True, null=True) owners = models.ManyToManyField(Person, blank=True, null=True)
def get_or_create_default_group(self): def get_or_create_default_group(self):
self.default_group = Group(name=self.name, if self.default_group:
semester=Semester.get_current()) return self.default_group
self.default_group.save() else:
default_group = Group(name=_("%s -- default") % self.short(),
semester=Semester.get_current(), course=self)
default_group.save()
self.default_group_id = default_group.id
self.save()
return default_group
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.default_group: if self.default_group:
self.default_group.course = self self.default_group.course = self
......
...@@ -44,6 +44,7 @@ def login(request): ...@@ -44,6 +44,7 @@ def login(request):
user.email = request.META['email'] user.email = request.META['email']
user.save() user.save()
p, created = Person.objects.get_or_create(user=user) p, created = Person.objects.get_or_create(user=user)
p.save()
try: try:
sem = Semester.get_current() sem = Semester.get_current()
...@@ -55,15 +56,18 @@ def login(request): ...@@ -55,15 +56,18 @@ def login(request):
attended = attended.split(';') attended = attended.split(';')
for c in attended: for c in attended:
co, created = Course.objects.get_or_create(code=c) co, created = Course.objects.get_or_create(code=c)
if created:
logger.warning("django Course %s created" % c)
g = co.get_or_create_default_group() g = co.get_or_create_default_group()
if p.course_groups.filter(semester=sem, course=co).count() == 0: if p.course_groups.filter(semester=sem, course=co).count() == 0:
try: try:
p.course_groups.add(g) g.members.add(p)
g.save()
messages.info(request, _('Course "%s" added.') % g.course) messages.info(request, _('Course "%s" added.') % g.course)
except Exception as e: except Exception as e:
logger.warning("Django ex %s" % e) logger.warning("Django ex %s" % e)
except ValidationError: except ValidationError as e:
pass logger.warning("Django ex4 %s" % e)
held = request.META['HTTP_NIIFEDUPERSONHELDCOURSE'] held = request.META['HTTP_NIIFEDUPERSONHELDCOURSE']
if held == '': if held == '':
...@@ -72,6 +76,8 @@ def login(request): ...@@ -72,6 +76,8 @@ def login(request):
held = held.split(';') held = held.split(';')
for c in held: for c in held:
co, created = Course.objects.get_or_create(code=c) co, created = Course.objects.get_or_create(code=c)
if created:
logger.warning("django Course %s created" % c)
g = co.get_or_create_default_group() g = co.get_or_create_default_group()
try: try:
co.owners.add(p) co.owners.add(p)
...@@ -79,6 +85,9 @@ def login(request): ...@@ -79,6 +85,9 @@ def login(request):
messages.info(request, _('Course "%s" ownership added.') % g.course) messages.info(request, _('Course "%s" ownership added.') % g.course)
except Exception as e: except Exception as e:
logger.warning("Django ex %s" % e) logger.warning("Django ex %s" % e)
co.save()
g.save()
p.save() p.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment