def merge(self, mid): """ Retrieves a Topic by mid, if doesn't exist, merges it from Freebase """ # Strip the trailing slash if mid.endswith("/"): mid = mid.rstrip("/") results = self.filter(mid=mid) ## If result fetched from the database if len(results) == 1: return results[0] ## If no result fetched from the database if len(results) < 1: result = topic(mid) summary = summarize(result) # Rename keys to fit the model summary['title'] = summary.pop('name') # Remove extra keys not required for model del summary['links'] return self.create(**summary) ## If multiple results returned (shouldn't happen) if len(results) > 1: raise self.model.MultipleObjectsReturned() raise ProgrammingError("Merge operation should return or raise model (exception)")
def retrieve(self, request, pk=None): """ Returns a topic detail from Freebase for a particular mid """ if pk is None: raise Http404 if not pk.startswith("/"): pk = "/" + pk result = topic(pk) return Response(summarize(result))