Forums

Django Threads and oauth

I have written a Django program which copies information stored in the Django model into Google calendar. So, a user logs in, selects some parameters and clicks the Update calendar button. The updating takes about 2 or 3 minutes. Meanwhile, the user doesn't get any feedback from the application. It is a bit, unprofessional. So I thought to make the updating process asynchronous. I tried threading and it works beautifully. But threads are not allowed in pythonanywhere.

I also thought about a scheduled task. I use the Django OAuth Toolkit. This toolkit requires that the user has to authorize the connection between the Django app and the Google calendar, so that the application can modify the calendar. I think that background tasks cannot be used in this case.

I have run out of ideas

Can you help me how to deal with this problem without threads?

Could you tell us a bit more about your code? What is the view that takes 2-3 minutes doing? Is it making calls to a Google API?

Exactly

This is the code simplified:

from apiclient.discovery import build
from oauth2client.contrib.django_util import decorators

@decorators.oauth_required # this decorator needs a 'request' parameter
def updatecal(request, person_id, ...):

    # get service    
    service = build(serviceName='calendar', version='v3', http=request.oauth.http )

    # get Google Calendar
    goocal = getgcal(service, ... )

    # query the Django model
    query = Table.objects.filter(person = person_id, date__gte=datefrom, date__lte=dateto )

    # for every record in query, insert the record in the Google Calendar
    for record in query:  
        insert_event(service, goocal, ... )

# get the calendar 
def getgcal( service , ...):
    # in this function I call:
    service.calendarList().list(pageToken=page_token).execute()
    calendar = service.calendars().insert(body=calendar_in).execute()

#  insert event into calendar
def insert_event( service, goocal, ... ):

    # check if the event already exists
    event_found = getcalevent(service, goocal,...)

    # add an event to calnou
    ins_event = service.events().insert(calendarId=goocal['id'], body=event).execute()

# get an event from the goocal calendar
def getcalevent(service, goocal, ... ):
    # in this function I call:
    events = service.events().list(calendarId=goocal['id'], 
                                    timeMax = starttime,
                                    timeMin = endtime,
                                    pageToken=page_token
                                    ).execute()

Can you do the first step of the oauth authentication (where the user authorises you to access their calendar) in a normal web page, and then save the token to the database, and then have a scheduled task that can pick up that token, and make the subsequent updates to the calendar?

Thanks for your advice I can give it a try. I'll let you know if I can save and use that token...