Exemplo n.º 1
0
    def processView(self):
        groupID = self.request.matchdict["group"]
        questionID = self.request.matchdict["question"]
        data = getQuestionDetails(self.request, groupID, questionID)
        number = getMemberNumber(self.request, data["member_id"])
        audios = listAnswers(self.request, self.user.id)
        error_summary = {}
        if data["question_status"] == 1 or data["question_status"] == -1:
            replied = False
        else:
            replied = True
        if self.request.method == "POST":
            data = self.getPostDict()
            setQuestionStatus(self.request, questionID, 2, data["audio_id"])
            for plugin in p.PluginImplementations(p.IIVR):
                plugin.sendReply(self.request, number, data["audio_id"],
                                 questionID)
                break  # Only one plugin implementing IVR will be called
            replied = True

        return {
            "error_summary": error_summary,
            "data": data,
            "groupid": groupID,
            "questionid": questionID,
            "audios": audios,
            "replied": replied,
        }
Exemplo n.º 2
0
 def processView(self):
     # If we logged in then go to dashboard
     if self.request.method == "GET":
         data = {}
     else:
         safe = check_csrf_token(self.request, raises=False)
         if not safe:
             return HTTPNotFound()
         data = variable_decode(self.request.POST)
         if data["user_pass"] != "":
             if data["user_pass"] == data["user_pass2"]:
                 # Load connected plugins and check if they modify the registration of an user
                 continue_registration = True
                 for plugin in p.PluginImplementations(p.IAuthorize):
                     (
                         continue_with_registration,
                         error_message,
                     ) = plugin.before_register(self.request, data)
                     if not continue_with_registration:
                         self.errors.append(error_message)
                         continue_registration = False
                 if continue_registration:
                     added, error_message = register_user(
                         self.request, data)
                     if not added:
                         self.errors.append(error_message)
                     else:
                         # Load connected plugins so they perform actions after the login is performed
                         for plugin in p.PluginImplementations(
                                 p.IAuthorize):
                             plugin.after_register(self.request, data)
                         loginData = data["user_id"]
                         headers = remember(self.request, loginData)
                         return HTTPFound(
                             location=self.request.route_url("dashboard"),
                             headers=headers,
                         )
             else:
                 self.errors.append(
                     self._("The password and its retype are not the same"))
         else:
             self.errors.append(self._("The password cannot be empty"))
     return {"next": next, "data": data}
Exemplo n.º 3
0
 def pluralize(self, noun, size):
     if size == 1:
         return noun
     plural = noun
     # Call connected plugins to see if they have extended or overwrite ushauri pluralize function
     for plugin in p.PluginImplementations(p.IPluralize):
         res = plugin.pluralize(noun, self.request.locale_name)
         if res != "":
             plural = res
     # Will return English pluralization if none of the above happens
     # return pluralize_en(noun)
     return plural
Exemplo n.º 4
0
 def processView(self):
     groupID = self.request.matchdict["group"]
     questionID = self.request.matchdict["question"]
     data = getQuestionDetails(self.request, groupID, questionID)
     number = getMemberNumber(self.request, data["member_id"])
     error_summary = {}
     if self.request.method == "POST":
         print("***************************88")
         print(self.request.POST)
         input_file = self.request.POST["data"].file
         uid = str(uuid.uuid4())
         audioPath = self.request.registry.settings["audioPath"]
         file_path = os.path.join(audioPath, uid + ".wav")
         temp_file_path = file_path + "~"
         input_file.seek(0)
         with open(temp_file_path, "wb") as output_file:
             shutil.copyfileobj(input_file, output_file)
         # Now that we know the file has been fully saved to disk move it into place.
         os.rename(temp_file_path, file_path)
         addAudio(
             self.request,
             uid,
             self.request.POST["audio_desc"],
             uid + ".wav",
             2,
             self.user.id,
         )
         print("***************************88")
         setQuestionStatus(self.request, questionID, 2, uid)
         for plugin in p.PluginImplementations(p.IIVR):
             plugin.sendReply(self.request, number, uid, questionID)
             break  # Only one plugin will send a reply
     return {
         "error_summary": error_summary,
         "data": data,
         "groupid": groupID,
         "questionid": questionID,
     }
