Django caching, authentication code, login, form validation

Keywords: Session Django Database Redis

1.3 Caching, Verification Code, Logon, Form Verification

Caching in Django

  1. Interface and usage

    from django.core.cache import cache
    
    # Set age = 123, 10 seconds expiration in the cache
    cache.set('age', 123, 10)
    
    # Get age
    a = cache.get('age')
    print(a)
    
    # Self increment
    x = cache.incr('age')
    print(x)
    
  2. Using Redis as the Cache Backend

    • Install pip install django-redis

    • settings configuration

      CACHES = {
          "default": {
              "BACKEND": "django_redis.cache.RedisCache",
              "LOCATION": "redis://127.0.0.1:6379/0",
              "OPTIONS": {
                  "CLIENT_CLASS": "django_redis.client.DefaultClient",
                  "PICKLE_VERSION": -1,
              }
          }
      }
      
  3. The expiration time can be used to process some temporary data with time-invalidation, such as mobile phone verification code.

  4. Interview questions:

    1. How to use Redis as a cache?

Analysis of Cookie and Session Mechanism

  1. Review of HTTP Protocol

    1. host
      1. linux: /etc/hosts
      2. windows: C:/Windows/System32/drivers/etc/hosts
  2. Generation process

    1. Browser: Send requests to the server
    2. Server: Accept and create a session object (which contains a session_id)
    3. Server: Execute the views function and get a response object
    4. Server: Execute response.set_cookie('session id', session_id) to write session_id to cookie
    5. Server: Return response to browser
    6. Browser: Read the response message, take session_id from Cookies and save it
  3. Follow-up requests

    1. Browser: Send requests to the server, session_id with Cookies to Server
    2. Server: Extract session_id from Headers Cookies
    3. Server: Find the corresponding data according to session_id and confirm the identity of the client
  4. Code Implementation in Django

    class SessionMiddleware(MiddlewareMixin):
        def __init__(self, get_response=None):
            self.get_response = get_response
            engine = import_module(settings.SESSION_ENGINE)
            self.SessionStore = engine.SessionStore  # Setting Session Storage Class
    
        def process_request(self, request):
            # Getting session ID from Cookie
            session_key = request.COOKIES.get('session_id')
    
            # Get previously saved data through session_key
            request.session = self.SessionStore(session_key)
    
        def process_response(self, request, response):
            try:
                # After the View function finishes, get the session status
                accessed = request.session.accessed
                modified = request.session.modified
                empty = request.session.is_empty()
            except AttributeError:
                pass
            else:
                # If there is a session ID in the Cookie, but the session is empty,
                # Explain that session.flush and other operations have been performed in view.
                # Delete session directly from Cookie
                if 'session_id' in request.COOKIES and empty:
                    response.delete_cookie(
                        settings.SESSION_COOKIE_NAME,
                        path=settings.SESSION_COOKIE_PATH,
                        domain=settings.SESSION_COOKIE_DOMAIN,
                    )
                else:
                    if accessed:
                        patch_vary_headers(response, ('Cookie',))
                    if (modified or settings.SESSION_SAVE_EVERY_REQUEST) and not empty:
                        # Set expiration time
                        if request.session.get_expire_at_browser_close():
                            max_age = None
                            expires = None
                        else:
                            max_age = request.session.get_expiry_age()
                            expires_time = time.time() + max_age
                            expires = cookie_date(expires_time)
    
                        # Save session data and refresh client Cookie
                        if response.status_code != 500:
                            try:
                                request.session.save()
                            except UpdateError:
                                raise SuspiciousOperation(
                                    "The request's session was deleted before the "
                                    "request completed. The user may have logged "
                                    "out in a concurrent request, for example."
                                )
    
                            # Let client add session ID to Cookie
                            response.set_cookie(
                                'session_id',
                                request.session.session_key,
                                max_age=max_age,
                                expires=expires,
                                domain=settings.SESSION_COOKIE_DOMAIN,
                                path=settings.SESSION_COOKIE_PATH,
                                secure=settings.SESSION_COOKIE_SECURE or None,
                                httponly=settings.SESSION_COOKIE_HTTPONLY or None,
                            )
            return response
    
  5. Interview questions:

    1. How to distinguish Cookie from Session?
    2. Describe the HTTP protocol

