コード例 #1
0
ファイル: views.py プロジェクト: zishanAhmad/tnz-inventory
def new_item(request):
    if request.method == 'POST':
        from uuid import uuid4

        sku = escape(request.POST.get('sku').strip())
        model = escape(request.POST.get('model').strip())
        name = escape(request.POST.get('name').strip())
        category_id = escape(request.POST.get('category').strip())
        category = Category.objects.get(id=category_id)
        price = escape(request.POST.get('price').strip())
        length = escape(request.POST.get('length').strip())
        breadth = escape(request.POST.get('breadth').strip())
        height = escape(request.POST.get('height').strip())
        image = request.FILES.get('image')
        image.name = '{}{}'.format(uuid4().hex, image.name[image.name.rfind('.'):])
        product = Product(sku=sku, model=model, name=name, category=category, length=length, breadth=breadth,
                          height=height, price=price, image=image)
        try:
            product.save()
            category.number_of_items += 1
            category.save()
            url = "%s?created=True" % reverse('user:dashboard')
        except IntegrityError:
            from django.utils.http import quote_plus
            r = "SKU needs to be unique"
            url = "%s?created=False&reason=%s" % (reverse('user:dashboard'), quote_plus(r))
        return HttpResponseRedirect(url)

    category = Category.objects.all()
    cd = {'category': category}
    return render(request, 'new.html', cd)
コード例 #2
0
 def make_request_url(self, new_rparams, doc_format=''):
     """ makes request urls from the new request object
         default doc_format is '' (HTML)
     """
     url = self.base_url + self.base_search_link
     if 'path' in new_rparams:
         if new_rparams['path'] is not None \
            and new_rparams['path'] is not False:
             # context_path = iri_to_uri(new_rparams['path'])
             context_path = new_rparams['path']
             context_path = context_path.replace(' ', '+')
             url += context_path
     url += doc_format
     param_sep = '?'
     param_list = []
     for param, param_vals in new_rparams.items():
         if param != 'path':
             for val in param_vals:
                 quote_val = quote_plus(val)
                 quote_val = quote_val.replace('%7BSearchTerm%7D',
                                               '{SearchTerm}')
                 param_item = param + '=' + quote_val
                 param_list.append(param_item)
     if len(param_list) > 0:
         # keep a consistent sort order on query parameters + values.
         param_list.sort()
         url += '?' + '&'.join(param_list)
     return url
コード例 #3
0
ファイル: filterlinks.py プロジェクト: ekansa/open-context-py
 def make_request_url(self,
                      new_rparams,
                      doc_format=''):
     """ makes request urls from the new request object
         default doc_format is '' (HTML)
     """
     url = self.base_url + self.base_search_link
     if 'path' in new_rparams:
         if new_rparams['path'] is not None \
            and new_rparams['path'] is not False:
             # context_path = iri_to_uri(new_rparams['path'])
             context_path = new_rparams['path']
             context_path = context_path.replace(' ', '+')
             url += context_path
     url += doc_format
     param_sep = '?'
     param_list = []
     for param, param_vals in new_rparams.items():
         if param != 'path':
             for val in param_vals:
                 quote_val = quote_plus(val)
                 quote_val = quote_val.replace('%7BSearchTerm%7D', '{SearchTerm}')
                 param_item = param + '=' + quote_val
                 param_list.append(param_item)
     if len(param_list) > 0:
         # keep a consistent sort order on query parameters + values.
         param_list.sort()
         url += '?' + '&'.join(param_list)
     return url
コード例 #4
0
def post_detail(request, slug=None):
    if not request.user.is_authenticated():
        messages.error(request, "You need to login first")
        # return redirect(reverse('posts:login'))
        return redirect(reverse('posts:homepage'))
    post = get_object_or_404(Post, slug=slug)
    if post.draft or post.publish_date > timezone.now().date():
        if not request.user.is_staff or not request.user.is_superuser:
            raise Http404
    share_string = quote_plus(post.title)
    context = {"post": post, "share_string": share_string}
    return render(request, "post_detail.html", context)