Exemplo n.º 5
0
 def processView(self):
     # If we logged in then go to dashboard
     next = self.request.params.get("next") or self.request.route_url(
         "dashboard")
     if self.request.method == "GET":
         userID = self.request.authenticated_userid()
         if userID is not None:
             currentUser = getUserData(self.request, userID)
             if currentUser is not None:
                 return HTTPFound(location=next)
     else:
         safe = check_csrf_token(self.request, raises=False)
         if not safe:
             raise HTTPNotFound()
         data = variable_decode(self.request.POST)
         login = data["login"]
         passwd = data["passwd"]
         user = getUserData(self.request, login)
         if user is not None:
             if checkLogin(login, passwd, self.request):
                 continue_login = True
                 error_message = ""
                 # Load connected plugins and check if they modify the login authorization
                 for plugin in p.PluginImplementations(p.IAuthorize):
                     continue_with_login, error_message = plugin.after_login(
                         self.request, user)
                     if not continue_with_login:
                         self.errors.append(error_message)
                         continue_login = False
                 if continue_login:
                     headers = remember(self.request, login)
                     return HTTPFound(location=next, headers=headers)
             else:
                 self.errors.append(self._("Invalid credentials"))
         else:
             self.errors.append(self._("The user account does not exists"))
     return {"next": next}