Verify SMS

  1. Adding new url routing mapping to urls.py
    1. url(r'api/user/submit/vcode', user_api.submit_vcode)
  2. Implement routing mapping:
    1. submit_vcode
      1. Get the mobile phone number, authentication code
      2. Remove the validation code corresponding to this phone number from the cache
      3. Check the validation code for consistency, login or automatic registration
      4. Inconsistent return error

Personal Data Interface Planning

  1. Access to Personal Data Interface
  2. Modifying Personal Data Interface
  3. Upload Personal Portrait Interface

Profile model design (for reference only)

Field Description
location Target City
min_distance Minimum search range
max_distance Maximum search range
min_dating_age Minimum dating age
max_dating_age Maximum dating age
dating_sex Matched sex
vibration Open vibration
only_matche Don't let me see my album for matching people
auto_play Auto Play Video

Difficulties in Development

  1. What is the relationship between Profile and User?
  2. How to build "table association" without using foreign keys in an enterprise?
  3. How should I verify that there are too many fields in the interface for bulk submission?
  4. How to upload the avatar? https://docs.djangoproject.com/zh-hans/2.0/topics/http/file-uploads/
  5. How to save a large number of static files in a large project?
  6. How can slow operations such as uploading files, sending validation codes and image processing be handled to shorten the waiting time of users?

