Opened 17 years ago

Closed 17 years ago

#4778 closed (duplicate)

unique together breaks admin

Reported by: canburak@… Owned by: Adrian Holovaty
Component: contrib.admin Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I cannot update the Submission from admin.

It works right without admin.

I get:

TypeError at /admin/reiloy/submission/1/
Related Field has invalid lookup: iexact
Request Method: 	POST
Request URL: 	http://localhost:8000/admin/reiloy/submission/1/
Exception Type: 	TypeError
Exception Value: 	Related Field has invalid lookup: iexact
Exception Location: 	/usr/lib/python2.5/site-packages/django/db/models/fields/related.py in get_db_prep_lookup, line 109
Python Executable: 	/usr/bin/python
Python Version: 	2.5.1
import os, datetime
from os.path import exists
from django.db import models, transaction
from django.contrib.auth.models import User
from django.core import validators
from django import newforms as forms
from django.conf import settings

# Create your models here.

# class CSDNUser(models.Model):
#     django_user = models.OneToOneField(User)
#     bilgi_id = models.PositiveIntegerField()
    
#     class Admin:
#         pass

#     def __str__(self):
#         return str(self.django_user)

class Student(models.Model):
    django_user = models.OneToOneField(User,unique=True)
    bilgi_id = models.PositiveIntegerField(unique=True,null=True,blank=True)

    def __str__(self):
        return str(self.django_user)

    def create_submission(self, project, files):
        s, created = Submission.objects.get_or_create(student=self, project=project)
        s.submission_time = datetime.datetime.now()
        filenames = s.get_filenames()

        def create_directory_if_not_exists(directory):
            try:
                os.makedirs(directory)
                return True
            except OSError:
                return False
        
        def save_file(filename, data):
            saveto = filenames[filename]
            create_directory_if_not_exists(saveto)
            f = open(os.path.join(saveto, filename),"w")
            f.write(data)
            f.close()
            return True

        for filename,fdata in files.items():
            content = files[filename]["content"]
            save_file(filename, content)

        s.save()
        self.django_user.message_set.create(message="Submission accepted")
        return True

    @transaction.commit_manually
    def updateFromQ(self):
        requests = CourseUpdateQ.objects.filter(student_id=self.bilgi_id)
        for request in requests:
            request.course.students.add(self)
            request.course.save()
            request.delete()
            transaction.commit()

    class Admin:
        list_display = ('django_user', 'bilgi_id')


class Instructor(models.Model):
    django_user = models.OneToOneField(User,unique=True)
    bilgi_id = models.PositiveIntegerField(unique=True)

    def __str__(self):
        return str(self.django_user)
    
    class Admin:
        list_display = ('django_user', 'bilgi_id')


class Course(models.Model):
    name = models.CharField(maxlength=64)
    code = models.CharField(maxlength=8,validator_list=[validators.MatchesRegularExpression(regexp="[a-z]{2,4}[0-9]{3}", error_message="Course code must consist of 2 - 4 lowercase characters, followed by 3 digits and must not include any spaces.")])
    students = models.ManyToManyField(Student, blank=True)
    instructors = models.ManyToManyField(Instructor, blank=True)

    def get_project_submission_tuples_for_user(self, django_user):
        ps = self.project_set.all()
        res = []

        for p in ps:
            try:
                s = p.submission_set.get(student__django_user=django_user)
            except Submission.DoesNotExist:
                s = None
                
            res.append((p,s))

        return res


    class Admin:
        list_display = ("code","name")
        list_filter = ('instructors',)

    def __str__(self):
        return "%s (%s)"%(self.name, self.code)

class SubmissionFilterGroup(models.Model):
    name = models.CharField(maxlength=64)
    filter_list = models.CharField(maxlength=256)

    def __str__(self):
        return self.name

    class Admin:
        list_display=('name', 'filter_list')

