def on_post(self, model):
        from django.contrib.auth.models import User
        from xdj import send_email, pymqr, medxdb
        from django.conf import settings
        model.ng_response = xdj.dobject(
            error_type="user_is_existing",
            data=xdj.dobject(
                username=model.post_data.username[0],
                email=model.post_data.email[0],
                firstname=model.post_data.firstname[0],
                lastname=model.post_data.lastname[0],
            ))
        count_of_users = User.objects.filter(
            username=model.post_data.username[0]).count()
        if count_of_users > 0:
            model.ng_response.error_type = "user_is_existing"
            return self.render(model)
        count_of_users = User.objects.filter(
            email=model.post_data.email[0]).count()
        if count_of_users > 0:
            model.ng_response.error_type = "email_is_existing"
            return self.render(model)
        user = User.objects.create_user(model.post_data.username[0],
                                        model.post_data.email[0],
                                        model.post_data.password[0])
        user.first_name = model.post_data.firstname[0]
        user.last_name = model.post_data.lastname[0]
        user.is_active = False
        user.save()
        import uuid
        qr = pymqr.query(medxdb.db(), settings.COLLECTION_TOKENS)
        token = uuid.uuid4()
        token = uuid.uuid4().__str__()
        import datetime
        qr.insert(token=token,
                  username=model.post_data.username[0],
                  created_on=datetime.datetime.utcnow()).commit()
        data = dict(url=model.appUrl + "/active_account/" + token,
                    username=user.username,
                    first_name=user.first_name,
                    last_name=user.last_name,
                    register_on=datetime.datetime.utcnow())
        send_email.send_email_by_template(
            user.email, model.request.LANGUAGE_CODE, "active_account_lms",
            "Active your account", "Hi {{username}},<br/> "
            "Please click <a url='{{url}}'>{{url}}</a> to active your account. <br/> "
            "Thank you for register.", data)

        return model.redirect(model.appUrl + "/register_message")
示例#2
0
 def on_get(self, model):
     import branding
     model.data = xdj.dobject()
     model.data.courses = branding.get_visible_courses()
     for item in model.data.courses:
         item.key = item.id.html_id()
     return self.render(model)
示例#3
0
    def __view_exec__(self, request, *args, **kwargs):
        import xdj
        from django.http import HttpResponse
        from django.shortcuts import redirect
        model = self.create_client_model(request)
        model.params = xdj.dobject(*args, **kwargs)
        if request.build_absolute_uri(self.rel_login_url).lower() != model.currentUrl.lower():
            if not self.on_authenticate(model):
                return redirect(model.appUrl + "/" + self.rel_login_url)
        if request.method == 'GET':
            return self.on_get(model)
        else:
            if not request.META.has_key("HTTP_AJAX_POST"):
                if request._get_post()!={}:
                    model.post_data.__dict__.update(
                        request._get_post()
                    )
                elif request.body and request.body != "":
                    from xdj import JSON
                    model.post_data.__dict__.update(
                        JSON.from_json(request.body)
                    )


                return self.on_post(model)
            else:
                try:
                    from xdj import JSON
                    if request.body.__len__()>0:
                        model.post_data.__dict__.update(JSON.from_json(request.body))
                    method_name = request.META["HTTP_AJAX_POST"]
                    method_items = method_name.split('.')
                    obj= self
                    for i in range(0,method_items.__len__()-1):
                        obj=getattr(obj,method_items[i])
                    method_exec = getattr(obj,method_items[method_items.__len__()-1])
                    ret = method_exec(model)
                    from util.json_request import JsonResponse
                    if type(ret) == JsonResponse:
                        return ret
                    json_data = JSON.to_json(ret)


                    return HttpResponse(json_data, content_type="application/json")
                except AttributeError as ex:
                    if not hasattr(obj,method_items[method_items.__len__()-1]):
                        raise Exception("{0} was not found in {1} or error '{2}'".format(
                            request.META["HTTP_AJAX_POST"],
                            self.on_get.im_func.func_code.co_filename,
                            ex.message
                        ))
                    else:
                        raise ex