Exemplo n.º 6
0
def loadRoutes(config):
    # Call connected to plugins to add any routes before FormShare
    for plugin in p.PluginImplementations(p.IRoutes):
        routes = plugin.before_mapping(config)
        appendToRoutes(routes)

    # FormShare routes
    routes = []
    routes.append(addRoute("home", "/", home_view, "landing/index.jinja2"))
    routes.append(
        addRoute("login", "/login", login_view, "dashboard/login.jinja2"))
    routes.append(
        addRoute("register", "/register", register_view,
                 "dashboard/register.jinja2"))
    routes.append(addRoute("logout", "/logout", logout_view, None))
    routes.append(
        addRoute("dashboard", "/dashboard", dashboard_view,
                 "dashboard/index.jinja2"))
    # Maintenace
    routes.append(
        addRoute(
            "counties",
            "/counties",
            countiesList_view,
            "/dashboard/maintenance/counties/counties.jinja2",
        ))
    routes.append(
        addRoute(
            "addcounty",
            "/counties/add",
            addCounty_view,
            "dashboard/maintenance/counties/add_county.jinja2",
        ))
    routes.append(
        addRoute("deletecounty", "/county/{county}/delete", deleteCounty_view,
                 None))
    routes.append(
        addRoute(
            "modifycounty",
            "/county/{county}/edit",
            modifyCounty_view,
            "dashboard/maintenance/counties/modify_county.jinja2",
        ))

    routes.append(
        addRoute(
            "subcounties",
            "/county/{county}/subcounties",
            subCountiesList_view,
            "dashboard/maintenance/counties/subcounties.jinja2",
        ))
    routes.append(
        addRoute(
            "addsubcounty",
            "/county/{county}/subcounties/add",
            addSubCounty_view,
            "dashboard/maintenance/counties/add_subcounty.jinja2",
        ))
    routes.append(
        addRoute(
            "deletesubcounty",
            "/county/{county}/subcounty/{subcounty}/delete",
            deleteSubCounty_view,
            None,
        ))
    routes.append(
        addRoute(
            "modifysubcounty",
            "/county/{county}/subcounty/{subcounty}/edit",
            editSubCounty_view,
            "dashboard/maintenance/counties/modify_subcounty.jinja2",
        ))

    routes.append(
        addRoute(
            "groups",
            "/groups",
            groupList_view,
            "dashboard/maintenance/groups/groups.jinja2",
        ))
    routes.append(
        addRoute(
            "addgroup",
            "/groups/add",
            addGroup_view,
            "dashboard/maintenance/groups/add_group.jinja2",
        ))
    routes.append(
        addRoute("deletegroup", "/group/{group}/delete", deleteGroup_view,
                 None))
    routes.append(
        addRoute(
            "modifygroup",
            "/group/{group}/modify",
            modifyGroup_view,
            "dashboard/maintenance/groups/edit_group.jinja2",
        ))
    routes.append(addRoute(
        "groupQR",
        "/group/{group}/qr",
        GroupQRCode,
        None,
    ))

    routes.append(
        addRoute(
            "members",
            "/group/{group}/members",
            membersList_view,
            "dashboard/maintenance/groups/members.jinja2",
        ))
    routes.append(
        addRoute(
            "deletemember",
            "/group/{group}/member/{member}/delete",
            deleteMember_view,
            None,
        ))
    routes.append(
        addRoute(
            "modifymember",
            "/group/{group}/member/{member}/modify",
            modifyMember_view,
            "dashboard/maintenance/groups/edit_member.jinja2",
        ))

    routes.append(
        addRoute(
            "agents",
            "/group/{group}/agents",
            agentsList_view,
            "dashboard/maintenance/groups/agents.jinja2",
        ))
    routes.append(
        addRoute("deleteagent", "/group/{group}/agent/{agent}/delete",
                 deleteAgent_view, None))
    routes.append(
        addRoute(
            "addagent",
            "/group/{group}/agents/add",
            addAgent_view,
            "dashboard/maintenance/groups/add_agent.jinja2",
        ))

    routes.append(
        addRoute("users", "/users", userList_view,
                 "dashboard/maintenance/users/users.jinja2"))
    routes.append(
        addRoute(
            "adduser",
            "/users/add",
            addUser_view,
            "dashboard/maintenance/users/add_user.jinja2",
        ))
    routes.append(
        addRoute(
            "deleteuser",
            "/user/{user}/delete",
            deleteUser_view,
            "dashboard/maintenance/users/add_user.jinja2",
        ))
    routes.append(
        addRoute(
            "modifyuser",
            "/user/{user}/modify",
            modifyUser_view,
            "dashboard/maintenance/users/edit_user.jinja2",
        ))
    routes.append(
        addRoute(
            "modifyuserpass",
            "/user/{user}/modifypass",
            modifyUserPass_view,
            "dashboard/maintenance/users/edit_pass.jinja2",
        ))

    routes.append(
        addRoute(
            "menus",
            "/menus",
            menusList_view,
            "dashboard/maintenance/menus/menus.jinja2",
        ))
    routes.append(
        addRoute(
            "addmenu",
            "/menus/add",
            addMenu_view,
            "dashboard/maintenance/menus/add_menu.jinja2",
        ))
    routes.append(
        addRoute(
            "modifymenu",
            "/menu/{menu}/modify",
            modifyMenu_view,
            "dashboard/maintenance/menus/edit_menu.jinja2",
        ))
    routes.append(
        addRoute("deletemenu", "/menu/{menu}/delete", deleteMenu_view, None))

    routes.append(
        addRoute(
            "items",
            "/menu/{menu}/items",
            menusItemsList_view,
            "dashboard/maintenance/menus/items.jinja2",
        ))
    routes.append(
        addRoute(
            "additem",
            "/menu/{menu}/add",
            addMenuItem_view,
            "dashboard/maintenance/menus/add_item.jinja2",
        ))
    routes.append(
        addRoute(
            "edititem",
            "/menu/{menu}/item/{item}/edit",
            editMenuItem_view,
            "dashboard/maintenance/menus/edit_item.jinja2",
        ))
    routes.append(
        addRoute("deleteitem", "/menu/{menu}/item/{item}/delete",
                 deleteMenuItem_view, None))
    routes.append(
        addRoute("itemstart", "/menu/{menu}/item/{item}/start",
                 setMenuItemAsStart_view, None))
    routes.append(
        addRoute(
            "responses",
            "/menu/{menu}/item/{item}/responses",
            menusItemsResponsesList_view,
            "dashboard/maintenance/menus/responses.jinja2",
        ))
    routes.append(
        addRoute(
            "addresponse",
            "/menu/{menu}/item/{item}/responses/add",
            addItemResponse_view,
            "dashboard/maintenance/menus/add_response.jinja2",
        ))
    routes.append(
        addRoute(
            "deleteresponse",
            "/menu/{menu}/item/{item}/response/{resp}/delete",
            deleteItemResponse_view,
            None,
        ))

    routes.append(
        addRoute(
            "audios",
            "/audios",
            audiosList_view,
            "dashboard/maintenance/audios/audios.jinja2",
        ))
    routes.append(
        addRoute(
            "addaudio",
            "/audios/add",
            addAudio_view,
            "dashboard/maintenance/audios/add_audio.jinja2",
        ))
    routes.append(
        addRoute(
            "editaudio",
            "/audios/{audio}/edit",
            modifyAudio_view,
            "dashboard/maintenance/audios/edit_audio.jinja2",
        ))
    routes.append(
        addRoute("deleteaudio", "/audios/{audio}/delete", deleteAudio_view,
                 None))
    routes.append(
        addRoute("playaudio", "/audios/{audio}/play", getAudio_view, None))
    routes.append(
        addRoute(
            "recordaudio",
            "/audios/record",
            recordAudio_view,
            "dashboard/maintenance/audios/record.jinja2",
        ))

    routes.append(
        addRoute(
            "modifyquestion",
            "/group/{group}/question/{question}/modify",
            modifyQuestion_view,
            "dashboard/maintenance/questions/edit_question.jinja2",
        ))
    routes.append(
        addRoute(
            "replytomember",
            "/group/{group}/question/{question}/reply",
            replyToMember_view,
            "dashboard/maintenance/questions/reply_to_member.jinja2",
        ))
    routes.append(
        addRoute(
            "recordandreplytomember",
            "/group/{group}/question/{question}/record",
            recordAndReplyToMember_view,
            "dashboard/maintenance/questions/record_a_reply.jinja2",
        ))

    # ODK Forms
    routes.append(
        addRoute("odkformlist", "/register/{group}/formList", formList_view,
                 None))
    routes.append(
        addRoute("odksubmission", "/register/{group}/submission",
                 submission_view, None))
    routes.append(
        addRoute("odkpush", "/register/{group}/push", push_view, None))
    routes.append(
        addRoute("odkxmlform", "/register/{group}/xmlform", XMLForm_view,
                 None))
    routes.append(
        addRoute("odkmanifest", "/register/{group}/manifest", manifest_view,
                 None))
    routes.append(
        addRoute(
            "odkmediafile",
            "/register/{group}/manifest/mediafile/{fileid}",
            mediaFile_view,
            None,
        ))

    appendToRoutes(routes)

    # Add the not found route
    config.add_notfound_view(notfound_view, renderer="404.jinja2")

    # Call connected plugins to add any routes after FormShare
    for plugin in p.PluginImplementations(p.IRoutes):
        routes = plugin.after_mapping(config)
        appendToRoutes(routes)

    # Now add the routes and views to the Pyramid config
    for curr_route in route_list:
        config.add_route(curr_route["name"], curr_route["path"])
        config.add_view(
            curr_route["view"],
            route_name=curr_route["name"],
            renderer=curr_route["renderer"],
        )
