Ejemplo n.º 1
0
    def testSQLSyncToCouch(self):
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)

        # Test Create
        sql_obj = Keyword()
        self.setRandomSQLObjectValues(sql_obj)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)

        couch_obj = SurveyKeyword.get(sql_obj.couch_id)
        self.compareObjects(couch_obj, sql_obj)
        self.assertTrue(SurveyKeyword.get_db().get_rev(couch_obj._id).startswith('2-'))

        # Test Update
        self.setRandomSQLObjectValues(sql_obj)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)
        couch_obj = SurveyKeyword.get(sql_obj.couch_id)
        self.compareObjects(couch_obj, sql_obj)
        self.assertTrue(SurveyKeyword.get_db().get_rev(couch_obj._id).startswith('3-'))

        # Test Delete
        couch_id = couch_obj._id
        sql_obj.delete()
        with self.assertRaises(ResourceNotFound):
            SurveyKeyword.get(couch_id)
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)
Ejemplo n.º 2
0
 def keyword(self):
     if self.keyword_id is None:
         raise Http404()
     sk = SurveyKeyword.get(self.keyword_id)
     if sk.domain != self.domain:
         raise Http404()
     return sk
Ejemplo n.º 3
0
 def keyword(self):
     if self.keyword_id is None:
         raise Http404()
     sk = SurveyKeyword.get(self.keyword_id)
     if sk.domain != self.domain:
         raise Http404()
     return sk
Ejemplo n.º 4
0
def add_keyword(request, domain, keyword_id=None):
    sk = None
    if keyword_id is not None:
        sk = SurveyKeyword.get(keyword_id)
        if sk.domain != domain:
            raise Http404

    if request.method == "POST":
        form = KeywordForm(request.POST)
        form._cchq_domain = domain
        form._sk_id = sk._id if sk is not None else None
        if form.is_valid():
            if sk is None:
                sk = SurveyKeyword(domain=domain)
            sk.keyword = form.cleaned_data.get("keyword")
            sk.form_type = form.cleaned_data.get("form_type")
            sk.form_unique_id = form.cleaned_data.get("form_unique_id")
            sk.delimiter = form.cleaned_data.get("delimiter")
            sk.use_named_args = form.cleaned_data.get("use_named_args", False)
            sk.named_args = form.cleaned_data.get("named_args")
            sk.named_args_separator = form.cleaned_data.get(
                "named_args_separator")
            sk.save()
            return HttpResponseRedirect(
                reverse("manage_keywords", args=[domain]))
    else:
        initial = {}
        if sk is not None:
            initial = {
                "keyword":
                sk.keyword,
                "form_unique_id":
                sk.form_unique_id,
                "form_type":
                sk.form_type,
                "use_custom_delimiter":
                sk.delimiter is not None,
                "delimiter":
                sk.delimiter,
                "use_named_args":
                sk.use_named_args,
                "use_named_args_separator":
                sk.named_args_separator is not None,
                "named_args": [{
                    "name": k,
                    "xpath": v
                } for k, v in sk.named_args.items()],
                "named_args_separator":
                sk.named_args_separator,
            }
        form = KeywordForm(initial=initial)

    context = {
        "domain": domain,
        "form_list": get_form_list(domain),
        "form": form,
        "keyword": sk,
    }

    return render(request, "reminders/partial/add_keyword.html", context)
Ejemplo n.º 5
0
    def get_keyword_display(self, keyword_id, content_cache):
        if keyword_id in content_cache:
            args = content_cache[keyword_id]
            return self.table_cell(*args)
        try:
            keyword = SurveyKeyword.get(keyword_id)
            if keyword.deleted():
                display = '%s %s' % (keyword.description,
                                     _('(Deleted Keyword)'))
                display_text = display
            else:
                urlname = (EditStructuredKeywordView.urlname
                           if keyword.is_structured_sms() else
                           EditNormalKeywordView.urlname)
                display = '<a target="_blank" href="%s">%s</a>' % (
                    reverse(urlname, args=[keyword.domain, keyword_id]),
                    keyword.description,
                )
                display_text = keyword.description
            args = (display_text, display)
        except ResourceNotFound:
            args = ('-', '-')

        content_cache[keyword_id] = args
        return self.table_cell(*args)
