I use admin calendar and time widgets in django form: description, how to use them
But widget input time format is: "%H:%M:%S". I need "%H:%M". Here is my code:
class CorporateOrderForm(ModelForm):
class Meta:
model=Order
#exclude=('giving_address_comment','giving_address','comment')
def __init__(self, *args, **kwargs):
super(CorporateOrderForm, self).__init__(*args, **kwargs)
self.fields['order_date'].widget = widgets.AdminDateWidget()
self.fields['order_time'].widget = widgets.AdminTimeWidget(format='%H:%M')
Last row is important: I try to change format. Here is AdminTimeWidget code:
class AdminTimeWidget(forms.TimeInput):
class Media:
js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")
def __init__(self, attrs={}, format=None):
super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format)
And here is TimeInput code:
class TimeInput(Input):
input_type = 'text'
format = '%H:%M:%S' # '14:30:59'
def __init__(self, attrs=None, format=None):
super(TimeInput, self).__init__(attrs)
if format:
self.format = format
self.manual_format = True
else:
self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
self.manual_format = False
It's not work, time format not changed. Where is the error?...
Do it this way:
class CorporateOrderForm(ModelForm):
class Meta:
model=Order
widgets = {
'order_date': widgets.AdminDateWidget(),
'order_time': widgets.AdminTimeWidget(format='%H:%M')
}
Related
I have a problem with my python app. I am following a tutorial posted on: https://auth0.com/blog/developing-restful-apis-with-python-and-flask/
I try to post data to the app by power-shell:
$params = #{amount=80; description='test_doc'}
Invoke-WebRequest -Uri http://127.0.0.1:5000/incomes -Method POST -Body ($params|ConvertTo-Json) -ContentType "application/json"
When i run the PS script i get an error from my python app:
TypeError: make_income() got an unexpected keyword argument 'many'
My code looks like this:
from marshmallow import post_load
from .transaction import Transaction, TransactionSchema
from .transaction_type import TransactionType
class Income(Transaction):
def __init__(self, description, amount):
super(Income, self).__init__(description, amount, TransactionType.INCOME)
def __repr__(self):
return '<Income(name={self.description!r})>'.format(self=self)
class IncomeSchema(TransactionSchema):
#post_load
def make_income(self, data):
return Income(**data)
How am i getting the argument many into my function? Is this a marshmallow problem?
I have tried adding ** but i get the same error:
def make_income(self, **data):
return Income(**data)
I have also tried
def make_income(self, data, **kwargs):
return Income(**data)
Here is my transaction.py file
import datetime as dt
from marshmallow import Schema, fields
class Transaction():
def __init__(self, description, amount, type):
self.description = description
self.amount = amount
self.created_at = dt.datetime.now()
self.type = type
def __repr__(self):
return '<Transaction(name={self.description!r})>'.format(self=self)
class TransactionSchema(Schema):
description = fields.Str()
amount = fields.Number()
created_at = fields.Date()
type = fields.Str()
In marsmallow 3, decorated methods (pre/post_dump/load,...) must swallow unknown kwargs.
class IncomeSchema(TransactionSchema):
#post_load
def make_income(self, data, **kwargs):
return Income(**data)
(You may want to notify the blog author about this.)
In addition to accepted solution, please make the below changes:
#app.route('/incomes')
def get_incomes():
schema = IncomeSchema(many=True)
incomes = schema.dump(
filter(lambda t: t.type == TransactionType.INCOME, transactions)
)
return jsonify(incomes.data) //change it to return jsonify(incomes)
#app.route('/incomes', methods=['POST'])
def add_income():
income = IncomeSchema().load(request.get_json())
transactions.append(income.data)//change it to transactions.append(income)
return "", 204
#app.route('/expenses')
def get_expenses():
schema = ExpenseSchema(many=True)
expenses = schema.dump(
filter(lambda t: t.type == TransactionType.EXPENSE, transactions)
)
return jsonify(expenses.data)// change it to return jsonify(expenses)
#app.route('/expenses', methods=['POST'])
def add_expense():
expense = ExpenseSchema().load(request.get_json())
transactions.append(expense.data) // change it to transactions.append(expense)
return "", 204
I am following this tutorial for using Graphene with Django, and everything was going smooth, until I reached the Integration with Django Rest Framework section.
This section says that you can reuse DRF serializers with Graphene, by creating serializers clones, but it doesn't say what to do with such clones in order to reuse DRF serializers with Graphene.
These are my serializers and clones:
from rest_framework import serializers
from graphene_django.rest_framework.mutation import SerializerMutation
from GeneralApp.models import Airport
from ReservationsManagerApp.serializers import ReservationSerializer
from ReservationsManagerApp.models import ReservationComponent, ReservationHotel, ReservationRoundtrip, ReservationTransfer, ReservationTour, ReservationService, Hotel
class ReservationMutation(SerializerMutation):
class Meta:
serializer_class = ReservationSerializer
class ReservationComponentGraphSerializer(serializers.ModelSerializer):
component = serializers.SerializerMethodField()
class Meta:
model = ReservationComponent
fields = ('id', 'reservation', 'dertour_bk', 'day', 'content_type', 'object_id', 'comment', 'is_invoiced', 'component')
def get_component(self, instance):
components_models = {
'reservationhotel': ReservationHotel,
'reservationroundtrip': ReservationRoundtrip,
'reservationtransfer': ReservationTransfer,
'reservationtour': ReservationTour,
'reservationservice': ReservationService,
}
component = components_models[instance.content_type.model].objects.get(id=instance.object_id)
return self.get_component_string(instance.content_type.model, component)
def get_component_string(self, component_model, component):
components_get_string = {
'reservationhotel': self.get_hotel_string,
'reservationroundtrip': self.get_roundtrip_string,
'reservationtransfer': self.get_transfer_string,
'reservationtour': self.get_tour_string,
'reservationservice': self.get_service_string,
}
return components_get_string[component_model](component):
def get_hotel_string(self, component):
return component.hotel.name
def get_roundtrip_string(self, component):
return component.roundtrip.name
def get_transfer_string(self, component):
origin_str = self.get_place_str('origin', component)
destination_str = self.get_place_str('destination', component)
return "{} => {}".format(origin_str, destination_str)
def get_place_str(self, case, component):
places_models = {
'airport': Airport,
'hotel': Hotel,
}
if case == 'origin':
return places_models[component.origin_content_type.model].objects.get(id=component.origin_object_id).name
else:
return places_models[component.destination_content_type.model].objects.get(id=component.destination_object_id).name
def get_tour_string(self, component):
return component.tour.name
def get_service_string(self, component):
return component.service.name
class ReservationComponentMutation(SerializerMutation):
class Meta:
serializer_class = ReservationComponentGraphSerializer
And this is my schemas.py:
import graphene
from graphene_django.types import DjangoObjectType
from ReservationsManagerApp.models import Reservation, ReservationComponent
from InvoicesManagerApp.models import Invoice, InvoiceEntry, InvoiceEntryComponent
from PaymentsManagerApp.models import Payment, PaymentReservationComponent
class ReservationType(DjangoObjectType):
class Meta:
model = Reservation
class ReservationComponentType(DjangoObjectType):
class Meta:
model = ReservationComponent
class InvoiceType(DjangoObjectType):
class Meta:
model = Invoice
class InvoiceEntryType(DjangoObjectType):
class Meta:
model = InvoiceEntry
class InvoiceEntryComponentType(DjangoObjectType):
class Meta:
model = InvoiceEntryComponent
class PaymentType(DjangoObjectType):
class Meta:
model = Payment
class PaymentReservationComponentType(DjangoObjectType):
class Meta:
model = PaymentReservationComponent
class Query(object):
all_reservations = graphene.List(ReservationType)
all_reservation_components = graphene.List(ReservationComponentType)
all_invoices = graphene.List(InvoiceType)
all_invoice_components = graphene.List(InvoiceEntryType)
all_invoice_entries_components = graphene.List(InvoiceEntryComponentType)
all_payment = graphene.List(PaymentType)
all_payment_reservation_components = graphene.List(PaymentReservationComponentType)
def resolve_all_reservations(self, info, **kwargs):
return Reservation.objects.all()
def resolve_all_reservation_components(self, info, **kwargs):
return ReservationComponent.objects.select_related('reservation').all()
def resolve_all_invoice_entries_components(self, info, **kwargs):
return InvoiceEntryComponent.objects.select_related('reservation_component').all()
def resolve_all_payment_reservation_components(self, info, **kwargs):
return PaymentReservationComponent.objects.select_related('reservation_component').all()
I don't know if I am missing something obvious, but I can't understand how am I suppose to use those serializers mutations with graphene. I guess it must be by configuring the Query class in some way, but I can't find a reference in the documentation.
I don't see any reason why we have to do as shown in that tutorial. It is much easier to connect drf and graphql in following way. Doing this way,you do not need to worry about any vague classes and just rely on main aspects of drf and graphene.
Construct drf serializers normally, and connect it to graphql as shown below.
Consider we have model Subject. Let's create CRUD api for it.
from graphene.types.scalars import Scalar
class ObjectField(Scalar): # to serialize error message from serializer
#staticmethod
def serialize(dt):
return dt
class SubjectType(DjangoObjectType):
class Meta:
model=Subject
# For mutation, use serializers
#creating subject
class CreateSubject(graphene.Mutation):
subject=graphene.Field(SubjectType)
message=ObjectField()
status=graphene.Int()
class Arguments:
name=graphene.String(required=True)
description=graphene.String(required=True)
#classmethod
def mutate(cls,root,info,**kwargs):
serializer=SubjectSerializer(data=kwargs)
if serializer.is_valid():
obj=serializer.save()
msg='success'
else:
msg=serializer.errors
obj=None
print(msg)
return cls(subject=obj,message=msg,status=200)
'''Updating subject'''
class UpdateSubject(graphene.Mutation):
subject=graphene.Field(SubjectType)
status=graphene.Int()
message=ObjectField()
class Arguments:
id=graphene.ID(required=True)
name=graphene.String()
description=graphene.String()
#classmethod
def mutate(cls,root,info,id,**kwargs):
sub=Subject.objects.get(id=id)
serializer=SubjectSerializer(sub,data=kwargs,partial=True)
if serializer.is_valid():
obj=serializer.save()
msg='success'
else:
msg=serializer.errors
obj=None
print(msg)
return cls(subject=obj,message=msg,status=200)
'''Delete Subject'''
class DeleteSubject(graphene.Mutation):
message=ObjectField()
status=graphene.Int()
class Arguments:
id=graphene.ID(required=True)
#classmethod
def mutate(cls,root,info,id,**kwargs):
c=Subject.objects.get(id=id)
c.delete()
return cls(message='success',status=200)
class Mutation(graphene.ObjectType):
create_subject=CreateSubject.Field()
update_subject=UpdateSubject.Field()
delete_subject=DeleteSubject.Field()
# Query is normal.
class Query(graphene.ObjectType):
subject=graphene.Field(SubjectType,id=graphene.Int(), slug=graphene.String())
subjects=graphene.List(SubjectType)
def resolve_subject(self, info, id=None, slug=None):
if id:
return Subject.objects.get(id=id)
if slug:
return Subject.objects.get(slug=slug)
def resolve_subjects(self,info,**kwargs):
return Subject.objects.all()
You can try making little framework-like thing for yourself to avoid redundant code as seen.
From your example:
import graphene
from your.schemas import Query as YourQuery
from your.serializers import ReservationComponentMutation
# notice its an objecttype, and i've also added some debug
class Mutation(graphene.ObjectType):
debug = graphene.Field(DjangoDebug, name="_debug")
create_reservation = ReservationComponentMutation.Field()
class Query(YourQuery, graphene.ObjectType):
pass
class Mutation(Mutation, graphene.ObjectType):
pass
root_schema = graphene.Schema(query=Query, mutation=Mutation)
And the url:
urlpatterns = (
url(r"^graphql", GraphQLView.as_view(schema=root_schema graphiql=True), name="graphql"),
)
# you can wrap csrf_exempt(GraphQLView.as_view(...)) for testing
# or you can setup the frontend `apollo-client` to use csrf_tokens
# also see the related links below
For example for apollo-client and not apollo-boost the window.csrf_token is {% csrf_token %} in the template rendering:
import { ApolloProvider } from "react-apollo";
import ApolloClient from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { InMemoryCache as Cache } from "apollo-cache-inmemory";
import { ApolloLink } from "apollo-link";
import fetch from "unfetch";
const uri = UrlUtils.makeUrl(Urls.graphQl);
const AuthLink = (operation, next) => {
const token = window.csrf_token;
operation.setContext(context => ({
...context,
headers: {
...context.headers,
"X-CSRFToken": token
}
}));
return next(operation);
};
const link = ApolloLink.from([
AuthLink,
new HttpLink({
uri,
credentials: "same-origin",
fetch // override fetch implementation for polyfills
})
]);
const apollo = new ApolloClient({
link,
cache: new Cache().restore({})
});
You should then be able to:
query TestQuery() {
resolveAllReservations () {
id
}
}
or:
mutate TestMutate($input: ReservationComponentMutationInput!) {
createReservation(input: $input) {
id
errors {
field
messages
}
}
_debug {
sql {
rawSql
}
}
}
related:
https://stackoverflow.com/a/39026832/2026508
I have a dynamic form:
class CollectionRequestParamForm(Form):
param_human_name = StringField(validators=[DataRequired()])
param_request_name = StringField()
class CollectionRequestParamCombinedForm(FlaskForm):
params = FieldList(FormField(CollectionRequestParamForm), min_entries=2)
submit = SubmitField('Submit')
And I want to set min_entries from code in view, like:
collection_request_params_form = CollectionRequestParamCombinedForm(min_entries=5)
I do this:
class CollectionRequestParamCombinedForm(FlaskForm):
def __init__(self, min_entries, *args, **kwargs):
FlaskForm.__init__(self, *args, **kwargs)
self.params = FieldList(FormField(CollectionRequestParamForm), min_entries=min_entries)
submit = SubmitField('Submit')
But: TypeError: 'UnboundField' object is not iterable
What is causing this error?
do this if you want to make it dynamic
for i in range(min_entries):
form.params.append_entry()
more for details https://stackoverflow.com/a/48710958/9218468
I have a view set like this
class NeProjectsViewSet(viewsets.ViewSet):
def list(self, request,org_unique_id):
''' something '''
def create(self, request,org_unique_id):
''' something '''
def retrieve(self):
''' something '''
def update(self, request, pk):
''' something '''
def partial_update(self, request):
''' something '''
def destroy(self, request):
''' something '''
and i've a method like this
def check_session(self,request):
current_datetime = datetime.now()
if ('last_login' in request.session):
last = (current_datetime - datetime.strptime(request.session['last_login'], "%Y-%m-%d %H:%M:%S.%f")).seconds
if last > base.SESSION_IDLE_TIMEOUT:
del request.session['token']
raise ValueError('Session Expired')
else:
request.session['last_login'] = str(current_datetime)
return (request.session['token'] == request.META['HTTP_AUTHORIZATION'])
to validate session for every request, for that i need to call this method before every method in the viewset. I read somewhere writing custom decorator is better way, so how to implement custom decorator for my view set to check session for request
Assuming you are using DRF.
I think you are going in wrong direction. If this is part of your permission layer you should just add custom permission class to your viewset
http://www.django-rest-framework.org/api-guide/permissions/
from rest_framework import permissions
class ValidateSession(permissions.BasePermission):
"""
Validate session expiration
"""
def has_permission(self, request, view):
current_datetime = datetime.now()
if ('last_login' in request.session):
last = (current_datetime - datetime.strptime(request.session['last_login'], "%Y-%m-%d %H:%M:%S.%f")).seconds
if last > base.SESSION_IDLE_TIMEOUT:
del request.session['token']
return False
else:
request.session['last_login'] = str(current_datetime)
return (request.session['token'] == request.META['HTTP_AUTHORIZATION'])
And then add it like this
class NeProjectsViewSet(viewsets.ViewSet):
permission_classes = (ValidateSession,)
...
Assuming you're using plain django
from django.contrib.auth.mixins import AccessMixin
class ValidateSessionMixin(AccessMixin):
"""
Validate session
"""
def has_permission(self):
current_datetime = datetime.now()
request = self.request
if ('last_login' in request.session):
last = (current_datetime - datetime.strptime(request.session['last_login'], "%Y-%m-%d %H:%M:%S.%f")).seconds
if last > base.SESSION_IDLE_TIMEOUT:
del request.session['token']
return True
else:
request.session['last_login'] = str(current_datetime)
return (request.session['token'] == request.META['HTTP_AUTHORIZATION'])
def dispatch(self, request, *args, **kwargs):
if not self.has_permission():
return self.handle_no_permission()
return super(ValidateSessionMixin, self).dispatch(request, *args, **kwargs)
And apply this mixin like this
class NeProjectsViewSet(ValidateSessionMixin, viewsets.ViewSet):
...
I'm trying to generate a custom HTML and I have a value I want to pass into xml.startElement (or root if you're thinking in generic terms). How do I go about doing this?
I'm currently using django rest framework a class view and a custom renderer -
This is the beginning of the renderer -
class TESTRenderer(renderers.BaseRenderer):
media_type = 'application/xml'
format = 'xml'
charset = 'utf-8'
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Renders *obj* into serialized XML.
"""
if data is None:
return ''
stream = StringIO()
xml = SimplerXMLGenerator(stream, self.charset)
xml.startDocument()
xml.startElement(header.data, {})
So as you can see I'm trying to pass a variable called header into the xml.startElement
Here's the view where that data lies -
class TestDetail(APIView):
permission_classes = (AllowAny,)
"""
Retrieve, update or delete a snippet instance.
"""
def get(self, request, pk, format=None):
jobmst_name = queryset1
nodmst_alias = queryset2
sysval_integer = queryset3
mst = queryset4
dtl = queryset5
dep = queryset6
trg = queryset7
name = str(jobmst_name)
master = str(nodmst_alias)
dbversion = str(sysval_integer)
header = 'job id="%s" name="%s" master="%s" dbversion="%s" xmlversion="1"' % (pk, name, master, dbversion)
jobmststring = JobmstSerializer(mst)
jobdtlstring = JobdtlSerializer(dtl)
jobdepstring = JobdepSerializer(dep, many=True)
trgjobstring = TrgjobSerializer(trg, many=True)
jobmst_serialized = {'jobmst': jobmststring.data}
jobdtl_serialized = {'jobdtl': jobdtlstring.data}
jobdep_serialized = [{'jobdep':item} for item in jobdepstring.data]
trgjob_serialized = [{'trgjob':item} for item in trgjobstring.data]
jobgroup = header, jobmst_serialized, jobdtl_serialized, jobdep_serialized, trgjob_serialized
return TestResponse(jobgroup)
The response it's using is here -
class TestResponse(HttpResponse):
"""
An HttpResponse that renders its content into XML.
"""
def __init__(self, data, **kwargs):
content = TESTRenderer().render(data)
kwargs['content_type'] = 'application/xml'
super(TestResponse, self).__init__(content, **kwargs)
Is there something I'm missing with the TestDetail where I should separate the header from the data?
maybe like this?
return TestResponse (header, jobgroup)
and then alter TestResponse to include?
def __init__(self, header, data, **kwargs):
I don't know python/django. but it seems the "Value" you are talking about are actually attributes you want to assign to the element node. I posted the same on your /r/django thread about this.