Package: nextcloud-orm
nextcloud-ormWouldn't it be nice to programmatically manage your nextcloud users and groups with a django-likeObject-Relation-Model (ORM), instead of interacting with rather clumsy nextcloud api calls?importnextcloud_ormfromnextcloud_orm.modelsimportNextcloudUser,NextcloudGroup,NextcloudGroupFolder# establish connection by using environment variablesnextcloud_orm.connect()# alternative#nextcloud_orm.connect(endpoint=YOUR_NEXTCLOUD_URL, user=YOUR_NEXTCLOUD_USERNAME, password=YOUR_NEXTCLOUD_PASSWORD)# Create a new nextcloud user "Somebody". Attributes like the email can be directly set.u=NextcloudUser(name='Somebody',password='some_$ecure_password')u.email='some@mail.example'u.save()# Create a group "Some Group" if it does not exist already and add "Somebody" to the groupg=NextcloudGroup.objects.get_or_create(name='Some Group')g.users.add(u)g.save()# Create a groupfolder "development" with 2GB quota and make the folder# accessible for the previously created group without sharing permissionf=NextcloudGroupFolder('development',quota=2*1024**3)f.groups.add(g,share=False)f.save()This package wraps the lower level modulenextcloud-api-wrapperwith a class based ORM.Supported functionalitiesThis module essentially supports the functionalities that the nextcloud ocs offers for users, groups and groupfolders (thegroupfoldersapp needs to be installed in nextcloud!).
Apart from those, this module wraps installed nextcloud apps byNextcloudAppIf you need other api functionalities, you may access the more complete lower level api by:fromnextcloud_orm.managersimportNextcloudManager# Only accessible after successful connection to nextcloudnextcloud_orm.connect()# Lower level apiapi=NextcloudManager.apiCore functionallitiesCurrently there are four nextcloud object models:NextcloudUser,NextcloudGroup,NextcloudGroupFolderandNextcloudApp.Each of them can be initialized with given arguments and can be syncronized to nextcloud by usingNextcloudObject.save():# unsaved/not synchronized yetu=NextcloudUser(name='username',email='some@mail.example',displayname='User Name')try:# synchronize user to nextcloudu.save()exceptNextcloudUser.AlreadyExistase:# the user already exists# the lower-level api request can be accessed through 'e.request'passIf the object already exists, anAlreadyExistexception will be raised.Fetching objects from nextcloudTo access the objects that are already stored, you should use theNextcloudManagerinstance that is available viaNextcloudObject.objects.In many cases one just wants interact with a nextcloud object, e.g. changing a users email address.
To query a single object from nextcloud useNextcloudObject.objects.get()# get preexisting user by nameu=NextcloudUser.objects.get(name='username')# get preexisting user by emailu=NextcloudUser.objects.get(email='some@mail.example')To query every object from nextcloud useNextcloudObject.objects.all(),
if you are just interested in a few objects with some particular attributes you may useNextcloudObject.objects.filter()# get preexisting user by nameusers=NextcloudUser.objects.all()foruinusers:print(u.displayname,u.email)# filtering exampleunlimited_quota_users=NextcloudUser.objects.filter(quota=None)A very handy function isget_or_create(defaults, **attributes), which creates the queried user if the user does not exist yet.defaults={'email':'some@mail.example','password':'some_$ecure_password','displayname':'User Name'}u=NextcloudUser.objects.get_or_create(defaults,name='username')Accessing related objectsOne of the nicest features is that you can access the related objects of an instance viaRelatedNextcloudManagerattributes, e.g.NextcloudUser.groupshandles the group-membership of the user.u=NextcloudUser.objects.get(name='username')try:g=u.groups.get(name='admin')exceptNextcloudGroup.DoesNotExistase:# the user is not a member of 'admin'print('The user is not an admin')# direct checking for containment is also possibleu.groups.contains('admin')# get all groups of the usergroups=u.groups.all()The relations between the models can be modified by usingadd(),remove()andset():u=NextcloudUser.objects.get(name='username')g=NextcloudGroup('new group')g.save()# add a new group membershipu.groups.add(g)# alternativeu.groups.add('new group')u.save()# remove group membership, e.g. to disable admin accessg=NextcloudGroup.objects.get('admin')u.groups.remove(g)# (shorter) alternativeu.groups.remove('admin')u.save()# setting the related attribute to a list is also possibleu.groups.set([g])Mapped relations:group-membership:NextcloudUser.groupsandNextcloudGroup.usersgroup-subadmin:NextcloudUser.subadminandNextcloudGroup.subadminsgroup-folder:NextcloudGroup.foldersandNextcloudGroupFolder.groups