Ejemplo n.º 6
0
 def get_deleted_item_data(self, item_id):
     try:
         s = SurveyKeyword.get(item_id)
     except ResourceNotFound:
         raise Http404()
     if s.domain != self.domain or s.doc_type != "SurveyKeyword":
         raise Http404()
     s.delete()
     return {"itemData": self._fmt_deleted_keyword_data(s), "template": "keyword-deleted-template"}
Ejemplo n.º 7
0
 def get_deleted_item_data(self, item_id):
     try:
         s = SurveyKeyword.get(item_id)
     except ResourceNotFound:
         raise Http404()
     if s.domain != self.domain or s.doc_type != "SurveyKeyword":
         raise Http404()
     s.retire()
     return {
         'itemData': self._fmt_keyword_data(s),
         'template': 'keyword-deleted-template',
     }
Ejemplo n.º 8
0
def add_keyword(request, domain, keyword_id=None):
    sk = None
    if keyword_id is not None:
        sk = SurveyKeyword.get(keyword_id)
        if sk.domain != domain:
            raise Http404
    
    if request.method == "POST":
        form = KeywordForm(request.POST)
        form._cchq_domain = domain
        form._sk_id = sk._id if sk is not None else None
        if form.is_valid():
            if sk is None:
                sk = SurveyKeyword(domain=domain)
            sk.keyword = form.cleaned_data.get("keyword")
            sk.form_type = form.cleaned_data.get("form_type")
            sk.form_unique_id = form.cleaned_data.get("form_unique_id")
            sk.delimiter = form.cleaned_data.get("delimiter")
            sk.use_named_args = form.cleaned_data.get("use_named_args", False)
            sk.named_args = form.cleaned_data.get("named_args")
            sk.named_args_separator = form.cleaned_data.get("named_args_separator")
            sk.save()
            return HttpResponseRedirect(reverse("manage_keywords", args=[domain]))
    else:
        initial = {}
        if sk is not None:
            initial = {
                "keyword" : sk.keyword,
                "form_unique_id" : sk.form_unique_id,
                "form_type" : sk.form_type,
                "use_custom_delimiter" : sk.delimiter is not None,
                "delimiter" : sk.delimiter,
                "use_named_args" : sk.use_named_args,
                "use_named_args_separator" : sk.named_args_separator is not None,
                "named_args" : [{"name" : k, "xpath" : v} for k, v in sk.named_args.items()],
                "named_args_separator" : sk.named_args_separator,
            }
        form = KeywordForm(initial=initial)
    
    context = {
        "domain" : domain,
        "form_list" : get_form_list(domain),
        "form" : form,
        "keyword" : sk,
    }
    
    return render(request, "reminders/partial/add_keyword.html", context)
Ejemplo n.º 9
0
    def get_keyword_display(self, keyword_id, content_cache):
        if keyword_id in content_cache:
            return content_cache[keyword_id]
        try:
            keyword = SurveyKeyword.get(keyword_id)
            if keyword.deleted():
                display = '%s %s' % (keyword.description, _('(Deleted Keyword)'))
            else:
                urlname = (EditStructuredKeywordView.urlname if keyword.is_structured_sms()
                    else EditNormalKeywordView.urlname)
                display = '<a target="_blank" href="%s">%s</a>' % (
                    reverse(urlname, args=[keyword.domain, keyword_id]),
                    keyword.description,
                )
        except ResourceNotFound:
            display = '-'

        content_cache[keyword_id] = display
        return display
Ejemplo n.º 10
0
    def get_keyword_display(self, keyword_id, content_cache):
        if keyword_id in content_cache:
            return content_cache[keyword_id]
        try:
            keyword = SurveyKeyword.get(keyword_id)
            if keyword.deleted():
                display = '%s %s' % (keyword.description, _('(Deleted Keyword)'))
            else:
                urlname = (EditStructuredKeywordView.urlname if keyword.is_structured_sms()
                    else EditNormalKeywordView.urlname)
                display = '<a target="_blank" href="%s">%s</a>' % (
                    reverse(urlname, args=[keyword.domain, keyword_id]),
                    keyword.description,
                )
        except ResourceNotFound:
            display = '-'

        content_cache[keyword_id] = display
        return display
