2017-03-26 19 views
1

現在の管理者関連に接続する予定のカレンダーがあります。管理者はカレンダーで日付を選択し、イベントが設定されるようにフォームを登録します。しかし、別の管理者関連でログインすると、前回の管理者からのイベントが表示されます。私は彼らをお互いに別々にしておきたい。Django - ユーザーが同じ値にアクセスできないように制限する

まだ初心者が正しい方法でガイドしてくれてありがとうございました。


models.py

class Administrator(AbstractUser): 
     # inherits the standard User-model 
     ... 
     association = models.ForeignKey(Association) 


class Event(models.Model): 
     name = models.CharField(max_length=50) 
     location = models.CharField(max_length=100) 
     start = models.DateTimeField(blank=False) 
     end = models.DateTimeField(blank=False) 
     allday = models.BooleanField() 
     description = models.TextField(max_length=200) 
     synced = models.BooleanField(default=False) 
     gid = models.CharField(default='', max_length=100) 
     association = models.ForeignKey(Association) 


class Association(models.Model): 
     asoc_name = models.CharField(max_length=50, null=True, blank=True) 

views.py

class calendar(ListView): 

    model = Event 
    template_name = 'calapp/calendar.html' 

    def get_queryset(self): 
     queryset = Event.objects.filter(association=self.request.user.association) 
     return queryset 


def event_add_edit(request): 
    if request.method == 'POST': 
     res = {'success': False} 

     action = request.POST['action'] 
     name = request.POST['name'] 
     location = request.POST['location'] 
     start = request.POST['start'] 
     end = request.POST['end'] 
     allday = request.POST['allday'] == 'true' 
     description = request.POST['description'] 
     synced = request.POST['synced'] == 'true' 
     association = Association.objects.filter(asoc_name=request.user.association) 
     asoc = Association.objects.get(id=association) 

     if action == 'add': 
      Event.objects.create(
       name=name, 
       location=location, 
       start=start, 
       end=end, 
       allday=allday, 
       description=description, 
       synced=synced, 
       association=asoc 
      ) 

      res['success'] = True 
      res['message'] = 'added' 
      eid = Event.objects.latest('id').id 
      res['eid'] = eid 
      res['data'] = Event.objects.values().get(id=eid) 
     elif action == 'edit': 


      eid = request.POST['eid'] 
      event = Event.objects.get(id=eid) 
      event.name = name 
      event.location = location 
      event.start = start 
      event.end = end 
      event.allday = allday 
      event.description = description 
      event.synced = synced 
      event.save() 

      res['success'] = True 
      res['message'] = 'edited' 
      res['eid'] = eid 
      res['data'] = Event.objects.values().get(id=eid) 

     return JsonResponse(res) 
    else: 
     raise Http404 

calendar.html

<div id="container"> 
<div id="cal-content-grid"> 
      <div id="week-days"> 
       <ul> 
        <li class="week-day">Sunday</li> 
        <li class="week-day">Monday</li> 
        <li class="week-day selected">Tuesday</li> 
        <li class="week-day">Wednesday</li> 
        <li class="week-day">Thursday</li> 
        <li class="week-day">Friday</li> 
        <li class="week-day">Saturday</li> 
       </ul> 
      </div> 
      <div class="week-row"> 
       <ul> 
        <li class="week-row-cell disabled">1</li> 
        <li class="week-row-cell disabled">2</li> 
        <li class="week-row-cell disabled">3</li> 
        <li class="week-row-cell disabled">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
      <div class="week-row"> 
       <ul> 
        <li class="week-row-cell">1</li> 
        <li class="week-row-cell">2</li> 
        <li class="week-row-cell">3</li> 
        <li class="week-row-cell">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
      <div class="week-row"> 
       <ul> 
        <li class="week-row-cell">1</li> 
        <li class="week-row-cell">2</li> 
        <li class="week-row-cell">3</li> 
        <li class="week-row-cell selected">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
      <div class="week-row"> 
       <ul> 
        <li class="week-row-cell">1</li> 
        <li class="week-row-cell">2</li> 
        <li class="week-row-cell">3</li> 
        <li class="week-row-cell">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
      <div class="week-row"> 
       <ul> 
        <li class="week-row-cell">1</li> 
        <li class="week-row-cell">2</li> 
        <li class="week-row-cell">3</li> 
        <li class="week-row-cell">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
      <div class="week-row last"> 
       <ul> 
        <li class="week-row-cell">1</li> 
        <li class="week-row-cell">2</li> 
        <li class="week-row-cell">3</li> 
        <li class="week-row-cell">4</li> 
        <li class="week-row-cell">5</li> 
        <li class="week-row-cell">6</li> 
        <li class="week-row-cell last">7</li> 
       </ul> 
      </div> 
     </div> 
    </div> 