コード例 #5
0
    def make_url_from_request_dict(
        self,
        base_request_url=None,
        request_dict=None,
        doc_extention=None,
    ):
        """Makes request url from a request_dict
        """
        if base_request_url is None:
            url = self.base_url + self.base_search_url
        else:
            url = base_request_url

        if request_dict is None:
            request_dict = self.request_dict

        path = get_path_value(request_dict)
        if path:
            url += path.replace(' ', '+')
        if doc_extention:
            url += doc_extention

        # Prepare the query parameters.
        param_list = []
        for param, param_vals in request_dict.items():
            if param == 'path':
                continue
            if not isinstance(param_vals, list):
                # params_vals maybe a single value, but we default
                # to treating it as a list.
                param_vals = [str(param_vals)]
            for val in param_vals:
                quote_val = quote_plus(val)
                quote_val = quote_val.replace('%7BSearchTerm%7D',
                                              '{SearchTerm}')
                param_item = param + '=' + quote_val
                param_list.append(param_item)
        if len(param_list):
            # keep a consistent sort order on query parameters + values.
            param_list.sort()
            url += '?' + '&'.join(param_list)
        return url
コード例 #6
0
ファイル: api.py プロジェクト: ekansa/open-context-py
 def get_uri_graph(self, uri, fformat='application/rdf+xml'):
     """
     gets an RDF graph for the Uberon URI
     """
     url = self.base_url + quote_plus(uri)
     self.request_url = url
     if self.delay_before_request > 0:
         # default to sleep BEFORE a request is sent, to
         # give the remote service a break.
         sleep(self.delay_before_request)
     graph = Graph()
     try:
         if(fformat is not False):
             graph.parse(url, format=fformat)
         else:
             graph.parse(url)
     except:
         print('Failed to load the graph.')
         graph = False
     if graph is not False:
         self.graph = graph
     return self.graph
コード例 #7
0
 def get_uri_graph(self, uri, fformat='application/rdf+xml'):
     """
     gets an RDF graph for the Uberon URI
     """
     url = self.base_url + quote_plus(uri)
     self.request_url = url
     if self.delay_before_request > 0:
         # default to sleep BEFORE a request is sent, to
         # give the remote service a break.
         sleep(self.delay_before_request)
     graph = Graph()
     try:
         if (fformat is not False):
             graph.parse(url, format=fformat)
         else:
             graph.parse(url)
     except:
         print('Failed to load the graph.')
         graph = False
     if graph is not False:
         self.graph = graph
     return self.graph
コード例 #8
0
 def make_request_url(self, new_rparams, doc_format=''):
     """ makes request urls from the new request object
         default doc_format is '' (HTML)
     """
     url = self.base_url + self.base_search_link
     if 'path' in new_rparams:
         if new_rparams['path'] is not None \
            and new_rparams['path'] is not False:
             # context_path = iri_to_uri(new_rparams['path'])
             context_path = new_rparams['path']
             context_path = context_path.replace(' ', '+')
             url += context_path
     url += doc_format
     param_sep = '?'
     for param, param_vals in new_rparams.items():
         if param != 'path':
             for val in param_vals:
                 quote_val = quote_plus(val)
                 quote_val = quote_val.replace('%7BSearchTerm%7D',
                                               '{SearchTerm}')
                 url += param_sep + param + '=' + quote_val
                 param_sep = '&'
     return url
コード例 #9
0
 def make_request_url(self,
                      new_rparams,
                      doc_format=''):
     """ makes request urls from the new request object
         default doc_format is '' (HTML)
     """
     url = self.base_url + self.base_search_link
     if 'path' in new_rparams:
         if new_rparams['path'] is not None \
            and new_rparams['path'] is not False:
             # context_path = iri_to_uri(new_rparams['path'])
             context_path = new_rparams['path']
             context_path = context_path.replace(' ', '+')
             url += context_path
     url += doc_format
     param_sep = '?'
     for param, param_vals in new_rparams.items():
         if param != 'path':
             for val in param_vals:
                 quote_val = quote_plus(val)
                 quote_val = quote_val.replace('%7BSearchTerm%7D', '{SearchTerm}')
                 url += param_sep + param + '=' + quote_val
                 param_sep = '&'
     return url
コード例 #10
0
 def prep_assocated_dc_metadata(self):
     """ prepares dc_metadata for items associated to the
         main item that actually has 1 or more gazetteer links
     """
     if self.is_valid and len(self.raw_associated) > 0:
         if isinstance(self.active_project_uuid, str):
             project_ent = self.get_entity(self.active_project_uuid)
         else:
             project_ent = self.get_entity(self.manifest.project_uuid)
         ass_items = []  # list of associated items
         ass_sets = []  # list of associated sets
         for key, ass in self.raw_associated.items():
             if isinstance(ass['uuid'], str) and \
                isinstance(ass['label'], str):
                 # we have a uuid identified item, meaning a specific
                 # related resource
                 ass['uri'] = URImanagement.make_oc_uri(
                     ass['uuid'], ass['item_type'])
                 ass['title'] = self.make_dcterms_title(
                     ass['label'], self.context)
                 # now prepare description information
                 description = ''
                 cat_ent = self.get_entity(ass['media_class_uri'])
                 if cat_ent is not False:
                     ass['class_label'] = cat_ent.label
                     ass['class_slug'] = cat_ent.slug
                     description += cat_ent.label
                 if ass['item_type'] in PelagiosData.ITEM_TYPE_DESCRIPTIONS:
                     if description == '':
                         description = 'A'
                     description += ' ' + PelagiosData.ITEM_TYPE_DESCRIPTIONS[
                         ass['item_type']]
                 ass['description'] = self.add_description_item_class_project(
                     description, project_ent)
                 if ass['temporal'] is None:
                     ass['temporal'] = self.temporal
                 ass_items.append(ass)
             elif self.contents_cnt > 1 or self.manifest.item_type == 'projects':
                 # the associated item is for a result set, not an individual item
                 rel_media_cat_ent = False
                 if isinstance(ass['media_class_uri'], str):
                     rel_media_cat_ent = self.get_entity(
                         ass['media_class_uri'])
                 cat_ent = self.get_entity(ass['class_uri'])
                 description = 'A set of '
                 if cat_ent is not False:
                     ass['class_label'] = cat_ent.label
                     ass['class_slug'] = cat_ent.slug
                     ass['title'] = cat_ent.label
                     description += cat_ent.label.lower()
                 else:
                     ass['title'] = 'Related'
                 if rel_media_cat_ent is not False:
                     ass['title'] += ' ' + rel_media_cat_ent.label
                     description += ' ' + rel_media_cat_ent.label.lower()
                 if ass['item_type'] in PelagiosData.ITEM_TYPE_DESCRIPTIONS_PLR:
                     type_des = PelagiosData.ITEM_TYPE_DESCRIPTIONS_PLR[
                         ass['item_type']]
                     ass['title'] += ' ' + type_des
                     description += ' ' + type_des.lower()
                 ass['title'] += ' Related to: ' + self.manifest.label
                 if isinstance(self.class_label, str) and \
                    self.manifest.item_type != 'projects':
                     ass['title'] += ' (' + self.class_label + ')'
                 ass['description'] = self.add_description_item_class_project(
                     description, project_ent)
                 param_sep = '?'
                 # payload is for querying for temporal data
                 payload = {
                     'response': 'metadata',
                     'type': ass['item_type'],
                     'prop': []
                 }
                 if ass['item_type'] == 'media':
                     ass['uri'] = settings.CANONICAL_HOST + '/media-search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(
                             self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=rel--' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append('rel--' + cat_ent.slug)
                     if rel_media_cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=' + rel_media_cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append(rel_media_cat_ent.slug)
                     elif isinstance(ass['media_class_uri'], str):
                         ass['uri'] += param_sep + 'prop=' + quote_plus(
                             ass['media_class_uri'])
                         payload['prop'].append(ass['media_class_uri'])
                 elif ass['item_type'] == 'subjects':
                     ass['uri'] = settings.CANONICAL_HOST + '/subjects-search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(
                             self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append(cat_ent.slug)
                 else:
                     ass['uri'] = settings.CANONICAL_HOST + '/search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(
                             self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=rel--' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append('rel--' + cat_ent.slug)
                     ass['uri'] += param_sep + 'type=' + ass['item_type']
                     param_sep = '&'
                 if project_ent is not False:
                     ass['uri'] += param_sep + 'proj=' + project_ent.slug
                     payload['proj'] = project_ent.slug
                 # now query Solr for temporal data
                 cq = CompleteQuery()
                 spatial_context = None
                 if isinstance(self.context, str):
                     spatial_context = self.context
                 if len(payload['prop']) < 1:
                     # remove unused property key
                     payload.pop('prop', None)
                 ass_metadata = cq.get_json_query(payload, spatial_context)
                 if 'dc-terms:temporal' in ass_metadata:
                     ass['temporal'] = ass_metadata['dc-terms:temporal']
                 ass_sets.append(ass)
             else:
                 pass
         if self.manifest.item_type == 'projects':
             # we have a project so get the hero image (if exists) directly
             # for the depiction (note: returns None if not found)
             self.depiction = self.get_depiction_image_file(self.uuid)
         else:
             # we have another item_type, so the self.depiction comes
             # from the list of associated items
             for ass in ass_items:
                 if isinstance(ass['depiction'], str):
                     # the item depiction file is the first one we find
                     # from the associated item list
                     self.depiction = ass['depiction']
                     break
         self.associated = ass_items + ass_sets
