Uncategorized

Connecting multiple Reddit accounts associated with one user in Python-Django


I want to create a website like postpone using Django and PRAW and I want to create a service that user can connect his/her multiple Reddit accounts and he/she can create schedule posts on reddit

so far, I did this. I created custom user model:

# customers/models.py
import uuid
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager


class CustomerManager(BaseUserManager):

    def create_user(self, email, password=None):
        if not email:
            raise ValueError('Users must have email')
        
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name, password=None):
        user = self.create_user(email=self.normalize_email(email), password=password)
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class Customer(AbstractBaseUser):
    email = models.EmailField(max_length=60, unique=True)
    data_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'

    objects = CustomerManager()

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True

And I added my reddit account informations such as client_id, client_secret and user_agent:

# customers/reddit_conf.py
import praw
from django.http import HttpResponse
from django.shortcuts import redirect
from oauthlib.oauth2 import WebApplicationClient
from django.contrib.auth import get_user_model
import requests
import secrets

User = get_user_model()

# Reddit App Configuration
client_id = "****************",
client_secret = "**************",
redirect_uri='http://127.0.0.1:8000',
user_agent="****** by u/******",
ratelimit_seconds=650,

def reddit_authorization(request):
    reddit = WebApplicationClient(client_id)
    
    STATE = request.session['state'] = secrets.token_urlsafe(16)

    request_uri = reddit.prepare_request_uri(
        "https://www.reddit.com/api/v1/authorize",
        redirect_uri=redirect_uri,
        scope=["identity", "read", "submit"],
        state=STATE,
        user_agent=user_agent,
        ratelimit_seconds=ratelimit_seconds
    )
    return redirect(request_uri)

def reddit_callback(request):
    code = request.GET.get("code")

    if code:
        reddit = WebApplicationClient(client_id)
        token_url, headers, body = reddit.prepare_token_request(
            "https://www.reddit.com/api/v1/access_token",
            authorization_response=request.build_absolute_uri(),
            code=code,
        )
        token_response = requests.post(
            token_url,
            headers=headers,
            data=body,
            auth=(client_id, client_secret),
        )
        reddit.parse_request_body_response(token_response.json())

    return redirect("home")

What should I do?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *