Skip to content

Instantly share code, notes, and snippets.

@AsadDevAI
Last active September 23, 2022 21:47
Show Gist options
  • Save AsadDevAI/c13be5987399edc6c163133f2c5c8f5e to your computer and use it in GitHub Desktop.
Save AsadDevAI/c13be5987399edc6c163133f2c5c8f5e to your computer and use it in GitHub Desktop.
Guide to create and import users maus data into edx LMS

Open LMS Shell

make lms-shell

Run shell environment

./manage.py lms shell

Create Users

from django.contrib.auth.models import User
for i in range(1,10):
  User.objects.create(username='Asad'+str(i),password='123', email='asad@test'+str(i)+'.com')

Create a shell script to import Maus:

import random
import time

from django.contrib.auth.models import User

from edly_panel_app.models import EdlyUserActivity


def str_time_prop(start, end, format, prop):
    """Get a time at a proportion of a range of two formatted times.

    start and end should be strings specifying times formated in the
    given format (strftime-style), giving an interval [start, end].
    prop specifies how a proportion of the interval to be taken after
    start.  The returned time will be in the specified format.
    """

    stime = time.mktime(time.strptime(start, format))
    etime = time.mktime(time.strptime(end, format))

    ptime = stime + prop * (etime - stime)

    return time.strftime(format, time.localtime(ptime))


def random_date(start, end, prop):
    return str_time_prop(start, end, '%Y-%m-%d', prop)


def enter_each_user_activity():
    from openedx.features.edly.models import EdlySubOrganization
    edly_sub_org = EdlySubOrganization.objects.get(id=1)
    for i in range(10):
        users = User.objects.all()
        for user in users:
            try:
                EdlyUserActivity.objects.create(user=user, edly_sub_organization=edly_sub_org)
            except:
                pass

        activities = EdlyUserActivity.objects.all()

        for activity in activities:
            try:
                activity.activity_date = random_date('2010-01-01', '2020-01-01', random.random())
                activity.save()
            except:
                pass
                

Import maus data using script

from edly_panel_app.data_import_script import enter_each_user_activity
enter_each_user_activity()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment