def handle(self, *args, **options): interactive = True if len(args) and args[0] == '--no-input': interactive = False # Ask the user if interactive if interactive: correct = False print "This process will delete the tag indexes directory. Continue: [y/n]" while not correct: opt = read_from_cmd() if opt != 'y' and opt != 'n': print "Please include 'y' or 'n'" else: correct = True if opt == 'n': return # Remove the index directory index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'social') index_path = os.path.join(index_path, 'indexes') rmtree(index_path, True) # Generate new search indexes tag_manager = TagManager(index_path) for o in Offering.objects.all(): aux_tags = list(o.tags) o.tags = [] o.save() tag_manager.update_tags(o, aux_tags)
def _create_tags(): tm = TagManager() offering1 = Offering.objects.get(name='test_offering1') offering2 = Offering.objects.get(name='test_offering2') offering3 = Offering.objects.get(name='test_offering3') tm.update_tags(offering1, ['service', 'tag']) tm.update_tags(offering2, ['dataset', 'tag']) tm.update_tags(offering3, ['widget'])
def update(self, request, organization, name, version): # Get offering try: org = Organization.objects.get(name=organization) offering = Offering.objects.get(owner_organization=org, name=name, version=version) except: return build_response(request, 404, 'Not found') # Check that the user has enough rights if request.user.userprofile.current_organization != org\ or (not offering.is_owner(request.user) and not request.user.pk in org.managers): return build_response(request, 403, 'Forbidden') # Build tag manager try: data = json.loads(request.raw_post_data) manager = TagManager() manager.update_tags(offering, data['tags']) except Exception, e: return build_response(request, 400, e.message)
def read(self, request, tag): # Get query params action = request.GET.get('action', None) start = request.GET.get('start', None) limit = request.GET.get('limit', None) sort = request.GET.get('sort', None) state = request.GET.get('filter', None) # Check action format if action and not (isinstance(action, str) or isinstance(action, unicode)): return build_response(request, 400, 'Invalid action format') # Validate action if action and action != 'count': return build_response(request, 400, 'Invalid action') if action and (start or limit or sort): return build_response( request, 400, 'Actions cannot be combined with pagination') # Validate pagination if (start and not limit) or (not start and limit): return build_response(request, 400, 'Both pagination params are required') if start and limit and (not start.isnumeric() or not limit.isnumeric()): return build_response(request, 400, 'Invalid format in pagination parameters') elif start and limit: start = int(start) limit = int(limit) if start and limit and (start < 1 or limit < 1): return build_response( request, 400, 'Pagination params must be equal or greater that 1') # Validate sorting allowed_sorting = ['name', 'date', 'popularity'] if sort and (not isinstance(sort, str) and not isinstance(sort, unicode)): return build_response(request, 400, 'Invalid sorting format') if sort and not sort in allowed_sorting: return build_response(request, 400, 'Invalid sorting value') # Validate state if state and state != 'published' and state != 'purchased': return build_response(request, 400, 'Invalid filter') try: # Build tag manager tm = TagManager() # Select action if action == 'count': response = {'number': tm.count_offerings(tag)} else: offerings = tm.search_by_tag(tag) response = [] # Get offering info for off in offerings: offering_info = get_offering_info(off, request.user) if not state and offering_info['state'] != 'published'\ and offering_info['state'] != 'purchased' and offering_info['state'] != 'rated': continue response.append(offering_info) # Sort offerings if needed if sort: rev = True if sort == 'name': rev = False elif sort == 'date': sort = 'publication_date' elif sort == 'popularity': sort = 'rating' response = sorted(response, key=lambda off: off[sort], reverse=rev) # If sort was needed pagination must be done after sorting if start and limit: response = response[start - 1:(limit + (start - 1))] except Exception as e: return build_response(request, 400, unicode(e)) # Create response return HttpResponse(json.dumps(response), status=200, mimetype='application/json; charset=utf-8')