Exemplo n.º 7
0
def load_environment(settings, config, apppath):
    # Add the session factory to the confing
    config.set_session_factory(my_session_factory)

    # Add render subscribers for internationalization
    config.add_translation_dirs("ushauri:locale")
    config.add_subscriber(
        "ushauri.i18n.i18n.add_renderer_globals", "pyramid.events.BeforeRender"
    )
    config.add_subscriber(
        "ushauri.i18n.i18n.add_localizer", "pyramid.events.NewRequest"
    )

    # Register jinja2
    config.registry.settings["jinja2.extensions"] = [
        "jinja2.ext.i18n",
        "jinja2.ext.do",
        "jinja2.ext.with_",
        SnippetExtension,
        extendThis,
        CSSResourceExtension,
        JSResourceExtension,
    ]
    config.include("pyramid_jinja2")

    # Add url_for_static to the request so plugins can use static resources
    config.add_request_method(__url_for_static, "url_for_static")
    # Add active resources to the request. This control the injection of resources into a request
    config.add_request_method(requestResources, "activeResources", reify=True)
    # Add a series of helper functions to the request like pluralize
    config.add_request_method(__helper, "h", reify=True)

    # Add core library and resources
    createResources(apppath, config)

    # Add the template directories
    templatesPathArray = []
    templatesPath = os.path.join(apppath, "templates")
    templatesPathArray.append(templatesPath)
    config.add_settings(templatesPaths=templatesPathArray)

    # Add the static view
    staticPath = os.path.join(apppath, "static")
    config.add_static_view("main_static", staticPath, cache_max_age=3600)
    # Add the template directories to jinja2
    config.add_jinja2_search_path(templatesPath)

    # Load all connected plugins
    p.load_all(settings)

    # Load any change in the configuration done by connected plugins
    for plugin in p.PluginImplementations(p.IConfig):
        plugin.update_config(config)

    # Call any connected plugins to add their libraries
    for plugin in p.PluginImplementations(p.IResource):
        pluginLibraries = plugin.add_libraries(config)
        for library in pluginLibraries:
            r.addLibrary(library["name"], library["path"], config)

    # Call any connected plugins to add their CSS Resources
    for plugin in p.PluginImplementations(p.IResource):
        cssResources = plugin.add_CSSResources(config)
        for resource in cssResources:
            r.addCSSResource(
                resource["libraryname"],
                resource["id"],
                resource["file"],
                resource["depends"],
            )

    # Call any connected plugins to add their JS Resources
    for plugin in p.PluginImplementations(p.IResource):
        jsResources = plugin.add_JSResources(config)
        for resource in jsResources:
            r.addJSResource(
                resource["libraryname"],
                resource["id"],
                resource["file"],
                resource["depends"],
            )

    # Call any connected plugins to add their modifications into the schema
    schemas_allowed = [
        "user",
        "project",
        "enumerator",
        "enumgroup",
        "datauser",
        "datagroup",
        "form",
    ]
    for plugin in p.PluginImplementations(p.ISchema):
        schemaFields = plugin.update_schema(config)
        for field in schemaFields:
            if field["schema"] in schemas_allowed:
                addColumnToSchema(
                    field["schema"], field["fieldname"], field["fielddesc"]
                )

    # Call any connected plugins to add their tables
    for plugin in p.PluginImplementations(p.IDatabase):
        plugin.update_schema(config, Base)

    # run configure_mappers after calling plugins implementing IDatabase
    # all relationships can be setup
    configure_mappers()

    # print "**********************88"
    # for table in metadata.sorted_tables:
    #     print table.name
    # print "**********************88"

    # jinjaEnv is used by the jinja2 extensions so we get it from the config
    jinjaEnv = config.get_jinja2_environment()

    # setup the jinjaEnv template's paths for the extensions
    initialize(config.registry.settings["templatesPaths"])

    # Finally we load the routes
    loadRoutes(config)