在Django模板中将“用户拥有的”与“其他”数据分开

时间:2013-11-08 21:20:19

标签: python django

我有一个由Openstack驱动的Django修改过的应用程序,它显示了用户可以启动的磁盘映像和快照。用户当前看到他们创建的快照和他们没有创建的快照。我想根据用户是否拥有当前表将其分为两个。

我的两个表定义如下(注意我相应地改变了row_actions):

class UserSnapshotsTable(OldSnapshotsTable):
  cloud = tables.Column(get_cloud, verbose_name=_("Cloud"))

    class Meta:
      name = "usersnapshots"
      verbose_name = _("User Snapshots")
      table_actions = (DeleteSnapshot,)
      row_actions = (LaunchSnapshot, LaunchCluster, EditImage, DeleteSnapshot)
      pagination_param = "snapshot_marker"
      row_class = UpdateRow
      status_columns = ["status"]

class OtherSnapshotsTable(OldSnapshotsTable):
  cloud = tables.Column(get_cloud, verbose_name=_("Cloud"))

    class Meta:
      name = "othersnapshots"
      verbose_name = _("Other Snapshots")
      table_actions = (DeleteSnapshot,)
      row_actions = (LaunchSnapshot, LaunchCluster)
      pagination_param = "snapshot_marker"
      row_class = UpdateRow
      status_columns = ["status"]

我更改了HTML模板以拉出“UserSnapshotsTable”和“OtherSnapshotsTable”表(我复制了原始表并重命名了两者),但两个完整表仍然在相应的标题下生成。生成数据有两个函数:

def get_usersnapshots_data(self):
    req = self.request
    marker = req.GET.get(UserSnapshotsTable._meta.pagination_param, None)
    try:
        usersnaps, self._more_snapshots = api.snapshot_list_detailed(req,
                                                           marker=marker)
    except:
        usersnaps = []
        exceptions.handle(req, _("Unable to retrieve user-owned snapshots."))
    return usersnaps

def get_othersnapshots_data(self):
    req = self.request
    marker = req.GET.get(OtherSnapshotsTable._meta.pagination_param, None)
    try:
        othersnaps, self._more_snapshots = api.snapshot_list_detailed(req,
                                                            marker=marker)
    except:
        othersnaps = []
        exceptions.handle(req, _("Unable to retrieve non-user-owned snapshots."))
    return othersnaps

还为图像定义了编辑/删除选项,并为快照导入了这些选项,这些选项似乎具有密钥比较。这是“删除”一个(第7行):

class DeleteImage(tables.DeleteAction):
    data_type_singular = _("Image")
    data_type_plural = _("Images")

    def allowed(self, request, image=None):
      if image:
        return image.owner == request.user.tenant_id
        # Return True to allow table-level bulk delete action to appear.
      return True

    def delete(self, request, obj_id):
      api.image_delete(request, obj_id)

如何将这些表格分开?这是我第一次在这里提问,所以如果我能提供进一步的信息,请告诉我。道歉的长度。

1 个答案:

答案 0 :(得分:0)

据我所知,您正在使用glanceclient。如果是这样,您可以使用extra_filters snapshot_list_detailed()参数来仅过滤此类用户图片:

usersnaps, self._more_snapshots = api.snapshot_list_detailed(
    req,
    marker = marker,
    extra_filters = {"owner": "user_name"}
)

封面snapshot_list_detailed使用Openstack Image Service API的GET images