def post(self, request): if isinstance(request.data, list): for se in request.data: smb_share = self.create_samba_share(se) else: smb_share = self.create_samba_share(request.data) refresh_smb_config(list(SambaShare.objects.all())) refresh_smb_discovery(list(SambaShare.objects.all())) self._restart_samba() return Response(SambaShareSerializer(smb_share).data)
def delete(self, request, smb_id): try: smbo = SambaShare.objects.get(id=smb_id) SambaCustomConfig.objects.filter(smb_share=smbo).delete() smbo.delete() except: e_msg = ( "Samba export for the id ({}) does not exist.").format(smb_id) handle_exception(Exception(e_msg), request) with self._handle_exception(request): refresh_smb_config(list(SambaShare.objects.all())) refresh_smb_discovery(list(SambaShare.objects.all())) self._restart_samba() return Response()
def put(self, request, smb_id): with self._handle_exception(request): try: smbo = SambaShare.objects.get(id=smb_id) except: e_msg = ("Samba export for the id ({}) does not exist." ).format(smb_id) handle_exception(Exception(e_msg), request) options = self._validate_input(request.data, smbo=smbo) custom_config = options["custom_config"] del options["custom_config"] smbo.__dict__.update(**options) admin_users = request.data.get("admin_users", []) if admin_users is None: admin_users = [] for uo in User.objects.filter(smb_shares=smbo): if uo.username not in admin_users: uo.smb_shares.remove(smbo) self._set_admin_users(admin_users, smbo) smbo.save() for cco in SambaCustomConfig.objects.filter(smb_share=smbo): if cco.custom_config not in custom_config: cco.delete() else: custom_config.remove(cco.custom_config) for cc in custom_config: cco = SambaCustomConfig(smb_share=smbo, custom_config=cc) cco.save() for smb_o in SambaShare.objects.all(): if not smb_o.share.is_mounted: mnt_pt = "%s%s" % (settings.MNT_PT, smb_o.share.name) try: mount_share(smb_o.share, mnt_pt) except Exception as e: logger.exception(e) if smb_o.id == smbo.id: e_msg = ("Failed to mount share ({}) due to a low " "level error.").format(smb_o.share.name) handle_exception(Exception(e_msg), request) refresh_smb_config(list(SambaShare.objects.all())) refresh_smb_discovery(list(SambaShare.objects.all())) self._restart_samba() return Response(SambaShareSerializer(smbo).data)