Construction of Database Table Relations

  1. Relational Classification

    • One-to-one relationship
    • One-to-many relationship
    • Many-to-many relationship
  2. Advantages and disadvantages of foreign keys

    • Advantage:
      • Data consistency and integrity are guaranteed by the database itself, and the data is more reliable.
      • Can increase the readability of ER graphs
      • Foreign keys can save development
    • Disadvantages:
      • Performance flaws with additional overhead
      • When the primary key table is locked, the table corresponding to the foreign key is also locked.
      • When deleting the data of the primary key table, the data of the foreign key table should be deleted first.
      • When modifying foreign key table fields, foreign key constraints need to be reconstructed
      • Can't be used in Distributed Environments
      • It's not easy to decouple data
  3. Application scenarios

    • Scenario: Internal systems, traditional enterprise applications can be used (data volume is controllable, database server number is controllable)
    • Not applicable scenario: Internet industry is not recommended to use
  4. Manually build associations

    1. One-to-one: the main table id corresponds to the sub-table id exactly one-to-one
    2. One-to-many: Add the unique table id field to the "many" table
    3. Many-to-many: Create relational tables in which only the IDs of two associated entries are stored
    4. Reflections on Blog Cases
      1. The relationship between user and text: one-to-many
      2. User-Collection Relations: Many-to-Many
      3. User-role-permission relationship
      4. User-role: one-to-many
        2. Role-permission: many-to-many
  5. Subtables can be associated by property

    • property usage

      class Box:
          def __init__(self):
              self.l = 123
              self.w = 10
              self.h = 80
      
          @property
          def V(self):
              return self.l * self.w * self.h
      
      b = Box()
      print(b.V)
      
    • Association operations on subtables

      class User(models.Model):
          ...
          user_id = models.IntergerField()
          ...
      
          @property
          def user_profile(self):
              if not hasattr(self, '_profile'):
                  self._profile = Profile.objects.get(user_id=self.user_id)
              return self._profile
      
      
      
      class User(models.Model):
      ...
          demo_id = models.IntegerField()
          ...
      
          @property
      def demo(self):
              if not hasattr(self, '_demo'):
              self._demo = Demo.objects.get(id=self.demo_id)
              return self._demo
      
      

    class Demo(models.Model):
    xxx = models.CharField()
    yyy = models.CharField()

     user = User.objects.get(id=123)
    

    print(user.demo.xxx)
    print(user.demo.yyy)
    ```

    • Attribute values can also be cached using cached_property

      from django.utils.functional import cached_property
      
      class User(models.Model):
          year = 1990
          month = 10
          day = 29
      
          @cached_property
          def age(self):
              today = datetime.date.today()
              birth_date = datetime.date(self.year, self.month, self.day)
              times = today - birth_date
              return times.days // 365
      
  • Interview questions:
    • Complex SQL Queries: Grouping, Connecting
      • The Student table has: id and name fields
      • Score tables are: student_id, exam_id, object_id, score
      • Ask:
        1. The best student in an exam
      • Advanced thinking: the top three students in each exam?
        • Tip: Window function.

Form form validation in Django

  • Django Form Core Function: Data Validation

  • The < form > tab in the web page

    • The method of the < form > tag can only be POST or GET
    • When method=POST, the form data is in the body part of the request
    • When method=GET, form data appears in the URL
  • Properties and Methods of Form Objects

    • form.is_valid(): form validation
    • form.has_changed(): Check for modifications
    • Form. clean < field > (): Special cleaning and validation for a field
    • Form. cleaned_data ['field name']: The cleaned data is stored in this property
  • Definition and use of Form

    from django import forms
    
    class TestForm(forms.Form):
        TAGS = (
            ('py', 'python'),
            ('ln', 'linux'),
            ('dj', 'django'),
        )
        fid = forms.IntegerField()
        name = forms.CharField(max_length=10)
        tag = forms.ChoiceField(choices=TAGS)
        date = forms.DateField()
    
    POST = {'fid': 'bear',
            'name': 'hello-1234567890',
            'tag': 'django',
            'date': '2017/12/17'}
    form = TestForm(POST)
    print(form.is_valid())
    print(form.cleaned_data)  # The cleaned_data attribute is added dynamically when the is_valid function is executed
    print(form.errors)
    
  • Model Form can be created from the corresponding Models

    class UserForm(ModelForm):
        class Meta:
            model = User
            fields = ['name', 'birth']
    

Cloud Service Market

  1. AWS: Amazon: Amazon Web Service, the world leader
    1. Cloud server: EC2: elastic cloud computer
    2. Cloud Storage: S3: simple storage service
    3. Cloud database: RDS: relationship database service: MySQL
    4. Message queue: SQS: Simple Queue Service
    5. Full-text retrieval:
    6. Cloud Data Warehouse: redshift
  2. Azure: Microsoft: There are a lot of linux servers, all kinds of services.
  3. Google Cloud
  4. Ali Yun: First in China
    1. Cloud Server: ECS
    2. Cloud Storage: OSS
    3. Cloud database: RDS
  5. Tencent Cloud
    1. ...
  6. Not worth mentioning cloud:
    1. Jinshan Cloud
    2. Baidu Cloud
    3. Huaweiyun
  7. Special Cloud:
    1. Storage:
      1. Seven oxen
        1. Picture zooming:
          1. New Picture Style
          2. Choose the scaling method you want
          3. give a name
          4. Preservation
          5. Usage method:
            1. http://pvhv7tfry.bkt.clouddn.com/space_city.jpg?imageView2/1/w/200/h/200/q/75|imageslim
        2. Gradual display of pictures:
          1.
      2. Shoot again
    2. Live broadcast:
    3. Short Message: Cloud News
    4. E-mail: sendcloud
  8. How to build services after going to the cloud:
    1. Only use cloud servers, others are implemented in the cloud servers themselves.
      1. Dependent on the cloud, the cost of changing or leaving the cloud is extremely low
      2. More people and longer development cycle
    2. Make full use of all kinds of cloud services
      1. Saving manpower cost, using fewer people and short development cycle
      2. Depending on cloud services, the cost of changing or leaving the cloud is high
      3. Make High Availability on Cloud
  9. Not in the clouds,
    1. Traditional Business
    2. Pure Traffic Service: Video Distribution
  10. Interview questions:
  11. What cloud services do you use?
  12. What functions are used?
  13. How much?

Posted by Zjoske on Wed, 28 Aug 2019 05:58:56 -0700