sgauri
sgauri

Reputation: 714

Allow user to edit once in django

Details:

I have a teacher dashboard where a teacher can create and share worksheets with other teachers. When any worksheet is shared with other teachers they can (but not necessary) provide feedback on that worksheet, which will be visible to the creator of worksheet.

Now I have a model which stores this sharing thing as follows:

class SharingWS(TimeStampWS):
    shared_by = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_by')
    shared_with = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_with')
    worksheet = models.ForeignKey('WorkSheet', on_delete=models.SET_NULL, null=True)
    flag = models.BooleanField()
    reason = models.TextField(blank=True)

as soon as the worksheet is created, an entry is created in the model with default flag=True.

Problem

Now how can I give only one chance to other teachers to review the worksheet. Once they have given a feedback, they cant do it again and if they dont give feedback it will be assumed approved after 2 days.

There are similar questions but for voting, where there is no previous entry in db and you can lookup. In my case an entry is already created so I can't use that concept.

Upvotes: 1

Views: 110

Answers (3)

sgauri
sgauri

Reputation: 714

With the hint from @Nihal and @Adam I solved it as follows.

Since I am already inheriting from TimeStampWS, I added a new field last_updated along with created_at. Now my models looked as followed:

class TimeStampWS(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

class SharingWS(TimeStampWS):
    shared_by = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_by')
    shared_with = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_with')
    worksheet = models.ForeignKey('WorkSheet', on_delete=models.SET_NULL, null=True)
    flag = models.BooleanField()
    reason = models.TextField(blank=True)

Now since as soon as the worksheet is created its created_at and last_updated time will have the difference in millisecond or at max in seconds. So I just used the date, hour and minute part to compare that whether teacher has provided feedback or not, This is my logic part:

views.py

def worksheet_review(request, id_r):
    current_time = timezone.now().replace(second=0, microsecond=0)

    if request.method=='POST':
        # got desired ShareWS object here
--->    creation_time = share_instance.created_at.replace(second=0,microsecond=0)
--->    modification_time = share_instance.last_updated.replace(second=0,microsecond=0)

        if creation_time==modification_time:
            # adding extra fields in the object
            share_instance.save()
            return HttpResponseRedirect(reverse('worksheet:worksheet_review_success'))

        elif current_time - creation_time > datetime.timedelta(days=2):
            return HttpResponseRedirect(reverse('worksheet:worksheet_review_once'))

Upvotes: 0

Adam Barnes
Adam Barnes

Reputation: 3203

Create a Feedback model, with a OneToOneField to SharingWS. Now, you can create a maximum of one Feedback instance per SharingWS, like so:

class SharingWS(TimeStampWS):
    shared_by = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_by')
    shared_with = models.ForeignKey('members.Profile', on_delete=models.CASCADE, related_name='shared_with')
    worksheet = models.ForeignKey('WorkSheet', on_delete=models.SET_NULL, null=True)


class Feedback(models.Model):
    sharing_ws = models.OneToOneField('app_name.SharingWS', on_delete=models.CASCADE, related_name='feedback')
    flag = models.BooleanField()
    reason = models.TextField(blank=True)

Upvotes: 1

KamilD
KamilD

Reputation: 163

Just check the condition if there exists record which has:

  1. shared_by - owner of worksheet
  2. shared_with - teacher that shared his opinion
  3. worksheet - specific worksheet

If it exists it means that specific teacher shared his opinion. Period For example such query:

SharingWS.objects.get(shared_by=Bob, shared_with=John, worksheet=BobsWorksheet)

looks for John's opinion about Bob's worksheet. If this query returns object it means that John can not share another opinion and you should disable it for him.

Upvotes: 0

Related Questions