Commits

Andres Vargas  committed 068ee76

adding changes on admin

  • Participants
  • Parent commits a91ae81

Comments (0)

Files changed (1)

File trident/accounts/admin.py

     form = FormAdmin
     readonly_fields = ("file","created")
 
+    def formfield_for_foreignkey(self, db_field, request, **kwargs):
+        if request.user.is_superuser:
+            return super(DumpAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
+        if db_field.name =="database":
+            profile = request.user.get_profile()
+            kwargs["queryset"] = Database.objects.filter(profile=profile)
+        return super(DumpAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
+
     def download(self, obj):
         a = u"%s/%s" %(settings.MEDIA_URL,obj.file)
         a = u"<a href='%(path)s'>download</a>" % dict(path=str(a))
         return render(request,"restore.html",d)
 
 
+
+class DatabaseAdmin(admin.ModelAdmin):
+
+    def formfield_for_foreignkey(self, db_field, request, **kwargs):
+        if request.user.is_superuser:
+            return super(DatabaseAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
+        if db_field.name =="profile":
+            profile = request.user.get_profile()
+            kwargs["queryset"] = Profile.objects.get(id = profile.id)
+        return super(DatabaseAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
+
+    def queryset(self, request):
+        qs = super(DatabaseAdmin, self).queryset(request)
+        if not request.user.is_superuser:
+            profile = request.user.get_profile()
+            return qs.filter(profile=profile)
+        else:
+            return qs
+
 admin.site.register(Dump, DumpAdmin)
-admin.site.register(Database)
+admin.site.register(Database, DatabaseAdmin)
 admin.site.register(Profile)