示例#4
0
        def doUpdateItem(self,sender):
            """
            Update account info
            :param sender:
            :return:
            """
            try:
                from xdj_models.models import CoursewareUserOrgs

                user_data= xdj.dobject(sender.post_data.user)

                users = self.owner.__get_user_models__().filter(username=user_data.username).all()
                if users.count()>0:
                    user=self.owner.__get_user_models__().get(username=user_data.username)
                    if sender.post_data.user.has_key("org"):
                        org = CoursewareUserOrgs()
                        if org.objects.filter(User=user).count()>0:
                            org.objects.get(User=user).delete()
                        _org = org.objects.create()
                        _org.User = user
                        _org.Org_id = int(sender.post_data.user["org"])
                        _org.save()

                    user.first_name= user_data.first_name
                    user.last_name = user_data.last_name
                    user.email = user_data.email
                    user.is_active = user_data.is_active
                    user.is_staff = user_data.is_staff
                    user.is_superuser=user_data.is_superuser
                    user.save()
                    return dict()
                else:
                    self.check_require_fields(sender.post_data.user, [
                        "username",
                        "email",
                        "password",
                        'confirmPassword'
                    ])
                    user = self.owner.__get_user_models__().create(
                        username=user_data.username,
                        email=user_data.email,
                        password=user_data.password
                    )
                    user.is_active = user_data.__dict__.get("is_active", False)
                    user.is_staff = user.__dict__.get("is_staff",False)
                    user.is_superuser = user_data.__dict__.get("is_superuser", False)
                    user.save()
                    return {}

            except RequireFieldError as ex:
                return dict(error = ex)
            except Exception as ex:
                raise ex
    def doLoadItems(self, sender):
        """
        https://programtalk.com/python-examples-amp/student.models.anonymous_id_for_user/
        :param sender:
        :return:
        """

        import branding
        import courseware
        from xdj_models.enities import courseware as cw
        from xdj import pymqr
        from xdj import medxdb
        from django.contrib.auth.models import User
        import sysadmin
        import datetime
        from django.db.models import Q

        # courseware.models.StudentModule.objects.all()[0].student.last_name
        ret = branding.get_visible_courses()
        qr = pymqr.query(medxdb.db(), cw.modulestore_active_versions)
        for item in ret:
            # course = courseware.models.StudentModule.objects.get(course_id=item.id)
            x = qr.new().match(pymqr.filters.org==item.id.org)\
                .match(pymqr.filters.run==item.id.run)\
                .match(pymqr.filters.course==item.id.course).object

            #     .match(pymqr.funcs.expr(
            #     (pymqr.docs.org == item.id.org) &
            #     (pymqr.docs.run == item.id.run) &
            #     (pymqr.docs.course == item.id.course)
            # )).object
            from xdj_models.models import CourseAuthors
            fx = CourseAuthors()()
            item.course_id = item.id.__str__()
            if not x.is_empty():
                authors = User.objects.filter(id=x.edited_by)
                if authors.__len__() > 0:
                    sql_items = CourseAuthors().objects.filter(
                        Q(user_id=x.edited_by) & Q(course_id=item.id)).count()
                    item.author = xdj.dobject(username=authors[0].username)
                    if sql_items == 0:
                        fx.user_id = x.edited_by
                        fx.course_id = item.id
                        fx.created_on = datetime.datetime.now()
                        fx.save()
            item.totalActiveStudent = courseware.models.StudentModule.objects.filter(
                course_id=item.id).filter(module_type="course").count()
            """calculate total activates students"""

        return ret