コード例 #11
0
ファイル: models.py プロジェクト: mfindeisen/open-context-py
 def prep_assocated_dc_metadata(self):
     """ prepares dc_metadata for items associated to the
         main item that actually has 1 or more gazetteer links
     """
     if self.is_valid and len(self.raw_associated) > 0:
         project_ent = self.get_entity(self.manifest.project_uuid)
         ass_items = []  # list of associated items
         ass_sets = [] # list of associated sets
         for key, ass in self.raw_associated.items():
             if isinstance(ass['uuid'], str) and \
                isinstance(ass['label'], str):
                 # we have a uuid identified item, meaning a specific
                 # related resource
                 ass['uri'] = URImanagement.make_oc_uri(ass['uuid'],
                                                        ass['item_type'])
                 ass['title'] = self.make_dcterms_title(ass['label'],
                                                        self.context)
                 # now prepare description information
                 description = ''
                 cat_ent = self.get_entity(ass['media_class_uri'])
                 if cat_ent is not False:
                     ass['class_label'] = cat_ent.label
                     ass['class_slug'] = cat_ent.slug
                     description += cat_ent.label
                 if ass['item_type'] in PelagiosData.ITEM_TYPE_DESCRIPTIONS:
                     if description == '':
                         description = 'A'
                     description += ' ' + PelagiosData.ITEM_TYPE_DESCRIPTIONS[ass['item_type']]
                 ass['description'] = self.add_description_item_class_project(description,
                                                                              project_ent)
                 if ass['temporal'] is None:
                     ass['temporal'] = self.temporal
                 ass_items.append(ass)
             elif self.contents_cnt > 1 or self.manifest.item_type == 'projects':
                 # the associated item is for a result set, not an individual item
                 rel_media_cat_ent = False
                 if isinstance(ass['media_class_uri'], str):
                     rel_media_cat_ent = self.get_entity(ass['media_class_uri'])
                 cat_ent = self.get_entity(ass['class_uri'])
                 description = 'A set of '
                 if cat_ent is not False:
                     ass['class_label'] = cat_ent.label
                     ass['class_slug'] = cat_ent.slug
                     ass['title'] = cat_ent.label
                     description += cat_ent.label.lower()
                 else:
                     ass['title'] = 'Related'
                 if rel_media_cat_ent is not False:
                     ass['title'] += ' ' + rel_media_cat_ent.label
                     description += ' ' + rel_media_cat_ent.label.lower()
                 if ass['item_type'] in PelagiosData.ITEM_TYPE_DESCRIPTIONS_PLR:
                     type_des = PelagiosData.ITEM_TYPE_DESCRIPTIONS_PLR[ass['item_type']]
                     ass['title'] += ' ' + type_des
                     description += ' ' + type_des.lower()
                 ass['title'] += ' Related to: ' + self.manifest.label
                 if isinstance(self.class_label, str) and \
                    self.manifest.item_type != 'projects':
                     ass['title'] += ' (' + self.class_label + ')'
                 ass['description'] = self.add_description_item_class_project(description,
                                                                              project_ent)
                 param_sep = '?'
                 # payload is for querying for temporal data
                 payload = {
                     'response': 'metadata',
                     'type': ass['item_type'],
                     'prop': []}
                 if ass['item_type'] == 'media': 
                     ass['uri'] = settings.CANONICAL_HOST + '/media-search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=rel--' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append('rel--' + cat_ent.slug)
                     if rel_media_cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=' + rel_media_cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append(rel_media_cat_ent.slug)
                     elif isinstance(ass['media_class_uri'], str):
                         ass['uri'] += param_sep + 'prop=' + quote_plus(ass['media_class_uri'])
                         payload['prop'].append(ass['media_class_uri'])
                 elif ass['item_type'] == 'subjects':
                     ass['uri'] = settings.CANONICAL_HOST + '/subjects-search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append(cat_ent.slug)
                 else:
                     ass['uri'] = settings.CANONICAL_HOST + '/search/'
                     if isinstance(self.context, str):
                         ass['uri'] += self.encode_url_context_path(self.context)
                     if cat_ent is not False:
                         ass['uri'] += param_sep + 'prop=rel--' + cat_ent.slug
                         param_sep = '&'
                         payload['prop'].append('rel--' + cat_ent.slug)
                     ass['uri'] += param_sep + 'type=' + ass['item_type']
                     param_sep = '&'
                 if project_ent is not False:
                     ass['uri'] += param_sep + 'proj=' + project_ent.slug
                     payload['proj'] = project_ent.slug
                 # now query Solr for temporal data
                 cq = CompleteQuery()
                 spatial_context = None
                 if isinstance(self.context, str):
                     spatial_context = self.context
                 if len(payload['prop']) < 1:
                     # remove unused property key
                     payload.pop('prop', None)
                 ass_metadata = cq.get_json_query(payload, spatial_context)
                 if 'dc-terms:temporal' in ass_metadata:
                     ass['temporal'] = ass_metadata['dc-terms:temporal']
                 ass_sets.append(ass)
             else:
                 pass
         if self.manifest.item_type == 'projects':
             # we have a project so get the hero image (if exists) directly
             # for the depiction (note: returns None if not found)
             self.depiction = self.get_depiction_image_file(self.uuid)
         else:
             # we have another item_type, so the self.depiction comes
             # from the list of associated items
             for ass in ass_items:
                 if isinstance(ass['depiction'], str):
                     # the item depiction file is the first one we find
                     # from the associated item list
                     self.depiction = ass['depiction']
                     break      
         self.associated = ass_items + ass_sets
コード例 #12
0
ファイル: tests.py プロジェクト: songea/rapidpro
 def mockedServerURL(self, content, status=200):
     return '%s/echo?content=%s&status=%d' % (
         TembaTestRunner.MOCKED_SERVER_URL, quote_plus(content), status)
コード例 #13
0
ファイル: models.py プロジェクト: hidalgopl/early-phase
 def address_query(self):
     return quote_plus(self.address.__str__())
コード例 #14
0
 def facet_param(self):
     return "%s=%s" % (self.klass.paramname, quote_plus(self.name))
コード例 #15
0
 def facet_param(self):
     return "%s=%s" % (self.klass.paramname, quote_plus(self.name))