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 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, 'search') index_path = os.path.join(index_path, 'indexes') rmtree(index_path, True) # Generate new search indexes se = SearchEngine(index_path) for o in Offering.objects.all(): se.create_index(o)
def _create_indexes(): index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') search_engine = SearchEngine(index_path) for off in Offering.objects.all(): search_engine.create_index(off)
def remove_review(self, user, review): """ Removes a given review """ rev = self._get_and_validate_review(user, review) # Remove review from offering rev.offering.comments.remove(review) # Update offering rating if len(rev.offering.comments): rev.offering.rating = ((rev.offering.rating * (len(rev.offering.comments) + 1)) - rev.rating) / len(rev.offering.comments) else: rev.offering.rating = 0 rev.offering.save() # Update offering indexes index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') se = SearchEngine(index_path) se.update_index(rev.offering) # Update top rated offerings self._update_top_rated() # Update user info to allow her to create a new review if rev.user == user: # Check if is a private review or an organization review if user.userprofile.is_user_org(): user.userprofile.rated_offerings.remove(rev.offering.pk) user.userprofile.save() else: self._remove_review_from_org( user, rev.offering, user.userprofile.current_organization) else: self._remove_review_from_org(rev.user, rev.offering, rev.organization) rev.delete()
def update_review(self, user, review, review_data): """ Updates a given review """ # Check data validation = self._validate_content(review_data) if validation: raise validation rev = self._get_and_validate_review(user, review) # Calculate new rating rate = ( (rev.offering.rating * len(rev.offering.comments)) - rev.rating + review_data['rating']) / len(rev.offering.comments) # update review rev.title = review_data['title'] rev.comment = review_data['comment'] rev.rating = review_data['rating'] rev.timestamp = datetime.now() rev.save() # Update offering rating rev.offering.rating = rate rev.offering.save() # Update offering indexes index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') se = SearchEngine(index_path) se.update_index(rev.offering) # Update top rated offerings self._update_top_rated()
def read(self, request, text): index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') search_engine = SearchEngine(index_path) filter_ = request.GET.get('filter', None) 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('state', None) if state: if state == 'ALL': state = ['uploaded', 'published', 'deleted'] else: state = state.split(',') # Check the filter value if filter_ and filter_ != 'published' and filter_ != 'provided' and filter_ != 'purchased': return build_response(request, 400, 'Invalid filter') if state and filter_ != 'provided': return build_response(request, 400, 'Invalid filters') if filter_ == 'provider' and not state: return build_response(request, 400, 'Invalid filters') count = False pagination = None # Check if the action is count if action != None: if action == 'count': count = True else: return build_response(request, 400, 'Invalid action') else: # Check pagination params (Only when action is none) if start != None and limit != None: pagination = { 'start': int(start), 'limit': int(limit) } elif (start != None and limit == None) or (start == None and limit != None): return build_response(request, 400, 'Missing pagination param') # Check sorting values if sort != None: if sort != 'date' and sort != 'popularity' and sort != 'name': return build_response(request, 400, 'Invalid sorting') if not filter_ or filter_ == 'published': response = search_engine.full_text_search(request.user, text, count=count, pagination=pagination, sort=sort) elif filter_ == 'provided': response = search_engine.full_text_search(request.user, text, state=state, count=count, pagination=pagination, sort=sort) elif filter_ == 'purchased': response = search_engine.full_text_search(request.user, text, state=['purchased'], count=count, pagination=pagination, sort=sort) return HttpResponse(json.dumps(response), status=200, mimetype='application/json')
def create_review(self, user, offering, review): """ Creates a new review for a given offering """ # Check if the user has purchased the offering (Not if the offering is open) if not offering.open: try: purchase = Purchase.objects.get( offering=offering, owner_organization=user.userprofile.current_organization) except: raise PermissionDenied( 'You cannot review this offering since you has not acquire it' ) elif offering.is_owner(user): # If the offering is open, check that the user is not owner of the offering raise PermissionDenied('You cannot review your own offering') # Check if the user has already review the offering. if (user.userprofile.is_user_org() and offering.pk in user.userprofile.rated_offerings)\ or (not user.userprofile.is_user_org() and user.userprofile.current_organization.has_rated_offering(user, offering)): raise PermissionDenied( 'You cannot review this offering again. Please update your review to provide new comments' ) # Validate review data validation = self._validate_content(review) if validation: raise validation # Create the review rev = Review.objects.create( user=user, organization=user.userprofile.current_organization, offering=offering, timestamp=datetime.now(), title=review['title'], comment=review['comment'], rating=review['rating']) offering.comments.insert(0, rev.pk) # Calculate new offering rate old_rate = offering.rating if old_rate == 0: offering.rating = review['rating'] else: offering.rating = ((old_rate * (len(offering.comments) - 1)) + review['rating']) / len(offering.comments) offering.save() # Update offering indexes index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') se = SearchEngine(index_path) se.update_index(offering) # Save the offering as rated if user.userprofile.is_user_org(): user.userprofile.rated_offerings.append(offering.pk) user.userprofile.save() else: user.userprofile.current_organization.rated_offerings.append({ 'user': user.pk, 'offering': offering.pk }) user.userprofile.current_organization.save() # Update top rated list self._update_top_rated()