0

I have this simple model that acts like a rsync config that is used to pre fill in the fields for a celery periodic task. The first time i create a new rsync config trough the model everything is okay and a new periodic task is being created without a problem. When i try and alter certain fields that will change the task fields such as task arguments, I'm getting a "IntegrityError column name is not unique" I feel that it has something to do with the model save method but im not sure how to get it Right. anyone got some ideas?

here is the model:

from django.forms import ModelForm
from djcelery.models import IntervalSchedule
from djcelery.models import PeriodicTask, IntervalSchedule
INTERVAL=(
    ('every=5','period 5 minutes'),
    )

class Customer(models.Model):
    """(Customer description)"""
    customername = models.CharField(blank=True, max_length=30)
    emailaddress = models.EmailField()
    phonenumber = models.CharField(blank=True, max_length=10)
    class Meta:
    verbose_name_plural = "Customer"
    def __unicode__(self):
        return self.customername

class RsyncConfig(models.Model):
    """(RsyncConfig description)"""
    cname = models.ForeignKey(Customer)
    rsyncname = models.CharField(blank=True, max_length=255)
    interval=models.CharField(max_length=8,choices=INTERVAL)
    fromip = models.IPAddressField(blank=True)
    source_dir = models.CharField(blank=True, max_length=255)
    destination_dir = models.CharField(blank=True, max_length=255)
    rsync_args = models.CharField(blank=True, max_length=255)

    class Meta:
        verbose_name_plural = "Rsync Config"

    def __unicode__(self):
        return self.cname.customername

And here is the admin.py form.

from django.contrib import admin
from django import forms
from djcelery.models import PeriodicTask, IntervalSchedule
from newrsync.models import Customer,RsyncConfig

class CustomerAdmin(admin.ModelAdmin):
    class Meta:
        model = Customer

class RsyncConfigAdminForm(forms.ModelForm):
    list_display = ('customername', 'rsyncname','source_dir','destination_dir')
    class Meta:
        model = RsyncConfig

    def __init__(self, *args, **kwargs):
        super(RsyncConfigAdminForm,self).__init__(*args, **kwargs)

    def save(self, commit=True):
        interval = IntervalSchedule.objects.get(every=5,period="minutes")
        model = super(RsyncConfigAdminForm, self).save(commit = False)
        model.cname = self.cleaned_data['cname']
        model.rsyncname = self.cleaned_data['rsyncname']
        model.fromip = self.cleaned_data['fromip']
        model.source_dir = self.cleaned_data['source_dir']
        model.destination_dir = self.cleaned_data['destination_dir']
        model.rsync_args = self.cleaned_data['rsync_args']
        if commit:
            model.save()
        PeriodicTask.objects.get_or_create(
                interval=interval,
                task='apps.mftconfig.tasks.rsync_proc',
                args=['rsync', 
                    model.rsync_args,
                    model.source_dir,
                    model.destination_dir],
                kwargs = {},
                name = (model.cname," ",model.rsyncname),
                enabled=False
                )
        return model


class RsyncConfigAdmin(admin.ModelAdmin):
    form = RsyncConfigAdminForm
    list_display = ('cname', 'rsyncname','source_dir','destination_dir')


admin.site.register(Customer,CustomerAdmin)
admin.site.register(RsyncConfig,RsyncConfigAdmin)
Daxomatic
  • 1
  • 2
  • Can you 0) fix the indentation in save() and 1) reproduce the problem in django shell and paste the shell session + traceback. – jpic Feb 28 '12 at 10:19
  • Hi jpic,the identation seems fine? and Im not sure what you mean by reproduce the problem in shell. It's a form, and I'm not sure how to reproduce it without the form? – Daxomatic Feb 28 '12 at 10:28
  • Try to reproduce it without the form. Try with the form RsyncConfigAdminForm(data={ ... }) – jpic Feb 28 '12 at 10:41
  • Im not getting any errors. I'm using %cpaste with ipython. the issue will happen when i try to change a value. then the "IntegrityError column name is not unique" error pop up.. – Daxomatic Feb 28 '12 at 10:48
  • We have no crystal ball. Please, post the traceback, and describe exactly what you do "when i try to change a value" – jpic Feb 28 '12 at 10:54
  • this is the traceback of the form error. I opened it ( after creation and changed a value. ( sorry for the weird formatting i couldnt get it right somehow.. thanks for all the help! – Daxomatic Feb 28 '12 at 11:16
  • Can you enable SQL query logging and paste the executed queries ? http://stackoverflow.com/questions/9064018/postgresql-geodjango-what-to-do-with-internal-error/9064582#9064582 – jpic Feb 28 '12 at 11:20

1 Answers1

0

I basically ended up doing a delete of the object right before i save a new version.It's Not perfect but at least i circumvent the unique restrain in the PeriodicTask model and now let's hope it won't bite me in the ass later on. If anyone has any suggestions, please! ;-)

class RsyncConfigAdminForm(forms.ModelForm):
    list_display = ('customername','rsyncname','source_dir','destination_dir')
class Meta:
    model = RsyncConfig

def __init__(self, *args, **kwargs):
    super(RsyncConfigAdminForm,self).__init__(*args, **kwargs)

def save(self, commit=True):

    instance = super(RsyncConfigAdminForm, self).save(commit = False)
    instance.customername = self.cleaned_data['customername']
    instance.rsyncname = self.cleaned_data['rsyncname']
    instance.fromip = self.cleaned_data['fromip']
    instance.source_dir = self.cleaned_data['source_dir']
    instance.destination_dir = self.cleaned_data['destination_dir']
    instance.rsync_args = self.cleaned_data['rsync_args']
    interval = IntervalSchedule.objects.get(every=5,period="minutes")
    p=PeriodicTask.objects.filter(name=instance.rsyncname)
    p.delete()
    PeriodicTask.objects.get_or_create(
            interval=interval,
            task='apps.mftconfig.tasks.rsync_proc',
            args=['rsync',
                instance.rsync_args,
                instance.source_dir,
                instance.destination_dir],
            kwargs = {},
            name = (instance.rsyncname),
            enabled=True
            )
    if commit:
        instance.save()
    return instance
Daxomatic
  • 1
  • 2