def get_viewset_for_path(path: str, method="GET") -> "rest_framework.viewsets.ViewSet": """ Look up which viewset matches a path. """ # NOTE: this doesn't support setting a different urlconf on the request resolver = get_resolver() try: resolver_match = resolver.resolve(path) except Resolver404 as exc: raise models.ObjectDoesNotExist("URL did not resolve") from exc callback, callback_args, callback_kwargs = resolver_match if not hasattr(callback, "cls"): raise NotAViewSet(f"Callback for {path} does not look like a viewset") viewset = callback.cls(**callback.initkwargs) viewset.action_map = callback.actions viewset.request = HttpRequest() viewset.args = callback_args viewset.kwargs = callback_kwargs viewset.action = viewset.action_map.get(method.lower()) return viewset
def get(cls, pk, tenant_pk): query_set = cls.objects.filter(id=pk, tenant_id=tenant_pk).filter(deleted=0) if not query_set: raise models.ObjectDoesNotExist("id: {}, tenant_id: {}".format( pk, tenant_pk)) return query_set[0]
def can_add_website(self): if not self.subscription: raise models.ObjectDoesNotExist( 'Customer Subscription doesn\'t exist') allows_infinite = self.subscription.total_websites_allowed == 0 return allows_infinite or self.websites.count( ) + 1 <= self.subscription.total_websites_allowed
def resolve_path(path: str, resolver=None, script_prefix=None) -> ResolverMatch: resolver = resolver or get_resolver() prefix = script_prefix or get_script_prefix() path = path.replace(prefix, "/", 1) try: return resolver.resolve(path) except Resolver404 as exc: raise models.ObjectDoesNotExist("URL did not resolve") from exc
def get_template(cls, template_id, assessment_id, report_type): # Return a template object if one exists which matches the specified # criteria, else throw an ObjectDoesNotExist error qs = cls.objects\ .filter(id=template_id, report_type=report_type)\ .filter(Q(assessment=assessment_id) | Q(assessment=None)) if qs.count() == 1: return qs[0] else: raise models.ObjectDoesNotExist()
def get_question_by_pk(self, question_pk): """ Get the question provided by the given question_pk. Raises ObjectDoesNotExist if no such question is attached to this Assignment. """ for question in self.questions.all(): if int(question.pk) == int(question_pk): return question raise models.ObjectDoesNotExist()
def get_question_by_slug(self, question_slug): """ Get a particular question, as identified by the question_slug. Raises ObjectDoesNotExist if the slugs' question is not related to this Assignment. Can't do this in the database 'cause generic relations. """ for question in self.questions.all(): if question.slug == question_slug: return question raise models.ObjectDoesNotExist()
def generate_object(obj, content_type=None): """ Function for get obj, take an obj or id & content_type :param obj: the object or id :param content_type: the content_type if object's id :return: ModelObject """ if isinstance(obj, models.Model): return obj elif isinstance(content_type, ContentType) and isinstance(obj, int): return content_type.model_class().objects.get(id=obj) raise models.ObjectDoesNotExist(_(u"The obj given isn't correct"))
def generate_content_type(content_type): """ Function who take id or content_type object :param content_type: id or content_type object :return: ContentType """ if isinstance(content_type, ContentType): return content_type elif isinstance(content_type, int): return ContentType.objects.get(id=content_type) raise models.ObjectDoesNotExist( _(u"The content_type giver isn't correct"))
class Fcuser(models.Model): objects = models.Manager() doseNotExist = models.ObjectDoesNotExist() email = models.EmailField(verbose_name='이메일') password = models.CharField(max_length=128, verbose_name='비밀번호') level = models.CharField(max_length=8, verbose_name="등급", choices=( ('admin', 'admin'), ('user', 'user') )) register = models.DateField(auto_now_add=True, verbose_name='등록날짜') def __str__(self): return self.email # Meta class Meta: db_table = 'fastcampus_fcuser' verbose_name = '사용자' verbose_name_plural = '사용자'
def get_viewset_for_path(path: str) -> "rest_framework.viewsets.ViewSet": """ Look up which viewset matches a path. """ # NOTE: this doesn't support setting a different urlconf on the request resolver = get_resolver() try: resolver_match = resolver.resolve(path) except Resolver404 as exc: raise models.ObjectDoesNotExist("URL did not resolve") from exc callback, callback_args, callback_kwargs = resolver_match assert hasattr(callback, "cls"), "Callback doesn't appear to be from a viewset" viewset = callback.cls(**callback.initkwargs) viewset.action_map = callback.actions viewset.request = HttpRequest() viewset.args = callback_args viewset.kwargs = callback_kwargs return viewset
def handle(self, *args, **options): input_filepath = options['csv_file'] verbose = options['verbosity'] > 0 with open(input_filepath, newline='') as csvfile: reader = csv.DictReader(csvfile, quoting=csv.QUOTE_NONE) for row in tqdm(reader, disable=not verbose): title = row['title'] contributors_names = row['contributors'].split('|') ISWC = row['iswc'] or None contributors = insert_and_get_contributors(contributors_names) contributors_names = to_set(contributors) try: if ISWC: meta = Metadata.objects.get(ISWC=ISWC) else: raise models.ObjectDoesNotExist() except models.ObjectDoesNotExist: meta = Metadata(title=title, ISWC=ISWC) meta.save() for contributor in contributors: meta.contributors.add(contributor) meta.save() similar_metas = Metadata.objects.filter(title=title) contributors_names = set(contributors_names) for similar_meta in similar_metas: if meta == similar_meta: continue similar_contributors = similar_meta.contributors.all() if to_set(similar_contributors) & contributors_names: meta.ISWC = meta.ISWC or similar_meta.ISWC for similar_contributor in similar_contributors: if similar_contributor.name not in contributors_names: meta.contributors.add(similar_contributor) similar_meta.delete() meta.save()
def checkout(request, next=None): """ Authenticate & log user in """ def checkip(ips, ip): if ip == '': return True return ips == ip user = None next = request.REQUEST.get("next", "") or "/" if request.user.is_active: return HttpResponseRedirect(next) if request.method == 'POST': server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) try: check_ip = True try: from ipware.ip import get_ip ips = get_ip(request) IPs = SystemIP.objects.all()[0] check_ip = checkip(ips, IPs.ip) except SystemIP.DoesNotExist: pass except IndexError: pass if check_ip: username = str(request.POST['user'].strip()) userpwd = str(request.POST['pass'].strip()) username = username.lower() check_mail = User.objects.get(email=username) if check_mail.is_superuser: check_mail.backend = 'django.contrib.auth.backends.ModelBackend' auth.login(request, check_mail) return HttpResponseRedirect(next) if settings.EMAIL_USE_TLS: server.ehlo() server.starttls() server.ehlo() server.login(username, userpwd) if check_mail.is_active: check_mail.backend = 'django.contrib.auth.backends.ModelBackend' auth.login(request, check_mail) return HttpResponseRedirect(next) else: error = "your accounts's incorrect , please contact " raise models.ObjectDoesNotExist() # except: # passwords except models.ObjectDoesNotExist: pass except smtplib.SMTPAuthenticationError: pass finally: server.quit() return render_to_response('checklog.html', { 'user': request.user, 'username': request.POST.get('username', '') }, context_instance=RequestContext(request))