class Project(models.Model):
    course = models.ForeignKey(Course)
    name = models.CharField(maxlength=64)
    submission_filter_group = models.ManyToManyField(SubmissionFilterGroup)
    deadline = models.DateTimeField()
    slug = models.SlugField(prepopulate_from=("name",), unique=True)
    description = models.TextField(blank=True)
    specification_file = models.FileField(upload_to="upload/staff/project_specification_file/%Y%m%d%H%M%S/",
                                          blank=True,
                                          null=True)

    def get_form(self):
        class DForm(forms.Form):
            pass

        pfs = self.projectfile_set.all()

        f = DForm()

        for pf in pfs:
            f.fields[pf.filename] = forms.Field(widget=forms.FileInput(),
                                                label=pf.filename,
                                                help_text="%s (%s)"%(pf.description,pf.mime_type))

        return f

    def filename_templates(self):
        cid = self.course.code
        return [(p.filename, os.path.join(settings.PROJECT_UPLOAD_DIR,
                             cid,
                             self.slug,
                             "%s")) for p in self.projectfile_set.all()]


    def is_deadline_passed(self):
        return self.deadline < datetime.datetime.now()
        
    class Admin:
        list_display = ('name','course','deadline')
        list_filter = ('course','deadline')


    class Meta:
        unique_together = (("slug", "course"),)
    
    def __str__(self):
        return "%s (%s)"%(self.name, self.course)

class ProjectFile(models.Model):
    mime_type = models.CharField(maxlength=32,core=True)
    description = models.CharField(maxlength=128,core=True)
    project = models.ForeignKey(Project,edit_inline=models.TABULAR)
    filename = models.CharField(maxlength=64,validator_list=[validators.MatchesRegularExpression(regexp="[a-zA-Z0-9.-_]", error_message="Filename must only contain letters, numbers, dash, dot and underscore characters")],core=True)

    class Admin:
        list_filter = ('project',)
        list_display = ('project', 'mime_type','description')

    def __str__(self):
        return "%s (%s)"%(self.description, self.mime_type)

    
class Submission(models.Model):
    student = models.ForeignKey(Student)
    project = models.ForeignKey(Project)
    submission_time = models.DateTimeField(auto_now_add=True)

    grade = models.PositiveIntegerField(help_text="Grade of the submission, over 100",
                                        blank=True,
                                        null=True)
    graders_note = models.CharField(maxlength=256,blank=True)
    graded_by = models.ForeignKey(Instructor,blank=True,null=True)
    graded_at = models.DateTimeField(blank=True,null=True)

    def get_course(self):
        return self.project.course

    def is_graded(self):
        return self.grade != None

    def get_filenames(self):
        return dict([(f,(s % self.student.bilgi_id)) for f,s in self.project.filename_templates()])

    class Admin:
        list_display = ('student','project','is_graded')
        list_filter = ('project','grade')

    def __str__(self):
        return "%s (%s)"%(self.student, self.project)

    #class Meta:
    #    unique_together = (("student", "project"),)
    
class UploadedFile(models.Model):
    content = models.FileField(upload_to="/tmp/cango/",core=True)
    submission = models.ForeignKey(Submission)
    
    class Admin:
        pass

    def __str__(self):
        return "%s"%(self.content)
    
class CourseUpdateQ(models.Model):
    student_id = models.PositiveIntegerField()
    course = models.ForeignKey(Course)

    class Meta:
        unique_together = (("student_id", "course"),)
    
    class Admin:
        pass

class Lecture(models.Model):
    course = models.ForeignKey(Course)
    date = models.DateTimeField()

    def __str__(self):
        return "%s - %s"%(self.course, self.date)
        
    class Admin:
        list_display=("course","date")

class Attendance(models.Model):
    lecture = models.ForeignKey(Lecture)
    student_id = models.PositiveIntegerField()

    class Admin:
        list_display=("lecture","student_id")
        list_filter = ('student_id',"lecture")

    class Meta:
        unique_together = (("lecture","student_id"),)

Change History (1)

comment:1 by Chris Beaven, 17 years ago

Resolution: duplicate
Status: newclosed

Looks like a dupe of #4562

Note: See TracTickets for help on using tickets.
Back to Top