| 1 |  |  | import inspect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from django.contrib import admin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from django.contrib.admin import SimpleListFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from django.core import urlresolvers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from django.db.models import Count, ForeignKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from django.utils.html import format_html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from chat import models | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from chat.models import UserJoinedInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from django.conf import settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | exclude_auto = () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | model_classes = (class_name[1] for class_name in inspect.getmembers(sys.modules[models.__name__], inspect.isclass) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 					if class_name[1].__module__ == models.__name__ and class_name[0] not in exclude_auto) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | for model in model_classes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	fields = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	class_struct = {'fields': fields, 'list_display': fields} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  | 	for field in model._meta.fields: | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 		if isinstance(field, ForeignKey): | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 			def gen_link(field): | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 				def link(obj): | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 					print(field) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 					another = getattr(obj, field.name) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 					if another is None: | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 						return "Null" | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 					else: | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 						link = '/admin/chat/{}/{}/change'.format(another._meta.verbose_name, another.id) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 						return u'<a href="%s">%s</a>' % (link, another.id) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 				link.allow_tags = True | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 				link.__name__ = str(field.name) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 				return link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			fields.append(gen_link(field)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			fields.append(field.name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	admin.site.register(model, type( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		'SubClass', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		(admin.ModelAdmin,), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		class_struct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | # class CountryFilter(SimpleListFilter): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | # 	title = 'country' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | # 	parameter_name = 'country' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | # 	def lookups(self, request, model_admin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | # 		query_set = model_admin.model.objects.values('ip__country').annotate(count=Count('ip__country')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | # 		return [(c['ip__country'], '%s(%s)' % (c['ip__country'], c['count'])) for c in query_set] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | # 	def queryset(self, request, queryset): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | # 		if self.value(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | # 			return queryset.filter(ip__country=self.value()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | # 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | # 			return queryset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | # @admin.register(UserJoinedInfo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | # class UserLocation(admin.ModelAdmin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | # 	list_display = ["time", "link_to_B"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | # 	def link_to_B(self, obj): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | # 		link = urlresolvers.reverse("admin:chat_user_change", args=[obj.Object.id])  # model name has to be lowercase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | # 		return u'<a href="%s">%s</a>' % (link, obj.B.name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | # | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | # 	link_to_B.allow_tags = True | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |