Пример #1
0
def get_item(language, appname, view, key, value):
    from django.conf import settings
    from xdj import pymqr, medxdb
    if not hasattr(settings, "COLLECTION_LANGUAGE"):
        raise Exception(
            "It looks like you forgot set 'COLLECTION_LANGUAGE' in django settings"
        )
    qr = pymqr.query(medxdb.db(), settings.COLLECTION_LANGUAGE)
    if value == None:
        value = key
    value = value.lstrip(" ").rstrip(" ")
    key = key.lstrip(" ").rstrip(" ").lower()
    appname = appname.lower()
    view = view.lower()
    language = language.lower()
    x = qr.new().where([
        pymqr.filters.language == language, pymqr.filters.app == appname,
        pymqr.filters.view == view, pymqr.filters.key == key
    ]).object
    if x.is_empty():
        qr.new().insert(language=language,
                        app=appname,
                        view=view,
                        key=key,
                        value=value).commit()
        return value
    else:
        return x.value
Пример #2
0
 def DoLoadItem(self, model):
     from django.conf import settings
     from xdj import pymqr, medxdb
     from bson import ObjectId
     qr = pymqr.query(medxdb.db(), settings.COLLECTION_LANGUAGE)
     x = qr.new().where(
         pymqr.filters._id == ObjectId(model.post_data.id)).object
     return x
Пример #3
0
 def DoSaveItem(self, model):
     from django.conf import settings
     from xdj import pymqr, medxdb, clear_language_cache
     from bson import ObjectId
     qr = pymqr.query(medxdb.db(), settings.COLLECTION_LANGUAGE)
     x = qr.new().where(
         pymqr.filters._id == ObjectId(model.post_data._id))
     x.set(value=model.post_data.value)
     x.commit()
     clear_language_cache()
     return model.post_data
Пример #4
0
 def DoLoadItems(self, model):
     from django.conf import settings
     from xdj import pymqr, medxdb
     import re
     qr = pymqr.query(medxdb.db(), settings.COLLECTION_LANGUAGE)
     if model.post_data.__dict__.has_key("search"):
         qr.pipeline.append({
             "$match": {
                 "value": re.compile(model.post_data.search, re.IGNORECASE)
             }
         })
     ret = qr.get_page(100, 0)
     return ret
     pass
Пример #5
0
    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 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
Пример #7
0
    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")
Пример #8
0
def get_email_template(language, name, defaultSubject, defaultTemplate, model):
    global cache
    if cache == None:
        cache = {}
    if not cache.has_key(language):
        cache.update({language: {}})
    if cache[language].has_key(name):
        return cache[language][name]
    lock.acquire()
    try:

        import json
        from xdj import pymqr, medxdb
        from django.conf import settings
        import os
        if not hasattr(settings, "EMAIL_TEMPLATE_PATH"):
            raise Exception(
                "It looks like you forgot put 'EMAIL_TEMPLATE_PATH' in settings of django"
            )
        fields = unwind_model(model)
        qr = pymqr.query(medxdb.db(), COLLECTION_TEMPLATES)
        tmp = qr.new().where(
            [pymqr.filters.language == language,
             pymqr.filters.name == name]).object
        if tmp.is_empty():
            language_dir = os.sep.join(
                [settings.EMAIL_TEMPLATE_PATH, language])
            if not os.path.exists(language_dir):
                os.makedirs(language_dir)
            template_dir = os.sep.join([language_dir, name])
            if not os.path.exists(template_dir):
                os.makedirs(template_dir)
            model_file = os.sep.join([template_dir, "model.json"])
            if not os.path.exists(model_file):
                f_model = open(model_file, "w")
                f_model.write(json.dumps(fields))
                f_model.close()
            else:
                f_model = open(model_file, "r")
                txt = f_model.read()
                f_model.close()
                fields = json.loads(txt)

            tmp_file = os.sep.join([template_dir, "template.html"])
            if not os.path.exists(tmp_file):
                f_tmp = open(tmp_file, "w")
                f_tmp.write(defaultTemplate)
                f_tmp.close()
            else:
                f_tmp = open(tmp_file, "r")
                txt = f_tmp.read()
                defaultTemplate = txt

            tmp_subject = os.sep.join([template_dir, "subject.html"])
            if not os.path.exists(tmp_subject):
                f_tmp = open(tmp_subject, "w")
                f_tmp.write(defaultSubject)
                f_tmp.close()
            else:
                f_tmp = open(tmp_subject, "r")
                txt = f_tmp.read()
                defaultSubject = txt

            ret = qr.new().insert(language=language,
                                  name=name,
                                  template=defaultTemplate,
                                  subject=defaultSubject,
                                  fields=fields)
            ret.commit()
            cache[language].update({
                name: {
                    "fields": fields,
                    "content": defaultTemplate,
                    "subject": defaultSubject
                }
            })
        else:
            cache[language].update({
                name: {
                    "fields": tmp.fields,
                    "content": tmp.template,
                    "subject": tmp.subject
                }
            })
        lock.release()
        return cache[language][name]
    except Exception as ex:
        lock.release()
        raise ex