</div> 

<div class="form-wrapper" id="add-event-form"> 
    <form action="" method="POST" role="form"> 
     {% csrf_token %} 
     <input type="hidden" name="action" id="add-event-action" value="add"> 
     <input type="hidden" name="eid" id="add-event-eid" value=""> 
     <input type="hidden" name="synced" id="add-event-synced" value="false"> 
     <ul> 
      <li class="form-li"> 
       <span class="form-top" id="add-event-top"></span> 
       <span class="btn-fa btn-close form-close"> 
        <i class="fa fa-times"></i> 
       </span> 
      </li> 
      <li class="form-li"> 
       <span class="input-field"> 
        <input id="add-event-name" maxlength="50" name="name" placeholder="Event Name" type="text" required> 
       </span> 
      </li> 
      <li class="form-li"> 
       <span class="input-field"> 
        <input id="add-event-location" maxlength="100" name="location" placeholder="Location" required> 
       </span> 
      </li> 
      <li class="form-li"> 
       <span class="input-name">Starts</span> 
       <span class="input-field datetime-field"> 
        <input id="add-event-sdate" name="start_date" type="date" ><input id="add-event-stime" name="start_time" type="time" required> 
       </span> 
      </li> 
      <li class="form-li"> 
       <span class="input-name">Ends</span> 
       <span class="input-field datetime-field"> 
        <input id="add-event-edate" name="end_date" type="date" ><input id="add-event-etime" name="end_time" type="time" required> 
       </span> 
      </li> 
      <li class="form-li"> 
       <input id="add-event-allday" type="checkbox" name="allday"> 
       <label for="add-event-allday" class="input-name"> All Day</label> 
      </li> 
      <li class="form-li"> 
       <textarea id="add-event-desc" name="description" placeholder="Description" maxlength="200"></textarea> 
      </li> 
      <li class="form-li"> 
       <input class="form-btn form-close" type="button" value="Cancel"> 
       <input class="form-btn btn-right" type="submit" value="Save"> 
      </li> 
     </ul> 
    </form> 
</div> 

calendar.js

# parts of the code that may be useful 

//generate event li 
getEventli: function (event) { 
    var $li = $("<li class='event-li'>"); 

    var $ename = $("<span class='event-li-name'>"); 
    $ename.text(event.name); 
    $li.append($ename); 

    var $etime = $("<span class='event-li-time'>"); 
    var start = new Date(event.start), 
     h = cal.getHourStr(start.getHours()); 

    $etime.text(h); 
    $li.append($etime); 

    // add data-eid attribute for detail show 
    $li.data("eid", event.id); 
    $li.click(cal.showDetailForm); 

    cal.eventsById[event.id].eventli.push($li); 

    return $li; 
}, 
+0

お使いのモデルを完全に展開してください。 – marin

+0

@marin - 更新されたモデル – Niknak

答えて

0

あなたのクエリが間違っている、あなたはすべての関連付けを要求していますが、ユーザが独自のモデルを取得する明示的な発言をしないでください。

association = Administrator.objects.filter(id=request.user.id).values('association__asoc_name') 

しかし、他の方法でモデルを書き直すことをお勧めします。

class Administrator(AbstractUser): 
    # inherits the standard User-model 
    # User is only here to refernce trought all models by FK 

class Association(models.Model): 
    asoc_name = models.CharField(max_length=50, null=True, blank=True) 
    user = models.ForeignKey(Administrator) 

class Event(models.Model): 
    name = models.CharField(max_length=50) 
    association = models.ForeignKey(Association) 
    # This kinde is esay to query and has some logic 

# Example Event Query 
q = Event.objects.filter(association__user__id=request.user.id) 

# Example Association Query 
q = Association.objects.filter(user=request.user.id) 

# Exmpale Return Association and Event 
q = Association.objects.filter(user=request.user.id).values('association__event__name') 
+0

他のモデルを書き直す方法はありませんか?私は他の機能がこのモデルに依存しているので、私はxD reaaaallyyyあなたの助けに感謝= D – Niknak

関連する問題