Ejemplo n.º 11
0
    def get_keyword_display(self, keyword_id, content_cache):
        if keyword_id in content_cache:
            return content_cache[keyword_id]
        try:
            keyword = SurveyKeyword.get(keyword_id)
            if keyword.doc_type.endswith('-Deleted'):
                # We'll get rid of this once we move to postgres
                raise ResourceNotFound()
        except ResourceNotFound:
            display = _('(Deleted Keyword)')
        else:
            urlname = (EditStructuredKeywordView.urlname if keyword.is_structured_sms()
                else EditNormalKeywordView.urlname)
            display = '<a target="_blank" href="%s">%s</a>' % (
                reverse(urlname, args=[keyword.domain, keyword_id]),
                keyword.description,
            )

        content_cache[keyword_id] = display
        return display
Ejemplo n.º 12
0
def add_keyword(request, domain, keyword_id=None):
    if keyword_id is None:
        s = SurveyKeyword(domain = domain)
    else:
        s = SurveyKeyword.get(keyword_id)
    
    context = {
        "domain" : domain,
        "form_list" : get_form_list(domain),
        "errors" : [],
        "keyword" : s
    }
    
    if request.method == "GET":
        return render_to_response(request, "reminders/partial/add_keyword.html", context)
    else:
        keyword = request.POST.get("keyword", None)
        form_unique_id = request.POST.get("survey", None)
        
        if keyword is not None:
            keyword = keyword.strip()
        
        s.keyword = keyword
        s.form_unique_id = form_unique_id
        
        errors = []
        if keyword is None or keyword == "":
            errors.append("Please enter a keyword.")
        if form_unique_id is None:
            errors.append("Please create a form first, and then add a keyword for it.")
        duplicate_entry = SurveyKeyword.get_keyword(domain, keyword)
        if duplicate_entry is not None and keyword_id != duplicate_entry._id:
            errors.append("Keyword already exists.")
        
        if len(errors) > 0:
            context["errors"] = errors
            return render_to_response(request, "reminders/partial/add_keyword.html", context)
        else:
            s.save()
            return HttpResponseRedirect(reverse("manage_surveys", args=[domain]))
Ejemplo n.º 13
0
    def get_keyword_display(self, keyword_id, content_cache):
        if keyword_id in content_cache:
            args = content_cache[keyword_id]
            return self.table_cell(*args)
        try:
            keyword = SurveyKeyword.get(keyword_id)
            if keyword.deleted():
                display = '%s %s' % (keyword.description, _('(Deleted Keyword)'))
                display_text = display
            else:
                urlname = (EditStructuredKeywordView.urlname if keyword.is_structured_sms()
                    else EditNormalKeywordView.urlname)
                display = '<a target="_blank" href="%s">%s</a>' % (
                    reverse(urlname, args=[keyword.domain, keyword_id]),
                    keyword.description,
                )
                display_text = keyword.description
            args = (display_text, display)
        except ResourceNotFound:
            args = ('-', '-')

        content_cache[keyword_id] = args
        return self.table_cell(*args)
Ejemplo n.º 14
0
def delete_keyword(request, domain, keyword_id):
    s = SurveyKeyword.get(keyword_id)
    s.retire()
    return HttpResponseRedirect(reverse("manage_surveys", args=[domain]))
Ejemplo n.º 15
0
def delete_keyword(request, domain, keyword_id):
    s = SurveyKeyword.get(keyword_id)
    if s.domain != domain or s.doc_type != "SurveyKeyword":
        raise Http404
    s.retire()
    return HttpResponseRedirect(reverse("manage_keywords", args=[domain]))
Ejemplo n.º 16
0
def delete_keyword(request, domain, keyword_id):
    s = SurveyKeyword.get(keyword_id)
    s.retire()
    return HttpResponseRedirect(reverse("manage_keywords", args=[domain]))