Best practice for serving restricted or premium media
I'm currently researching Django as a potential framework for a web application I am building. We plan to make a variety of media (images and videos) free on our website; however, we want to have some premium media available only when certain criteria are met (e.g. the logged-in user is of a certain group or has the appropriate permissions). What is considered the best way to implement this? I suppose we could get Django to serve the media directly, but this would place undue load on the server. Can anyone recommend any tips or tricks? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Best practice for serving restricted or premium media
Thanks for the great suggestions. I believe that we will start out with shared hosting the migrate to a VPS (e.g. SliceHost) if scaling becomes an issue. We're not really expecting to become a high-traffic website (our target audience is small). I'm not familiar with any of these approaches, but I can now use Google to find and review some tutorials regarding each of these. Will they all work on shared hosting? Brad On Tue, Apr 20, 2010 at 10:50 PM, Graham Dumpleton < graham.dumple...@gmail.com> wrote: > > > On Apr 21, 12:09 pm, Brad Buran wrote: > > I'm currently researching Django as a potential framework for a web > > application I am building. We plan to make a variety of media (images > > and videos) free on our website; however, we want to have some premium > > media available only when certain criteria are met (e.g. the logged-in > > user is of a certain group or has the appropriate permissions). > > > > What is considered the best way to implement this? I suppose we could > > get Django to serve the media directly, but this would place undue > > load on the server. Can anyone recommend any tips or tricks? > > Groups and permissions are obviously things that can be handled by > Django. For the actual serving up of the media files, there are > various ways of doing it depending on how you are hosting Django. > > So, how are you intending to host it? > > Options for serving static media where Django handles request and > handler deals with groups or permissions are: > > 1. Use X-Sendfile response header to have server send raw file. > Supported by Apache, lighttpd and nginx, although possibly require > option server module, eg mod_xsendfile. > > 2. Have nginx as front end with fastcgi backend, or with Apache/ > mod_wsgi as backend and use X-Accel-Redirect response header to map to > private static files hosted by nginx. > > 3. Under Apache/mod_wsgi, use wsgi.file_wrapper extension of WSGI to > return file. This may be hard as not really well supported by Django > natively at this point. > > 4. Under Apache/mod_wsgi daemon mode, use Location response header > with 200 reponse to map to static file served by Apache. This is like > X-Accel-Redirect under nginx, but you need a mod_rewrite rule to > restrict access to static media sub request as created by Location > directive redirect. > > 5. Use perlbal as front end to backend HTTP server host Django in some > way and use X-Reproxy-URL response header. Like Other variants above > but perlbal sends static file identified by that header. > > Other load balancers may support other similar headers for having them > send static files. > > There are other ways as well provided you were happy with standard > HTTP Basic/Digest authentication being done by Apache and not form/ > session based logins. > > There is a Django ticket, which I believe still hasn't been > integrated, to try and make various of these available under a simple > usable interface. As such, right now, may have to integrate it > explicitly. > > So, lots to choose from. > > Graham > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
auto generate "dummy" content (e.g. Lorem ipsum generator for Django)
Is there any easy way of generating dummy content for models in Django? As I'm learning how to use Django, I often find myself deleting the sqlite database and running syncdb each time (rather than dealing with the issues of manual schema migration each time I make a change to my models). As part of this, I'd like to regenerate some dummy content so I can test the various views and templates quickly. It seems pretty straightforward: the model.*Fields can give the necessary clues as to the content that can be generated. Before I start writing this, I thought I'd check to see if anyone has done something similar. I tried searching for a similar app, but couldn't find anything. Brad -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
best practice for widget that edits multiple model fields
I have a group of fields (date_start, date_end, all_day) that I would like to reuse in several models. I've defined a MultiWidget that displays two split datetime fields plus a checkbox (for the all_day toggle). However, I'm not sure how to get the MultiWidget to set the value of these three fields in the model. If I understand correctly, a MultiWidget can only set the value of a single model field. Hence, I would need to create some sort of field (e.g. "combined_date_info") that MultiWidget saves the serialized data from these three form fields. Once I have done this, then I would use model.save() to deserialize the data in the combined_date_info field and save it to the appropriate model fields. This solution seems a bit hackish and not very DRY. Are there better approaches? Brad -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: best practice for widget that edits multiple model fields
Hi Scott: Thank you for the suggestion. I reviewed the docs, and it appears like it could work but would restrict the functionality. For example, I would have to somehow serialize two datetime types plus a boolean type into a single database column. This would take away a lot of advantages (such as being able to filter based on only one of the datetimes). One other option I am exploring is simply doing: class DateRange(Model): start = DateTimeField() end = DateTimeField() On Wed, Jul 21, 2010 at 8:03 AM, Scott Gould wrote: > My immediate thought upon reading your post was that you should build > an accompanying custom model field. Having said that, I haven't had > cause to do one that involved (only extend -- simply -- the stock > fields) so can't provide any specifics. > > > http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/#howto-custom-model-fields > > On Jul 20, 10:00 pm, Brad Buran wrote: > > I have a group of fields (date_start, date_end, all_day) that I would > like > > to reuse in several models. I've defined a MultiWidget that displays two > > split datetime fields plus a checkbox (for the all_day toggle). However, > > I'm not sure how to get the MultiWidget to set the value of these three > > fields in the model. If I understand correctly, a MultiWidget can only > set > > the value of a single model field. Hence, I would need to create some > sort > > of field (e.g. "combined_date_info") that MultiWidget saves the > serialized > > data from these three form fields. Once I have done this, then I would > use > > model.save() to deserialize the data in the combined_date_info field and > > save it to the appropriate model fields. > > > > This solution seems a bit hackish and not very DRY. Are there better > > approaches? > > > > Brad > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: best practice for widget that edits multiple model fields
<> Hi Scott: Thank you for the suggestion. I reviewed the docs, and it appears like it could work but would restrict the functionality. For example, I would have to somehow serialize two datetime types plus a boolean type into a single database column. This would take away a lot of advantages (such as being able to filter based on only one of the datetimes). One other option I am exploring is simply doing: class DateRange(Model): start = DateTimeField() end = DateTimeField() all_day = BooleanField() # subclass DateRange so we can reuse it in various models class EventDateRange(DateRange): date = ForeignKey(Event) class Event(Model): #various other fields required for event Does this seem like it would make more sense? Thanks, Brad On Wed, Jul 21, 2010 at 8:03 AM, Scott Gould wrote: > My immediate thought upon reading your post was that you should build > an accompanying custom model field. Having said that, I haven't had > cause to do one that involved (only extend -- simply -- the stock > fields) so can't provide any specifics. > > > http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/#howto-custom-model-fields > > On Jul 20, 10:00 pm, Brad Buran wrote: > > I have a group of fields (date_start, date_end, all_day) that I would > like > > to reuse in several models. I've defined a MultiWidget that displays two > > split datetime fields plus a checkbox (for the all_day toggle). However, > > I'm not sure how to get the MultiWidget to set the value of these three > > fields in the model. If I understand correctly, a MultiWidget can only > set > > the value of a single model field. Hence, I would need to create some > sort > > of field (e.g. "combined_date_info") that MultiWidget saves the > serialized > > data from these three form fields. Once I have done this, then I would > use > > model.save() to deserialize the data in the combined_date_info field and > > save it to the appropriate model fields. > > > > This solution seems a bit hackish and not very DRY. Are there better > > approaches? > > > > Brad > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Sorting related model
Given the following two models forming the base of a wiki-style app: class Page(models.Model): current_revision = models.IntegerField() class PageContent(models.Model): page = models.ForeignKey(Page) revision = models.IntegerField() created = models.DateTimeField() Here, we have a canonical Page object with a current_revision that always points to the most current revision, stored in PageContent. How can I construct a Page queryset, ordered by the Pages that have the most recently-created PageContent? Currently, what I am doing is: qs = PageContent.objects.filter(revision=F('page__current_revision')).order_by('created').select_related() pages = [content.page for content in qs] This works fine, but is there a more straightforward way to do this? Thanks, Brad -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
problem reversing namespaced URL (application instance not recognized?)
I'm trying to reuse a wiki app twice in my project; however, whenever I call the reverse function, it returns the URL of the first instance defined in my urls.conf. I have attempted specifying the view name as app_instance:view_name, app_name:view_name as well as using the current_app keyword of the reverse function. I have not had any luck so far and was hoping for suggestions on what I'm doing wrong. I've only posted the relevant parts of the code. Per the Django documentation on namespacing, I have set up my top-level URLs conf as: ... (r'help/', include('test.wiki.urls', 'wiki', 'help'), {'wiki': 'help'}), (r'learn/wiki/', include('test.wiki.urls', 'wiki', 'learn'), {'wiki': 'learn'}), ... In my wiki app, the urls.conf has url(r'^(?P[-\w]+)/$', 'test.wiki.views.display', name='wiki_page_detail'), url(r'^(?P[-\w]+)/404$', direct_to_template, {'template': 'wiki/page_404.html'}, name='wiki_page_404') Finally, in my display view function (note that this uses the wiki keyword defined in the top-level urls.conf to know which : def display(request, wiki, slug): try: wiki = models.Wiki.objects.get_or_create(wiki) page = models.Page.objects.get(wiki=wiki, slug=slug) except models.Page.DoesNotExist: url = reverse('%s:wiki_page_404' % wiki.title, kwargs={'slug': slug}, current_app=wiki.title) return HttpResponseRedirect(url) Thanks, Brad -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: problem reversing namespaced URL (application instance not recognized?)
Problem solved. I misunderstood the Django docs on URL namespacing and was trying to take a shortcut by passing a 3-tuple rather than explicitly naming the arguments to include. Instead of include('test.wiki.urls', 'wiki', 'help') I should have been doing include('test.wiki.urls', app_name='wiki', namespace='help'). Sorry for the trouble, Brad On Sun, Oct 17, 2010 at 6:14 PM, Brad Buran wrote: > I'm trying to reuse a wiki app twice in my project; however, whenever I > call the reverse function, it returns the URL of the first instance defined > in my urls.conf. I have attempted specifying the view name as > app_instance:view_name, app_name:view_name as well as using the current_app > keyword of the reverse function. I have not had any luck so far and was > hoping for suggestions on what I'm doing wrong. I've only posted the > relevant parts of the code. > > Per the Django documentation on namespacing, I have set up my top-level > URLs conf as: > > ... > (r'help/', include('test.wiki.urls', 'wiki', 'help'), {'wiki': 'help'}), > (r'learn/wiki/', include('test.wiki.urls', 'wiki', 'learn'), {'wiki': > 'learn'}), > ... > > In my wiki app, the urls.conf has > > url(r'^(?P[-\w]+)/$', 'test.wiki.views.display', > name='wiki_page_detail'), > url(r'^(?P[-\w]+)/404$', direct_to_template, {'template': > 'wiki/page_404.html'}, name='wiki_page_404') > > Finally, in my display view function (note that this uses the wiki keyword > defined in the top-level urls.conf to know which : > > def display(request, wiki, slug): > try: > wiki = models.Wiki.objects.get_or_create(wiki) > page = models.Page.objects.get(wiki=wiki, slug=slug) > except models.Page.DoesNotExist: > url = reverse('%s:wiki_page_404' % wiki.title, kwargs={'slug': > slug}, current_app=wiki.title) > return HttpResponseRedirect(url) > > > Thanks, > Brad > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Implementing query with Django ORM - left join with length() and replace() functions
I'm trying to replicate a query using Django's ORM; however, I'm having a bit of difficulty figuring out how to accomplish it. The model that I'm querying is defined as: class Word(models.Model): objects = WordManager() spelling = models.CharField(max_length=128) ipa = models.CharField(max_length=128) The query I'm trying to accomplish: SELECT a.id, a.spelling, a.ipa b.spelling as b_spelling, b.ipa as b_ipa FROM dictionary_word as a, dictionary_word as b WHERE a.ipa != b.ipa and length(a.ipa) = length(b.ipa) and a.ipa = replace(b.ipa, '%s', '%s') The idea of this query is to find all pairs of words that differ by one phoneme (e.g. "mat" and "bat" differ by only one sound). Is this query possible to accomplish with the ORM, or do I need to use a raw query? Thanks, Brad -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Replicating complex query with two dense_rank() functions using ORM
I have a "puzzle of the day" that users can answer. I track the puzzle of the day using the Puzzle model. I track the answers using the PuzzleAnswer model. I would like to calculate the number of consecutive puzzles a particular user (i.e., the author) gets right in a row. The current SQL I use that can calculate the start date of the streak, end date of the streak and the number of days in the streak. As you can see, it does a dens_rank over the puzzles (to count them in order), then does a join with the PuzzleAnswer, then does a second dense rank over the merged tables. I figured out how to use the DenseRank function in the Django ORM on the Puzzle manager, but I cannot figure out how to do the left join next. Any advice? SELECT min(s.id) AS id, count(s.date) AS streak, min(s.date) AS start_streak, max(s.date) AS end_streak, s.author_id FROM ( SELECT dense_rank() OVER (ORDER BY ROW(pa.author_id, pr.rank)) AS id, pa.created AS date, (pr.rank - dense_rank() OVER (ORDER BY ROW(pa.author_id, pr.rank))) AS g, pa.author_id FROM (( SELECT "POTD_puzzle".id, dense_rank() OVER (ORDER BY "POTD_puzzle".published) AS rank FROM public."POTD_puzzle") pr JOIN public."POTD_puzzleanswer" pa ON ((pr.id = pa.puzzle_id))) WHERE pa.correct) s GROUP BY s.author_id, s.g ORDER BY count(s.date) DESC; The models are: class PuzzleAnswer(models.Model): puzzle = models.ForeignKey(Puzzle, editable=True, on_delete=models.CASCADE) answer = models.CharField(max_length=64) correct = models.BooleanField(editable=False) created = models.DateTimeField(auto_now_add=True) author = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL) class Puzzle(models.Model): category = models.ForeignKey(PuzzleCategory, on_delete=models.CASCADE, help_text=category_help) notation = models.CharField(max_length=64) correct_answer = models.CharField(max_length=64) published = models.DateField(blank=True, null=True, db_index=True, unique =True) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3d8106a7-df44-4697-91fe-06c861132ae6n%40googlegroups.com.