Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Forked from thebookworm101/my integrity
Last active December 10, 2015 13:08

Revisions

  1. SmileyChris revised this gist Jan 2, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion views.py
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@ def add_job(request):
    msg = "Job saved successfully"
    messages.success(request, msg, fail_silently=True)
    return redirect(article)
    return render('job/job_form.html', {'form': form})
    return render(request, 'job/job_form.html', {'form': form})
  2. SmileyChris revised this gist Jan 2, 2013. 2 changed files with 7 additions and 4 deletions.
    9 changes: 6 additions & 3 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    class JobForm(forms.ModelForm):
    """ this handles the start_time and end_time defaults"""
    """
    A form used for creating a Job. It handles the start_time and end_time defaults.
    """

    class Meta:
    exclude = ['slug','author',]
    fields = [] # Everything except 'slug' and 'author'.
    model = Job

    def save(self, *args, **kw):
    last_entry = self.instance.author.job_set.aggregate(max_end_time=Max('end_time'))['max_end_time']
    if last_entry:
    self.instance.start_time = last_entry.end_time
    return super(JobForm,self).save(*args, **kw)
    return super(JobForm, self).save(*args, **kw)
    2 changes: 1 addition & 1 deletion views.py
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@ def add_job(request):
    msg = "Job saved successfully"
    messages.success(request, msg, fail_silently=True)
    return redirect(article)
    return render('job/job_form.html', { 'form': form })
    return render('job/job_form.html', {'form': form})
  3. SmileyChris revised this gist Jan 2, 2013. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion forms.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ class Meta:
    model = Job

    def save(self, *args, **kw):
    last_entry = Job.objects.filter(author=self.request.user).aggregate(max_end_time=Max('end_time'))['max_end_time']
    last_entry = self.instance.author.job_set.aggregate(max_end_time=Max('end_time'))['max_end_time']
    if last_entry:
    self.instance.start_time = last_entry.end_time
    return super(JobForm,self).save(*args, **kw)
  4. SmileyChris revised this gist Jan 2, 2013. 3 changed files with 21 additions and 49 deletions.
    11 changes: 11 additions & 0 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    class JobForm(forms.ModelForm):
    """ this handles the start_time and end_time defaults"""
    class Meta:
    exclude = ['slug','author',]
    model = Job

    def save(self, *args, **kw):
    last_entry = Job.objects.filter(author=self.request.user).aggregate(max_end_time=Max('end_time'))['max_end_time']
    if last_entry:
    self.instance.start_time = last_entry.end_time
    return super(JobForm,self).save(*args, **kw)
    49 changes: 0 additions & 49 deletions my integrity
    Original file line number Diff line number Diff line change
    @@ -12,52 +12,3 @@ Exception Type: IntegrityError
    Exception Value:

    (1048, "Column 'author_id' cannot be null")



    #########################Problem im solving: im trying to set the begin_time to be the same as the end_time of the previous entry of this model by the same user if any (only the very first time wont have a previous end_time).
    ###Here is my solution i have so far:

    #######################The view #########

    @login_required
    def add_job(request):
    form = JobForm(request.POST or None)
    if form.is_valid():
    article = form.save(commit=False)
    article.author = request.user
    article.save()
    msg = "Job saved successfully"
    messages.success(request, msg, fail_silently=True)
    return redirect(article)
    return render_to_response('job/job_form.html',
    { 'form': form },
    context_instance=RequestContext(request))


    ######################the JobForm #########

    class JobForm(forms.ModelForm):
    """ this handles the start_time and end_time defaults"""
    class Meta:
    exclude = ['slug','author',]
    model = Job

    def __init__(self,*args, **kw):
    super(JobForm,self).__init__( *args, **kw)

    def save(self, *args, **kw):
    instance = super(JobForm,self).save()
    #last_entry = Job.objects.filter(author__id=user.id).order_by('-id')[0]
    last_entry = Job.objects.filter(author=self.request.user).aggregate(max_end_time=Max('end_time'))['max_end_time']
    if last_entry:
    instance.start_time = last_entry.end_time
    else:
    # this is the very first record do nothing
    pass
    instance.save()
    return instance




    10 changes: 10 additions & 0 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    @login_required
    def add_job(request):
    form = JobForm(data=request.POST or None)
    if form.is_valid():
    form.instance.author = request.user
    article = form.save()
    msg = "Job saved successfully"
    messages.success(request, msg, fail_silently=True)
    return redirect(article)
    return render('job/job_form.html', { 'form': form })
  5. @thebookworm101 thebookworm101 revised this gist Jan 2, 2013. 1 changed file with 0 additions and 15 deletions.
    15 changes: 0 additions & 15 deletions my integrity
    Original file line number Diff line number Diff line change
    @@ -61,18 +61,3 @@ class JobForm(forms.ModelForm):




    ################### here is the model ,
    class Job(models.Model):
    """"""

    start_time = models.DateTimeField(default=datetime.datetime.now(),)
    end_time= models.DateTimeField( help_text="YYYY-MM-DD HH-MM-SS")
    activity = models.CharField(max_length=255)
    task = models.CharField(max_length=100)
    category= models.CharField(max_length=1,choices=MY_CATEGORIES,default='A')
    subcategory = models.CharField(max_length=1,choices=MY_SUBCATEGORIES,default='A')

    slug = models.SlugField(max_length=50, unique=True) #
    author = models.ForeignKey(User)
    objects = models.Manager()
  6. @thebookworm101 thebookworm101 revised this gist Jan 2, 2013. 1 changed file with 18 additions and 14 deletions.
    32 changes: 18 additions & 14 deletions my integrity
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,21 @@
    ###Problem, im trying to set the begin_time to be the same as the end_time of the previous entry of this model by the same user if any (only the very first time wont have a previous end_time).
    ################entering some data the first time to my form doesnt work, it gives this error:


    IntegrityError at /job/add/article/

    (1048, "Column 'author_id' cannot be null")

    Request Method: POST
    Request URL: http://localhost:8000/job/add/article/
    Django Version: 1.4.3
    Exception Type: IntegrityError
    Exception Value:

    (1048, "Column 'author_id' cannot be null")



    #########################Problem im solving: im trying to set the begin_time to be the same as the end_time of the previous entry of this model by the same user if any (only the very first time wont have a previous end_time).
    ###Here is my solution i have so far:

    #######################The view #########
    @@ -41,20 +58,7 @@ class JobForm(forms.ModelForm):
    instance.save()
    return instance

    ################But on entering some data the first time it doesnt work, it gives this error:


    IntegrityError at /job/add/article/

    (1048, "Column 'author_id' cannot be null")

    Request Method: POST
    Request URL: http://localhost:8000/job/add/article/
    Django Version: 1.4.3
    Exception Type: IntegrityError
    Exception Value:

    (1048, "Column 'author_id' cannot be null")



  7. @thebookworm101 thebookworm101 created this gist Jan 2, 2013.
    74 changes: 74 additions & 0 deletions my integrity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    ###Problem, im trying to set the begin_time to be the same as the end_time of the previous entry of this model by the same user if any (only the very first time wont have a previous end_time).
    ###Here is my solution i have so far:

    #######################The view #########

    @login_required
    def add_job(request):
    form = JobForm(request.POST or None)
    if form.is_valid():
    article = form.save(commit=False)
    article.author = request.user
    article.save()
    msg = "Job saved successfully"
    messages.success(request, msg, fail_silently=True)
    return redirect(article)
    return render_to_response('job/job_form.html',
    { 'form': form },
    context_instance=RequestContext(request))


    ######################the JobForm #########

    class JobForm(forms.ModelForm):
    """ this handles the start_time and end_time defaults"""
    class Meta:
    exclude = ['slug','author',]
    model = Job

    def __init__(self,*args, **kw):
    super(JobForm,self).__init__( *args, **kw)

    def save(self, *args, **kw):
    instance = super(JobForm,self).save()
    #last_entry = Job.objects.filter(author__id=user.id).order_by('-id')[0]
    last_entry = Job.objects.filter(author=self.request.user).aggregate(max_end_time=Max('end_time'))['max_end_time']
    if last_entry:
    instance.start_time = last_entry.end_time
    else:
    # this is the very first record do nothing
    pass
    instance.save()
    return instance

    ################But on entering some data the first time it doesnt work, it gives this error:


    IntegrityError at /job/add/article/

    (1048, "Column 'author_id' cannot be null")

    Request Method: POST
    Request URL: http://localhost:8000/job/add/article/
    Django Version: 1.4.3
    Exception Type: IntegrityError
    Exception Value:

    (1048, "Column 'author_id' cannot be null")



    ################### here is the model ,
    class Job(models.Model):
    """"""

    start_time = models.DateTimeField(default=datetime.datetime.now(),)
    end_time= models.DateTimeField( help_text="YYYY-MM-DD HH-MM-SS")
    activity = models.CharField(max_length=255)
    task = models.CharField(max_length=100)
    category= models.CharField(max_length=1,choices=MY_CATEGORIES,default='A')
    subcategory = models.CharField(max_length=1,choices=MY_SUBCATEGORIES,default='A')

    slug = models.SlugField(max_length=50, unique=True) #
    author = models.ForeignKey(User)
    objects = models.Manager()