示例#6
0
                    def exec_request_get(self, request, *args, **kwargs):
                        model = self.obj.owner.create_client_model(request)
                        model.params = xdj.dobject(kwargs)

                        def do_rendert(model):
                            return self.obj.owner.render_with_template(
                                model, self.obj.template)

                        self.obj.render = do_rendert
                        if request.method == "GET":
                            if hasattr(self.obj, "on_get"):
                                return self.obj.on_get(model)
                            else:
                                return self.obj.owner.render_with_template(
                                    model, self.obj.template)
                        else:
                            if request.META.has_key("HTTP_AJAX_POST"):
                                exec_method = request.META["HTTP_AJAX_POST"]
                                try:
                                    from xdj import JSON
                                    from django.http import HttpResponse
                                    from exceptions import ValueError
                                    try:
                                        model.post_data.__dict__.update(
                                            JSON.from_json(request.body))
                                    except ValueError as ex:
                                        pass
                                    method = getattr(self.obj, exec_method)
                                    ret = method(model)
                                    json_data = JSON.to_json(ret)
                                    return HttpResponse(
                                        json_data,
                                        content_type="application/json")
                                except AttributeError as ex:
                                    if not hasattr(self.obj, exec_method):
                                        import inspect
                                        code_file = inspect.getfile(
                                            self.obj.__class__)
                                        raise Exception(
                                            "{0} was not found in {1}".format(
                                                exec_method, code_file))
                                    else:
                                        raise ex
示例#7
0
 def get_course_outline(self, model, course_id):
     """
     Get course outline of course_id
     :param model:
     :param course_id:
     :return:
     """
     tab_type = 'courseware'
     # from courseware.courses import get_course_with_access
     course_key = self.get_course_key(course_id)
     with self.get_module_store().bulk_operations(course_key):
         course, privilege = self.get_course_with_access(
             model.request.user, 'load', course_key)
         if not course:
             return None, privilege
         # Render the page
         # tab = CourseTabList.get_tab_by_type(course.tabs, tab_type)
         # page_context = create_page_context(request, course=course, tab=tab)
         from openedx.features.course_experience.utils import get_course_outline_block_tree
         courseware_outline = get_course_outline_block_tree(
             model.request, course_id)
         return xdj.dobject(courseware_outline), privilege
示例#8
0
def load_settings():
    from xdj import dobject
    from xdj import pymqr
    from xdj.medxdb import db
    from django.conf import settings
    global config
    if config == None:
        x = pymqr.query(db(), COLLECTION_SETTINGS).object
        if x.is_empty():
            v = pymqr.query(db(), COLLECTION_SETTINGS).insert(host="...",
                                                              port=25,
                                                              user="******",
                                                              email="...",
                                                              password="******")
            v.commit()
            x = pymqr.query(db(), COLLECTION_SETTINGS).object
            config = dobject(host=x.host,
                             port=x.port,
                             user=x.user,
                             password=x.password,
                             email=x.email)
        else:
            config = x
示例#9
0
 def on_get(self, sender):
     _ = sender._
     sender.menu = [
         xdj.dobject(caption=_ > "System",
                     items=[
                         xdj.dobject(caption=_ > "Accounts",
                                     page="system/users"),
                         xdj.dobject(caption=_ > "Email",
                                     page="system/email_settings")
                     ]),
         xdj.dobject(caption=_ > "Categories",
                     items=[
                         xdj.dobject(caption=_ > "Organization",
                                     page="system/orgs"),
                         xdj.dobject(caption=_ > "Subject",
                                     page="system/subjects"),
                         xdj.dobject(caption=_ > "Courses",
                                     page="couserware/courses")
                     ]),
         xdj.dobject(caption="Resources",
                     items=[
                         xdj.dobject(caption=_ > "Linguistics",
                                     page="linguistics/list")
                     ]),
         xdj.dobject(caption="Database",
                     items=[
                         xdj.dobject(caption="Tables", page="db/tables"),
                         xdj.dobject(caption="SQL", page="db/sql")
                     ])
     ]
     return self.render(sender)