[PYTHON] [Django] I tried to implement access control by class inheritance.

Preface

Please forgive the mistake as it is written by a beginner with a memo. I'm making a chat app, but I want to make sure that only the participants in the room can see the chat room. I spent a lot of time trying to implement it by overriding the methods in the class view, There was an insanely easy way, so I will introduce it.

Main subject

models.py



class Room(models.Model):
     (Abbreviation)

class User(AbstractBaseUser, PermissionsMixin):
     (Abbreviation)

class JoinRoom(models.Model):#Intermediate table
    room = models.ForeignKey(Room,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.PROTECT)

It may not be better to write it here, but I will summarize it here for the sake of clarity. It seems that get is faster than using filter, so I do this.

views.py



from django.contrib.auth.mixins import UserPassesTestMixin

class OnlyParticipantMixin(UserPassesTestMixin):
    raise_exception = True

    def test_func(self):
        try:
            exist_or_not = MyRoom.objects.get(room=self.kwargs['pk'],user=self.request.user)#I haven't tried yet if I need to substitute.
            return True
        except MyRoom.DoesNotExist:
            return False

class CommentCreateView(OnlyParticipantMixin,generic.CreateView):#Doesn't it work unless it's in this order?
     template_name = 'hogehoge/hogehoge'

That's it. If anyone other than the participant tries to peep, an error will occur.

reference

https://developer.yukimonkey.com/article/20200417/

https://intellectual-curiosity.tokyo/2018/12/06/django%E3%81%AE%E8%A9%B3%E7%B4%B0%E7%94%BB%E9%9D%A2detailview%E3%81%A7%E8%87%AA%E5%88%86%E4%BB%A5%E5%A4%96%E3%81%AE%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AB%E3%82%A2%E3%82%AF/

Recommended Posts

[Django] I tried to implement access control by class inheritance.
I tried to implement PCANet
I tried to implement StarGAN (1)
I tried to implement Deep VQE
I tried to implement anomaly detection by sparse structure learning
I tried to implement adversarial validation
I tried to implement hierarchical clustering
I tried to implement Realness GAN
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I tried to implement sentence classification by Self Attention with PyTorch
I tried to implement PLSA in Python
I tried to implement Autoencoder with TensorFlow
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
I tried Django
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to program bubble sort by language
I tried to implement reading Dataset with PyTorch
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to classify dragon ball by adaline
[Introduction to PID] I tried to control and play ♬
I tried to implement the traveling salesman problem
I tried to implement sentence classification & Attention visualization by Japanese BERT in PyTorch
I tried to debug.
I tried to paste
I tried to create a table only with Django
I tried to implement multivariate statistical process management (MSPC)
I tried to implement and learn DCGAN with PyTorch
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement time series prediction with GBDT
I tried to implement GA (genetic algorithm) in Python
I tried to implement Grad-CAM with keras and tensorflow
I tried to implement SSD with PyTorch now (Dataset)
I tried to access Google Spread Sheets using Python
I tried to implement automatic proof of sequence calculation
[Introduction] I tried to implement it by myself while explaining the binary search tree.
[Introduction] I tried to implement it by myself while explaining to understand the binary tree
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried to learn PredNet
I tried to implement a one-dimensional cellular automaton in Python
I tried to organize SVM.
I tried to implement breakout (deception avoidance type) with Quantx
I tried to speed up video creation by parallel processing
I wanted to classify Shadowverse card images by reader class
[Introduction to simulation] I tried playing by simulating corona infection ♬
[Introduction to Pandas] I tried to increase exchange data by data interpolation ♬
I tried to get started with Hy ・ Define a class
I tried to classify MNIST by GNN (with PyTorch geometric)
I tried to reintroduce Linux
I tried to implement ListNet of rank learning with Chainer
I tried to implement the mail sending function in Python
I tried to introduce Pylint
I tried to implement Harry Potter sort hat with CNN